I use the codeblock .
when the code is:
printf("%.1f", 1);
The program can run,and the output is 0.0
. I want to know why. `
Change it to:
printf("%.1f", 1.0);
f
conversion specifier requires an argument of type double
but you are passing an int
value ( 1
is of type int
). Passing an argument of the wrong type to printf
invokes undefined behavior.
Using wrong format specifier invokes undefined behavior . You may get either expected or unexpected result. Use %d
instead as the argument passed to printf
is int
type or change 1
to 1.0
if you are using %f
.
If a conversion specification is invalid, the behavior is undefined. 282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
The format is incorrect , you should give a float or a double as the second argument to the printf function. The gcc compiler also gives a warning on such mistakes by programmers.
printf("%.1f",1.23);
Output:
1.2
Be carefull with these kind of mistakes. Good luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.