enter image description hereWhy in C, when I print a double type variable with %lf it shows 0.000000…but if I use just %f it shows the desire result? Moreover, when we take an input we use %lf for double type variable. Why i am getting 0.0000 instead of 3.0000 ?
Asked
Active
Viewed 1.5k times
1
-
3Please show a [MCVE]. – Jabberwocky Nov 18 '16 at 08:55
-
4This is probably already answered in: http://stackoverflow.com/questions/19952200/scanf-printf-double-variable-c – Daniel Heinrich Nov 18 '16 at 08:57
-
1The answer of the question http://stackoverflow.com/questions/19952200/scanf-printf-double-variable-c is not able to give a perfect definition of my shared picture. Why it is giving 0.0000 instead of 3.0000 – Nov 18 '16 at 12:39
-
Hi, if you look at Artem Shinkarov's answer in the link : http://stackoverflow.com/questions/19952200/scanf-printf-double-variable-c, you will get your answer... – Aarjavee S. Kamdar Jan 30 '17 at 17:11
-
I ran your code on codechef online compiler and it gave me the desired answer : 3.000000, so, it maybe so, that your compiler doesn't allow "lf"... – Aarjavee S. Kamdar Jan 30 '17 at 17:12
-
try running your same code in this ide : https://www.codechef.com/ide – Aarjavee S. Kamdar Jan 30 '17 at 17:25
1 Answers
2
for printf %f and %lf can be used interchangeably for floats and double.
for scanf %f formats float
for scanf %lf formats double
Reason is that scanf take pointer(address of variable) as the input in both cases. Whereas in case of printf which takes actual variables (float or double) as the input parameters which are convertible to each other(type casting)

hrishi
- 443
- 2
- 12