Friday 10 November 2017

c - what's is going wrong with this loop condition?






Look at the output of href="http://ideone.com/7oK3Xl" rel="noreferrer">this link(scroll down to
see the output) to find out what I'm trying to
accomplish



The problem is with the
for loop on line number
9-11



for(i=0; i<=0.9;
i+=0.1){
printf("%6.1f
",i);
}


I
expected this to print values from 0.0 until 0.9 but it stops after printing 0.8, any
idea why ??



itemprop="text">
class="normal">Answer



Using
float here is source of problem. Instead, do it with an
int:



int i;
for(i = 0; i
<= 10; i++)
printf("%6.1f ", (float)(i /
10.0));


Output:




0.0
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...