What Happens When I Print an Uninitialized Variable in C++

What will be the value of uninitialized variable?

Technically, the value of an uninitialized non static local variable is Indeterminate[Ref 1].

In short it can be anything. Accessing such a uninitialized variable leads to an Undefined Behavior.[Ref 2]

[Ref 1]

C99 section 6.7.8 Initialization:

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

[Ref 2]

C99 section 3.18 Undefined behavior:

behavior, upon use of a nonportable or erroneous program construct, of erroneous data, or of indeterminately valued objects, for which this International Standard imposes no requirements.

Note: Emphasis mine.

What happens to a declared, uninitialized variable in C? Does it have a value?

Static variables (file scope and function static) are initialized to zero:

int x; // zero
int y = 0; // also zero

void foo() {
static int x; // also zero
}

Non-static variables (local variables) are indeterminate. Reading them prior to assigning a value results in undefined behavior.

void foo() {
int x;
printf("%d", x); // the compiler is free to crash here
}

In practice, they tend to just have some nonsensical value in there initially - some compilers may even put in specific, fixed values to make it obvious when looking in a debugger - but strictly speaking, the compiler is free to do anything from crashing to summoning demons through your nasal passages.

As for why it's undefined behavior instead of simply "undefined/arbitrary value", there are a number of CPU architectures that have additional flag bits in their representation for various types. A modern example would be the Itanium, which has a "Not a Thing" bit in its registers; of course, the C standard drafters were considering some older architectures.

Attempting to work with a value with these flag bits set can result in a CPU exception in an operation that really shouldn't fail (eg, integer addition, or assigning to another variable). And if you go and leave a variable uninitialized, the compiler might pick up some random garbage with these flag bits set - meaning touching that uninitialized variable may be deadly.

How does C compilers handle using an uninitialized variable?

This is nothing to do with your compiler, although different compilers may have side-effects that affect the value in a way that appears to be consistent. But regardless, your program has undefined behavior. You did not initialize the value, and so your program's behavior cannot be predicted.

When you declare the variable x, the compiler only records your intent to store a value large enough to hold int. Now, it doesn't matter where it decides to put this. It might push it onto your stack in memory, or it might choose to keep a CPU register available without using memory at all.

So, when you ask for the value of x, there is absolutely no way to know what you will get. Most likely you will get whatever dirty value existed previously at whatever location the compiler determined it would set aside. But equally, the compiler could have utterly failed to even decide where x lives, since it was never used, and then do something awful that leads to a program crash, or just about anything else.

The good news is that you don't have to care about what could happen or why or under what conditions. As a programmer, all you need to care about is that the behavior is undefined. End of story.

And how to fix this? Easy. Give x a value before you ever try to read its value.

Why is uninitialized variable not printing garbage value?

My GCC compiler generated following warning.

In function ‘bar’:
source_file.c:9:9: warning: unused variable ‘b’ [-Wunused-variable]
int b = 50;
^
source_file.c: In function ‘foo’:
source_file.c:5:5: warning: ‘c’ is used uninitialized in this function [-Wuninitialized]
printf("%d\n", c);

And it's print garbage value. If you use uninitialized variable in C. it is invoked undefined behaviour.

C11 6.3.2.1 (p2):

[...] If the lvalue designates an object of automatic storage duration
that could have been declared with the register storage class (never
had its address taken), and that object is uninitialized (not declared
with an initializer and no assignment to it has been performed prior
to use), the behavior is undefined.

Why uninitialized variable print a strange negative value?

What you're doing (reading the value of an uninitialised variable) is undefined behaviour; anything can happen, from it appearing to work, to printing random values, to crashing, to buying pizza with your credit card.



Related Topics



Leave a reply



Submit