Auto Initialization of local variables

前端 未结 2 345
醉梦人生
醉梦人生 2020-12-11 10:26

I have the following code snippet.

int j;
printf(\"%d\",j);

As expected, I get a garbage value.

32039491

2条回答
  •  一整个雨季
    2020-12-11 10:57

    It is having indeterminate value. It can be anything.

    Quoting C11 §6.7.9

    If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. [...]

    Automatic local variables, unless initialized explicitly, will contain indeterminate value. In case you try to use a variable while it holds indeterminate value and either

    • does not have the address taken
    • can have trap representation

    the usage will lead to undefined behavior.

提交回复
热议问题