-7

As is known to us, using an uninitialized variable is an undefined behavior in C or C++. For example, if we have:

int x;
printf("%d", x);

It will produce a number that we can't predict.

My question is that how C compilers handle using an uninitialized variable.

Jason
  • 36,170
  • 5
  • 26
  • 60
Peiqin
  • 79
  • 1
  • 7
  • 1
    If you don't initialise it, the compilser doesn't initialise it either, and it takes whatever value it contained previously. That's why you should initialise the variables. – AntonH May 08 '17 at 04:16
  • 2
    Printing an uninitialized local variable invokes Undefined Behavior. The results can be anything! – Spikatrix May 08 '17 at 04:18
  • 1
    See [What is undefined behaviour?](http://stackoverflow.com/a/4105123/1505939) – M.M May 08 '17 at 04:31
  • 2
    This might give you some more info: https://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior-in-c – Retired Ninja May 08 '17 at 04:32
  • Does this answer your question? [Consistency of undefined behavior for a fixed compiler](https://stackoverflow.com/questions/74155360/consistency-of-undefined-behavior-for-a-fixed-compiler) – Jason Jan 04 '23 at 14:39

2 Answers2

4

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.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • But why the output is 2? As a comment to my question says, the compiler takes whatever value it contained previously. What kind of behavior makes it take 2? – Peiqin May 08 '17 at 04:28
  • @dapao Undefined behaviour. – M.M May 08 '17 at 04:30
  • 1
    @dapao Compiler declares `x` at certain memory location. As you have not initialized it, that memory location may contain anything... Anything left over from other code's execution etc. When you read that memory location, You read old value. Earlier program must have written 2 there, hence you are getting 2. – Swanand May 08 '17 at 04:31
  • Hahaha "why" indeed.... I have edited my answer. I hope this will provide more context. But you will be disappointed to learn that it doesn't matter why, and there is no standard definition of how or why you get that value. If you care about _why_, you're kinda missing the point. – paddy May 08 '17 at 04:32
1

The result doesn't seems strange at all!!!!

But it is common if try to print a value of a variable which wasn't earlier initialized.It is usually known to be a garbage value .Generally c or c++ compiler takes garbage value which can be any (positive or negative) primarily non zero.(In your problem you get a 2 )

The basic way to get rid of garbage is to Initialize your variables.Just merely using declared variables without initializing them ,generates mere garbage. Always remember nothing declared is empty they must have a garbage associated with it :)

Mathews Sunny
  • 1,796
  • 7
  • 21
  • 31
  • For this case specifically, it does not have to be a "garbage value". The variable has local scope but does not have its address taken. This is always undefined behavior and might as well lead to a crash as to strange output. – Lundin May 08 '17 at 06:41