Posted by sun_vivek on 18:07:00 02-10-2003
look at the program lines:
int a;
a=15+1.0;
printf("%d",a);
/*gives an output 16*/
NOW
printf("%d",15+1.0);
/*gives an output zero !!! why????*/
all the operations which contain any floating point number gives an output 0 .
could u please tell me why??????
Posted by Smerdyakov on 23:08:00 02-10-2003
The %d format specifier for printf expects an integer. You are passing a floating point value in the second example, so you are essentially asking for random output. In the first case, you assign the result back to a first, so it is automatically converted to an integer.
Posted by KaGez on 20:13:00 02-11-2003
a=15+1.0
also doesn't work with _all_ compilers. Some will refuse to compile that... with some compilers you will need to do something like:
a=15+(int)1.0 or
a=(int)(15+1.0)
but the rest is as Smerdyakov exmaplied it.
[addsig]