#include<stdio.h>
int main()
{
float a=5;
printf("%d",a);
return 0;
}
This gives the output:
0
Why is the output zero?
#include<stdio.h>
int main()
{
float a=5;
printf("%d",a);
return 0;
}
This gives the output:
0
Why is the output zero?
You have to use a different formatting string, just have a look at http://www.cplusplus.com/reference/clibrary/cstdio/printf/
printf("%f", a);
It doesn't print 5 because the compiler does not know to automatically cast to an integer. You need to do (int)a
yourself.
That is to say,
#include<stdio.h>
void main()
{
float a=5;
printf("%d",(int)a);
}
correctly outputs 5.
Compare that program with
#include<stdio.h>
void print_int(int x)
{
printf("%d\n", x);
}
void main()
{
float a=5;
print_int(a);
}
where the compiler directly knows to cast the float to an int, due to the declaration of print_int
.
%d
format specifier can only be used with values of type int
. You are passing a double
(which float
will be implicitly converted to). The resultant behavior is undefined. There no answer to "why it prints 0?" question. Anything can be printed. In fact, anything can happen.
P.S.
int main
, not void main
.conio.h
in standard C.You'll want to use %f for printing a float value.
eg
float a=5;
printf("%f",a);
You should either cast it to an int to use %d, or use a format string to display the float with no decimal precision:
void main() {
float a=5;
printf("%d",(int)a); // This casts to int, which will make this work
printf("%.0f",a); // This displays with no decimal precision
}
You need to use %f
instead of %d
- %d
is just for integers while %f
is for floating point:
#include<stdio.h>
#include<conio.h>
void main()
{
float a=5;
printf("%f",a);
}
Because printf
doesn't have a prototype for its arguments beyond the formatting string, your float
argument is promoted to double
, per §6.5.2.2 paragraph 6 of the standard. The value 5.0 as a double
has the representation:
0x4014000000000000
so that gets stored to memory in the location that printf
will look for its first argument.
When printf
processes your formatting string and encounters %d, it loads an int
from that memory address. Because you are on a little-endian machine on which int
is 32 bits (or smaller, depending on the actual system), it interprets the low bits of the double you passed as the integer to be printed. Since the low word of the representation of 5.0 is exactly zero, it prints 0
.
As the other people said, you need to use %f
in the format string or convert a
to an int.
But I want to point out that your compiler, probably, knows about printf()
's format string and can tell you you're using it wrong. My compiler, with the appropriate invocation (-Wall
includes -Wformat
), says this:
$ /usr/bin/gcc -Wformat tmp.c
tmp.c: In function ‘main’:
tmp.c:4: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
$ /usr/bin/gcc -Wall tmp.c
tmp.c: In function ‘main’:
tmp.c:4: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
$
Oh, and one more thing: you should include '\n' in the printf()
to ensure the output is sent to the output device.
printf("%d\n", a);
/* ^^ */
or use fflush(stdout);
after the printf()
.
A brilliant answer from stefen cannon(7th above). The result matches explanation when trying with a different number like 1.1. This is really informative.