admin管理员组

文章数量:1406052

int num1 =5, num2 = 5;
num1--;
num2--;

int num3 = 7;

printf("%f\n",num1);
printf("%d\n",num2);
printf("%f",num3);

It returns:

0.000000 \<-WHY?

4

0.000000 \<-WHY?

Process returned 0 (0x0)   execution time : 0.032 s

Press any key to continue.

I tried to cast the print like this and it works now :

printf("%f\n",(float)num1);

But, why before it doesn't print the number correctly?

int num1 =5, num2 = 5;
num1--;
num2--;

int num3 = 7;

printf("%f\n",num1);
printf("%d\n",num2);
printf("%f",num3);

It returns:

0.000000 \<-WHY?

4

0.000000 \<-WHY?

Process returned 0 (0x0)   execution time : 0.032 s

Press any key to continue.

I tried to cast the print like this and it works now :

printf("%f\n",(float)num1);

But, why before it doesn't print the number correctly?

Share Improve this question edited Mar 6 at 21:29 dbush 227k25 gold badges247 silver badges320 bronze badges asked Mar 6 at 16:38 JeJe 1773 bronze badges 10
  • 5 Because printing int with format of float is Undefined Behavior. – dimich Commented Mar 6 at 16:43
  • 2 If you print in scientific notation with "%1.4ef\n" you will probably see that it is not actually 0, it is just too small to display in decimal. – François Andrieux Commented Mar 6 at 16:49
  • 2 In plain words: the format specifier tells to the printf() which type of data is passed to the function. If you tell it to expect a float value the function will interpret the passed value as a a float, that incidentally could correspond to a malformed float type, that could look like a 0.00 or 123e-23 or anything else. To print a number in another format first convert it, i.e. using casting: printf("%f",(float)num3). The (float) part prepended to the value to convert is called casting. Google it for more info. – Frankie_C Commented Mar 6 at 16:53
  • 1 You need to get your wording right. printf does not return 0.00. It prints it. It returns the number of printed characters but you do not look at the return value at all. Printing some output is a totally different thing than returning something. Most functions return something without ever printing anything. – Gerhardh Commented Mar 7 at 7:39
  • 1 It looks like you fot to enable a good set of warnings. For GCC, I recommend -Wall -Wextra as a minimum; consider also -Wpedantic -Warray-bounds for identifying some other common mistakes. – Toby Speight Commented Mar 7 at 15:16
 |  Show 5 more comments

2 Answers 2

Reset to default 8

Each different format specifier for printf expects a particular type. Failure to supply the correct type triggers undefined behavior in your code.

This is related to the fact that printf is a variadic function, meaning it can take any number of different arguments, so it needs to be told (via the format string) what the types of those arguments are.

just pointing out what it means to be undefined behavior, and what can happen.

the caller allocates stack space for arguments. float goes promotion to double as per variadic arguments Default argument promotions, whereas int doesn't.

the caller allocates 4 bytes of stack space whereas the callee expects 8 bytes of stack space allocated, what can be in the 4 bytes that weren't properly allocated ? maybe some leftovers from whatever was there, or maybe the application will crash instantly.

trying it online and calling the function twice, once it prints 0.0000 and the other is 1.0000, the result is completely undefined, anything can happen, don't try to explain it.

online demo


Since c++ is was tagged, we have a type-safe alternative in c++23 namely std::print

std::print("{}",num3);

The compiler will deduce the needed conversions. For lower c++ versions we have fmt library which implement it, or you can just use C++ streams, which has known disadvantages sometimes.

本文标签: Why in C when I print a int with the format of float it returns 000Stack Overflow