Result of Printf (“%d”, &a)

后端 未结 3 1259
长情又很酷
长情又很酷 2020-12-22 14:17

I have a code in C .

    int a; 
    a =10;
    printf (\"%d\", &a); 

I want to know if some garbage will be printed or error msg will

3条回答
  •  再見小時候
    2020-12-22 14:55

    This is undefined behaviour.

    If you are new to C, this may be a surprise. However the C specification defines the behaviour of certain programs (called "correct programs"). If you do not follow the rules in the specification, then your program is not correct, and literally anything may happen. The above link goes into more detail.

    Your program doesn't follow the rules because the %d format specifier for printf must have a corresponding argument of type int (after the default argument promotions), but instead you pass an argument of type int *.

    Since it is undefined behaviour, the output is meaningless and it is generally not worthwhile to investigate.

提交回复
热议问题