What will be its output and why?

后端 未结 3 1941
庸人自扰
庸人自扰 2021-01-29 16:44

I was going through scope rules questions and all and then got a code snippet, below:

#include 
int main()
{
  int x = 1, y = 2, z = 3;
  printf(\         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-29 17:07

    @mohit-jain is correct.

    Using the wrong format specifier yields incorrect parameter interpretation on the stack, resulting in undefined and compiler specific behavior.

    Note that on a modern compiler, like gcc or clang, it will complain that your format specification is wrong:

    $ clang test.c 
    test.c:12:54: warning: format specifies type 'int' but the argument has type 'float'
          [-Wformat]
                 printf(" x = %d, y = %d, z = %d \n", x, y, z);
                                      ~~                 ^
                                      %f
    1 warning generated.
    

提交回复
热议问题