main()
{
float a=8.8;
double b=8.8;
if(a==b)
printf("Equal");
else
printf("not equal");
getch();
}
what is the output?
with reason

Answers were Sorted based on User's Feedback



main() { float a=8.8; double b=8.8; if(a==b) printf("Equal"); else printf("no..

Answer / vignesh1988i

here it will print : either NOT EQUAL or EQUAL
according to me and my compailer it's NOT EQUAL only.......
depends upon the compailer actually......

here variable 'a' is assigned wit float and 'b' is assigned
with double....
here comes the problem ie. of allocation of no. of bytes

according to my compailer float allocates 4 bytes and double
allocates 8 bytes.... so when it compares each location bit
by bit the float will lag by two bytes of space than double
has .. so since the comparsion takes a decision tat it's not
equal since it dosent check the other two locations of
double wit float which is lagging wit that two bytes of
memory space....... so it prints NOT EQUAL


thank you

Is This Answer Correct ?    19 Yes 2 No

main() { float a=8.8; double b=8.8; if(a==b) printf("Equal"); else printf("no..

Answer / rakesh

It will print "not equal". Reason is the difference in
precision of the numbers. ie numbers like 8.8 or 8.1 can't
be stored with complete precision in binary sysetm since
it's mantissa part will not end but continues with a
series. So value calculated for single precision(float)
number will be slightly different from the value calculated
for double precision (double) number. To verify this use
gcc and gdb in linux.

If you try with numbers 8.25, 8.5 or 8.75 the program will
print "equal" since the mantissa part ends with in the
precision.

I think this has nothing to do with compiler version or
inabilty of comparision.

Is This Answer Correct ?    10 Yes 0 No

main() { float a=8.8; double b=8.8; if(a==b) printf("Equal"); else printf("no..

Answer / pushkar pahare

This problem has different answers on different compilers.
I had used turboc3, where is always printf "not equal".
Because the size of float is 2 bytes and size of double is
4 bytes, So, when compiler checks this, it can only compare
two bytes rest are left un checked and thus it concludes
false condition. It is same on RHEL5 environment.

Where as on Visual Studio 6.0, It will yield "Equal",
because here both the float and double variable acquires
the same space in memory. Thus the compiler can compare all
four bytes of memory which in fact contains the same values
because the representation of value is same in float and in
double.

Is This Answer Correct ?    1 Yes 0 No

main() { float a=8.8; double b=8.8; if(a==b) printf("Equal"); else printf("no..

Answer / hema

If i want to compare the value stored in float and double,
how do i do that? I tried typecasting i,e.,

if (a==float(b))
printf("Equal");
else
printf("not equal");

Even then it say, not equal. How to overcome this problem?

Is This Answer Correct ?    1 Yes 0 No

main() { float a=8.8; double b=8.8; if(a==b) printf("Equal"); else printf("no..

Answer / sandy0103

I am NOT sure, if it is something to do with the compiler,
rather it is something to do with the precision. Float has
got single precision and double is double precision. This
precision difference will get different value when you see
raw value. In another sense, I tried debugging the program
in VS2005 debugger, and if we monitor the value using
watch, you can see for float it is taking the value
as "8.8000002" and for double, it is taking value as "
8.8000000000000007". And hence the precision plays a role
here.

Is This Answer Correct ?    0 Yes 0 No

main() { float a=8.8; double b=8.8; if(a==b) printf("Equal"); else printf("no..

Answer / vikram

answer is not equal;
bcoz float cannot be compared with double.

Is This Answer Correct ?    5 Yes 8 No

Post New Answer

More C Interview Questions

How main function is called in c?

0 Answers  


why to assign a pointer to null sometimes??how can a pointer we declare get assigned with a garbage value by default???

0 Answers  


what are the stoge class in C and tel the scope and life time of it?

2 Answers   Tech Mahindra,


Explain a pre-processor and its advantages.

0 Answers  


There is a number and when the last digit is moved to its first position the resultant number will be 50% higher than the original number.Find the number?

1 Answers  


we have to use realloc only after malloc or calloc ? or we can use initially with out depending on whether we are using malloc or calloc in our program ?

2 Answers  


How to write a code for implementing my own printf() and scanf().... Please hep me in this... I need a guidance... Can you give an coding for c... Please also explain about the header files used other than #include<stdio.h>...

0 Answers  


In the below code, how do you modify the value 'a' and print in the function. You'll be allowed to add code only inside the called function. main() { int a=5; function(); // no parameters should be passed } function() { /* add code here to modify the value of and print here */ }

1 Answers  


if (i = 0)printf ("True"); elseprintf("False"); Under what conditions will the above print out the string "True" a) Never b) Always c) When the value of i is 0 d) all of the above

0 Answers  


Why can’t constant values be used to define an array’s initial size?

0 Answers  


Who had beaten up hooligan "CHAKULI" in his early college days?

1 Answers  


What does emoji p mean?

0 Answers  


Categories