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 | Show 5 more comments2 Answers
Reset to default 8Each 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
版权声明:本文标题:Why in C when I print a int with the format of float it returns 0.00? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744961924a2634708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
"%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:49printf()
which type of data is passed to the function. If you tell it to expect afloat
value the function will interpret the passed value as a afloat
, that incidentally could correspond to a malformedfloat
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:53printf
does not return0.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-Wall -Wextra
as a minimum; consider also-Wpedantic -Warray-bounds
for identifying some other common mistakes. – Toby Speight Commented Mar 7 at 15:16