Behavior of uninitialized local char?

前端 未结 3 1680
挽巷
挽巷 2021-01-19 06:31

If you have lets say a local int that is uninitialized, then its gets an undefined value but if you have a local char variable should that not have

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-19 06:50

    If you have lets say a local int that is uninitialized, then its gets an undefined value

    No, that isn't the right way to think about it. Your local variable doesn't get an undefined value, it gets no value whatsoever. Subsequently querying such an uninitialized variable invokes undefined behavior.

    Your program won't necessarily print "0". It won't necessarily print any number, or even anything at all. Granted, on typical computers, using typical compilers, your program will print some number, but within the scope of the C++ language, we can't make any prediction about what your program will do or not do.

提交回复
热议问题