HI,i was trying to do the following.I tried but cant figure out my error.I want the output to be 123.456
Code:#include <stdio.h>
int main(void){
unsigned int a=123456;
double b=a/1000;
printf("%d",b); //123.456 output wanted
}
Do help.. :(
Printable View
HI,i was trying to do the following.I tried but cant figure out my error.I want the output to be 123.456
Code:#include <stdio.h>
int main(void){
unsigned int a=123456;
double b=a/1000;
printf("%d",b); //123.456 output wanted
}
Do help.. :(
Two mistakes:
1. dividing two integers gives an integer answer. You want a floating point answer, so you need to make sure at least one of the numbers is floating point e.g. 1000.0
2. %d is the wrong format specifier for a double. Read your documentation again to find the correct specifier.
It's %f, not %lf, which you might expect.Quote:
2. %d is the wrong format specifier for a double. Read your documentation again to find the correct specifier.
Also consider returning 0.