Click to See Complete Forum and Search --> : Help this C newbie!!!
Anil roy
04-07-2002, 09:24 AM
Please could someone tell me why this simple piece of code works but in the output all the miles are 0.Please note that the entire printf line is in one line.
#include <stdio.h>
int main()
{
int a;
a = 0;
while (a <= 100)
{
printf("%4d kilometres K = %4d miles M\n", a, a*1.6);
a = a + 10;
}
return 0;
}
marvin
04-07-2002, 10:45 AM
The "a*1.6" is an integer times a double, which results in a double. printf expects an integer (since you put a %d in the format string) so it probably gets a little confused.
You can tell printf to print the miles as a double (use %f instead of %d) or you could convert the result of the multiplication back to an integer, i.e. "(int) (a*1.6)"
Btw, use the code tags when posting code, it will keep the indentation so the example code is easier to read.
Anil roy
04-07-2002, 01:33 PM
Thanks a lot.
I used %f instead of %d and this tinny program ran like a breeze.
I just started my course in C yesterday and hope that I can become an expert at it.
Disc0stoo
04-07-2002, 03:17 PM
Yeah, you'll find more and more as you get into C that you gotta watch those variable types. Can be a pain sometimes...