2

Possible Duplicate:
What is printf’s behaviour when printing an int as float?

int main()
{
 int x=4;
 int y=987634;
 printf("%f %f",x,y);
}

On compiling this code i get an output as 0.000000 0.000000. Shouldn't there be a type promotion of x and y to floating point numbers? Shouldn't the O/P be 4.000000 and 987634.000000? Can anyone help me with this. Thanx in Advance.

Community
  • 1
  • 1
Vignesh_dino
  • 313
  • 2
  • 6
  • 1
    I can't quite find an *exact* duplicate, but this type of question has been asked [many](http://stackoverflow.com/questions/2398791/how-is-conversion-of-float-double-to-int-handled-in-printf) [times](http://stackoverflow.com/questions/1632080/print-the-float-value-in-integer-in-c-language) [before](http://stackoverflow.com/questions/8203754/what-is-printfs-behaviour-when-printing-an-int-as-float). – Adam Rosenfield Nov 27 '12 at 16:51
  • I'd be pleased to see anything of this type. Can u post the links. @AdamRosenfield – Vignesh_dino Nov 27 '12 at 16:52
  • But why is it always Zero? Shouldn't the result be the integer equivalent of the set bits? – Vignesh_dino Nov 27 '12 at 17:24

5 Answers5

5

Conversions happen to arguments to functions with a prototype which includes the specific parameters. The prototype for printf() does not include the specific parameters after the first one

int printf(const char *format, ...);

so, no arguments after the 1st one get automatically converted except as defined by "default argument conversions" (basically any integer type with a rank lower than int to int and any floating-point type with a rank lower than double to double (thank you, Pascal Cuoq)). You need to convert them explicitly yourself with a cast operation

    printf("%f %f\n", (double)x, (double)y);

Ohhh ... and you really, really, really should include the header that has the prototype in question (under penalty of Undefined Behaviour)

#include <stdio.h>
pmg
  • 106,608
  • 13
  • 126
  • 198
  • 1
    Just a note regarding “no arguments after the 1st one get converted automatically”: there are promotions applied to the arguments in the ellipsis. They are called “default argument promotions” and they do not include promotion from `int` to `double` (C99 6.5.2.2:7) – Pascal Cuoq Nov 28 '12 at 15:43
1

The compiler has no idea that your printf format string is going to interpret the arguments as floats. It passes them straight through as ints.

Because printf is a varargs function, it's really up to you to pass parameters that make sense.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
0

Try printf("%i %i",x,y); to print integers as 4 987634. For printf formatting details see http://www.cplusplus.com/reference/cstdio/printf/.

rknuus
  • 118
  • 1
  • 9
0

ints and floats are stored differently in memory, but your compiler does not know that you want floats. You need to convert them explicitly.

printf("%f %f",(float)x,(float)y);
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • 2
    Since variadic arguments are default-promoted, that does the right thing, but it is somewhat misleading. You should directly cast to `double`. – Daniel Fischer Nov 27 '12 at 16:57
0

Variadic functions (printf() is one) aren't type checked because variadic signatures don't contain any type information. Thus, there's no implicit type casting. You have to do it manually:

printf("%f %f", (double)x, (double)y);
Nikos C.
  • 50,738
  • 9
  • 71
  • 96