tags:

views:

753

answers:

9
#include<stdio.h>
int main()
{
    float a=5;
    printf("%d",a);
    return 0;
}

This gives the output:

0

Why is the output zero?

+4  A: 

You have to use a different formatting string, just have a look at http://www.cplusplus.com/reference/clibrary/cstdio/printf/

printf("%f", a);

Gabb0
+9  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.

Mark Rushakoff
Specifically, it doesn't know to automatically cast to an integer in a variadic function, but that's rather hard to explain to the beginner, particularly since it sure looks like the C compiler should know the types involved.
David Thornley
Besides the variadic-ness, knowing what type to cast would require the compiler to parse the string passed to printf and deduce what type a is supposed to be. GCC can easily handle a constant "%d\n" and warn you when passed `-Wall`, but good luck when the string isn't hard-coded into the program!
Mark Rushakoff
+7  A: 

%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.

  1. That's int main, not void main.
  2. There's no such header as conio.h in standard C.
AndreyT
+1 for pointing out that printf() is passed a double. I fixed the code to remove the superfluous and irrelevant `#include <conio.h>`.
Jonathan Leffler
+3  A: 

You'll want to use %f for printing a float value.

eg

float a=5;
printf("%f",a);
Tom
+3  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
}
Reed Copsey
+3  A: 

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);
}
Stephen Doyle
+9  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.

Stephen Canon
+1 point for describing mechanizm in detail
PiotrK
+1  A: 

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().

pmg
A: 

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.

mohan