int main( )
{
int x = 5;
float y = 3.1f;
printf("int x=%d ,float x=%f ,y=%f, y=%d\n", x, x, y, y); //X86
return 0;
}
I think the answer are 5 , 5.0 , 3.1 , 3. but the answer is enter image description here
why?
int main( )
{
int x = 5;
float y = 3.1f;
printf("int x=%d ,float x=%f ,y=%f, y=%d\n", x, x, y, y); //X86
return 0;
}
I think the answer are 5 , 5.0 , 3.1 , 3. but the answer is enter image description here
why?
Copyright © 2021 Jogjafile Inc.
The
%dformat specifier requires an argument of typeint, and%frequires an argument of typedouble(possibly promoted fromfloat). If you pass an argument of the wrong type, the behavior is undefined.The solution: Don't do that.
(The most likely behavior is that the memory or register containing an
intvalue will be displayed as if it were of typedouble, but anything could happen.)For most function calls, passing an
intto a function expecting adoubleargument causes the value to be converted, so42becomes42.0. But sinceprintfis variadic, the expected types are determined by the format string, not the parameter type, so the compiler can't in general know to generate a type conversion.