Unexpected output of printf

前端 未结 4 1966
有刺的猬
有刺的猬 2020-11-30 15:15
int a=5;
float b=3.5;
printf(\"%d\",b);
printf(\"\\n%f\",a);

Can anyone please tell me why this code is showing unexpected output (garbage\\n3.5)

相关标签:
4条回答
  • 2020-11-30 16:06

    You are passing a float for the %d format string, but printf is expecting an int.

    printf is a variable argument list function: printf(const char *format_string,...);

    This means that the rest of the arguments after the format string can be of any type and number, and the compiler doesn't know what they should be. It is up to the programmer to provide arguments that are of the type that printf is expecting. What printf expects is determined by the format string. When you give a %d in your format string, the printf function is going to expect that the next argument is an int. Since you are passing a float, something strange is probably going to happen. For example, those bytes which make up the floating point number might be treated as if they were an int, which won't give you any meaningful value.

    Actually it is a little more complicated than that. There are special rules about how parameters are passed to variable argument functions. One of the rules is that float values are passed as doubles. Which means that your float value is first converted to a double before being passed to printf. Most likely a double is eight bytes on your platform, while an int is only four. So the printf function could be treating the first four bytes of your double value as an int.

    What is perhaps even worse is that on the next line, an int is being passed where a double is expected. Which means that the printf function could treat the first four bytes of your int as part of the double, and then read four more bytes that weren't even part of your arguments.

    The details of what actually happens is platform-specific. The language simply states that you shouldn't pass the wrong type of arguments and makes no guarantees about will happen if you do.

    0 讨论(0)
  • 2020-11-30 16:06

    Because the a variable is a int type, while your are specifying a format string for a float type, and vice versa for the variable b.

    Note :

    %d is for integer type

    %f is for float type

    You should use :

    int a=5;
    float b=3.5;
    printf("%f",b);
    printf("\n%d",a);
    
    0 讨论(0)
  • 2020-11-30 16:17

    Format string incorrect error, according to your declarations of a and b:

    printf("%d",b); <-- "b is float"     Wrong!
    printf("\n%f",a); <-- "a is an int"   Wrong! -- Undefined behavior 
    

    should be:

    printf("%f",b);
    printf("\n%d",a);
    

    Q :- Why you getting that output?

    It's due to undefined behavior of your code:

    From INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x

    7.16.1 Variable argument list access macros

    (page 270)

    7.16.1.1 The va_arg macro

    [...] If there is no actual next argument, or if type is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), the behavior is undefined, except for the following cases:
    — one type is a signed integer type, the other type is the corresponding unsigned integer type, and the value is representable in both types;
    — one type is pointer to void and the other is a pointer to a character type

    0 讨论(0)
  • 2020-11-30 16:18
    int main()
    {
    
    int a=5;
    float b=3.5;
    printf("%f",b); //Use %f
    printf("\n%d",a); // Use %d
    
    }
    

    Refer: printf

    wrong format specifier to printf leads undefined behavior in most cases.

    However, since printf is a variadic function, and the arguments to variadic functions undergo the default argument cast. Like, a char is casted to an int.

    0 讨论(0)
提交回复
热议问题