views:

80

answers:

2
+1  Q: 

input-output in C

double d;
scanf("%f", &d);
printf("%f", d);

result:

input: 10.3

output: 0.00000

Why? i think output should be 10.3 visual studio 2008.

+5  A: 

For scanf(), %f is for a float. For double, you need %lf. So,

#include <stdio.h>
main() {
    double d; 
    scanf("%lf", &d); 
    printf("%f\n", d);
}

with input 10.3 produces 10.300000.

Ramashalanka
+1 for %4.1lf format string Sergey could view this: http://www.cplusplus.com/reference/clibrary/cstdio/printf/
stacker
`%lf` is needed for `scanf()`, but for `printf()`, `%f` means `double` (and works with `float` too, because `float` is promoted to `double` in the variable portion of the argument list). `%lf` is meaningless to `printf()`.
caf
printf is a vararg function, so argument promotion need not apply - compiler does not know argument types beyond format string. Said that, %lf is needed.
el.pescado
el.pescado: There are a particular set of argument promotions that are always applied to arguments in the variable portion of the argument list (they are the same as those applied to the arguments of a function declared without a prototype).
caf
+1  A: 

Try replacing %f with %lf. %f is used when dealing with float, not double. (or alternately, you could make d a float).

MAK
thanks, it works
Sergey