What happens to uninitialized variables in C/C++?

From "C++ Primer" by Lippman,

When we define a variable, we should give it an initial value unless we are certain that the initial value will be overwritten before the variable is used for any other purpose. If we cannot guarantee that the variable will be reset before being read, we should initialize it.

Q.1) What happens if an uninitialized variable is used in say an operation? Will it crash/ will the code fail to compile?

I searched the internet for answer to the same but there were differing ‘claims’. Hence the following questions,

Q.2) Will C and C++ standards differ in how they treat an uninitialized variable?

Q.3) Regarding similar queries, how and where can I find an ‘official’ answer? Is it practical for an amateur to look up the C and C++ standards?

>Solution :

Q.1) What happens if an uninitialized variable is used in say an operation? Will it crash/ will the code fail to compile?

Many compilers will warn you about code that improperly uses the value of an uninitialized variable. Many compilers have an option that says "treat warnings as errors". So depending on the compiler you’re using and the option flags you invoke it with, the code might fail to compile, although we can’t say that it will fail to compile.

If the code does compile, and you try to run it, it’s obviously impossible to predict what will happen. In most cases the variable will start out containing an "indeterminate" value. Whether that indeterminate value will cause your program to work correctly, or work incorrectly, or crash, is anyone’s guess. If the variable is an integer and you try to do some math on it, you’ll probably just get a weird answer. But if the variable is a pointer and you try to indirect on it, you’re quite likely to get a crash.

It’s often said that uninitialized local variables start out containing "random garbage", but that can be misleading, as evidenced by the number of people who post questions here pointing out that, in their program where they tried it, the value wasn’t random, but was always 0 or was always the same. So I like to say that uninitialized local variables never start out holding what you expect. If you expected them to be random, you’ll find that (at least on any given day) they’re quite repeatable and predictable. But if you expect them to be predictable (and, god help you, if you write code that depends on it), then by jingo, you’ll find that they’re quite random.

Whether use of an uninitialized variable makes your program formally undefined turns out to be a complicated question. But you might as well assume that it does, because it’s a case you want to avoid just as assiduously as you avoid any other dangerous, undefined behavior.

See this old question and this other old question for more (much more!) information on the fine distinction between undefined and indeterminate behavior in this case.

Q.2) Will C and C++ standards differ in how they treat an uninitialized variable?

They might. If you read the C standard carefully, you’ll find out that not all uses of uninitialized local variables are undefined. (Some are merely "indeterminate".) But the passages quoted from the C++ standards by other answers here make it sound like it’s undefined all the time. Again, for practical purposes, the question probably doesn’t matter, because as I said, you’ll want to avoid it no matter what.

Q.3) Regarding similar queries, how and where can I find an ‘official’ answer? Is it practical for an amateur to look up the C and C++ standards?

It is not easy to obtain copies of the standards (let aloe official ones, which often cost money), and the standards can be difficult to read and to properly interpret, but yes, given effort, anyone can obtain and read the standards.

Leave a Reply