relational expression querry

hello..
i was solving question and suddenly i came across this question
int main()
{
float pi=3.14,x=2.5;
int a,b;
a=(pi==3.14);
b=(x==2.5);
printf("%d,%d"a,b);
return 0;
}

the ans given there was 0,1
but it should be either 0,0 or 1,1...this baffled me. So pls give me the reason for the ans. Thanx in advance..:)
closed account (zb0S216C)
The result I received was 1, 1, as expected according to you. You have a truncation warning as well as a missing comma within the printf( ) argument list (after the string literal).

Wazzak
Last edited on
I got 0, 1 also. Weird. It gave me 1,1 when i put float pi = 3.14f; Dunno what's happening. Oo
thats what it is puzzling me....how can u get 0,1 ??? i tried float a,b;
still the ans is 0,1 :(
@framework: what compiler are u using,GCC????
because in turbo it is giving 0,1 ans.
i got no truncation warning
Last edited on
closed account (zb0S216C)
pulkitsharva wrote:
what compiler are u using,GCC???? (sic)

Microsoft's compiler that came with VCE '10 (don't know the official name).

pulkitsharva wrote:
in turbo it is giving 0,1 ans (sic)

Why is your software so old? Correct me if I'm wrong, but wasn't Turbo C++ released in 1991 (when I was born (I'm now 19; 20 this year))?

pulkitsharva wrote:
i got no truncation warning (sic)

I think any decent compiler would detect warnings such as truncation.

You really need to state your compiler in your original post. That way, the users that reply have the necessary information.

Wazzak
Last edited on
closed account (z05DSL3A)
Technically, the possible truncation of (float pi=3.14,x=2.5;) is not the problem but should point you to the problem.

pi==3.14, this is comparing a float with a double literal, comparing floating point types is error prone at best but worse when comparing different precisions.
Last edited on
@framework: hey hey man i 'm using oturboc because i'm familar to it....btw i have got the ans...
the ans is this turbo compiler divide the last digit into number of bits and if the last digit is 5 or greater than it the ans will be 1. so if we have enter 2 number like pi=3.15,x=2.5 we will get ans 1,1.....
and if we have enter number like pi=3.14,x=2.2(2.4) we will get ans o,o...:)
Topic archived. No new replies allowed.