Uninitialized variable in C

前端 未结 5 1095
滥情空心
滥情空心 2020-12-07 03:15

I\'m a little bit confused. As far as I know, if you declare an int in C, without initializing it, for e.g: int x;

so its value is indeterminate. So if

5条回答
  •  余生分开走
    2020-12-07 04:01

    Reading the value of an uninitialized variable leads to undefined behavior. And undefined behavior means that it can crash. It doesn't mean it will or it is obliged to crash.

    An uninitialized variable has unspecified value - it's just unknown what its value is. So in practice, with any sane implementation, this kind of code will presumably never crash. There's a valid memory address backing the variable, it has some garbage content, printf() reads it without problem, interprets it as an integer and prints it, that's all.

提交回复
热议问题