strange behavior of scanf for short int

后端 未结 5 2022
余生分开走
余生分开走 2020-12-11 10:25

the code is as follows:

#include 
main()
{
    int m=123;
    int n = 1234;
    short int a;
    a=~0;
          


        
5条回答
  •  伪装坚强ぢ
    2020-12-11 11:09

    You're invoking Undefined Behavior when passing a pointer to a non-int to scanf's %d.

    Likely, the compiler introduces padding bytes for alignment purposes and the values get stored in the padding bytes and not the "useful" bytes.

    However, the compiler is free to do anything from raise a segfault / access violation to invoke nasal demons.

提交回复
热议问题