simple program, strange result

A

andthen

Here is the code:

#include <stdio.h>

int main() {
double a = 3.01;
int b = (int)(a * 100.0);
printf("%d",b);
return 0;
}

I believe that the output should be "301", however I am getting output of
"300" when I compile this program on borland's compiler (bcc32) and run it.
I also tried a compiler I got from the internet called lcc, which also leads
to output of "300". I tried Microsoft's compiler and it gave me "301". Does
anyone know what the explanation is for these strange results? Also I do not
have access to gcc right now, but I would be interested to know which output
it gives.
 
H

Hans-Christian Egtvedt

Here is the code:

#include <stdio.h>

int main() {
double a = 3.01;
int b = (int)(a * 100.0);
printf("%d",b);

modified to:
printf("%d\n",b);
return 0;
}

I believe that the output should be "301", however I am getting output of
"300" when I compile this program on borland's compiler (bcc32) and run it.
I also tried a compiler I got from the internet called lcc, which also leads
to output of "300". I tried Microsoft's compiler and it gave me "301". Does
anyone know what the explanation is for these strange results? Also I do not
have access to gcc right now, but I would be interested to know which output
it gives.

Made a quick file called test.c

~ > gcc -Wall -o test test.c
~ > ./test
300

~ > gcc -Wall -O2 -o test test.c
~ > ./test
301
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top