jwala said:
can anybody explain me plz, why this code doesn't print 1.0??
#include <stdio.h>
void main(){
float x;
for (x=0;x<=1.0;x+=0.1)
printf("%0.1f ",x);
}
In this short piece of code, you demonstrate at least three times that
you have violated the norms of behavior for posting. Had you followed
the newsgroup, checked the archives, or checked the FAQs before posting,
you would have known that main returns an int, that floating point
arithmetic is inexact for all but numbers whose denominators are powers
of 2, that the behavior of programs without an end-of-line character at
the end of the last output line isn't portably defined, and that in the
prevailing C89/C90 standard, a return value from main should be supplied.
Try running this:
#include <stdio.h>
#include <float.h>
int /* NOT 'void' */ main(void)
{
float x;
for (x = 0; x <= 1.0; x += 0.1) ;
printf("%0.*g %0.*g\n", FLT_DIG, x, FLT_DIG, x - 1);
return 0;
}
[on my implementation]
1 1.19209e-07