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..![]()
This is a discussion on divide long by double within the C Programming forums, part of the General Programming Boards category; HI,i was trying to do the following.I tried but cant figure out my error.I want the output to be 123.456 ...
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.
Last edited by iMalc; 10-30-2007 at 11:49 PM.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
It's %f, not %lf, which you might expect.2. %d is the wrong format specifier for a double. Read your documentation again to find the correct specifier.
Also consider returning 0.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.