(e-mail address removed) opined:
hi everyone,
main()
{
float f=0.7;
if ( f< 0.7)
printf("C");
else
printf("C++");
}
main()
{
float f=0.7;
if(f < 0.7f)
printf("C");
else
printf("C++");
}
The first code snippet prints C and the second one prints C++.Can
anyone tell me the reason for it
No they don't (or at least not necessarily).
a) you do not include <stdio.h> so `printf` is unknown
b) you do not terminate output with '\n'
c) it's `int main(void)`
d) you're missing `return 0;` (or whatever value)
However, your problem is in the fact that undecorated floating point
constants, like `0.7` are of type `double`, by appending 'f', you make
them `float`. Now, if sizes of your `float`s and `double`s are
different representation of 0.7 in them may differ. Adding:
printf("%20.10f %20.10f\n",0.7, 0.7f);
to your code (after fixing the rest) I get (gcc 4.0.3pre, SUSE 10.0):
0.7000000000 0.6999999881
The morale is: if you're going to use floating point numbers in your
applications, carefully study their limitations, and your particular
implementation. Also look into C Standard to see what are the minumum
requirements for a conforming implementation. This advice applies
equally to any programming language you choose.
HTH
--
if (argc > 1 && strcmp(argv[1], "-advice") == 0) {
printf("Don't Panic!\n");
exit(42);
}
(Arnold Robbins in the LJ of February '95, describing RCS)
<
http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>