5
#include <iostream>

using namespace std;

int weirdVariable = weirdVariable  + 1;
int main() {
  cout<< weirdVariable ;
  return weirdVariable ;
}

I was just wondering how this un-initialized variable is not returning error and returning 1.So my question is, how/why is it returning the value "1". Is this program logically valid? Or is it some flaw?

kotAPI
  • 1,073
  • 2
  • 13
  • 37

2 Answers2

5

It's not uninitialized. Variables with static storage duration (like a global variable) are first zero-initialized before any further initialization. So weirdVariable ends up with the value 1.

§3.6.2 [basic.start.init] Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.

If you were to declare wierdVariable as local to main, it would be uninitialized. This will give you undefined behaviour because performing lvalue-to-rvalue conversion (read: using the value of) on an uninitialized object gives undefined behaviour.

§4.1 [conv.lval] If the object to which the glvalue refers is [...] uninitialized, a program that necessitates this conversion has undefined behavior.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • But sometimes I get errors from compilers saying that I haven't initialized certain variables, so those variables weren't static? – kotAPI Jan 24 '14 at 09:20
  • If they were local pointers, you need to initialize them for the compiler to know what to assign to them initially – Marco A. Jan 24 '14 at 09:21
  • 1
    @kotAPI Right, they weren't static. – Joseph Mansfield Jan 24 '14 at 09:22
  • @DavidKernin I'm using a GNU GCC 4.7.2 version. Maybe the results vary in different compliers. – kotAPI Jan 24 '14 at 09:24
  • 2
    @kotAPI Undefined behaviour doesn't mean it's invalid C++. It's perfectly valid C++, but when you run your executable, anything could happen at all. A good compiler is able to warn you that you might be encountering this situation (as I think GCC, clang, and VS all do). – Joseph Mansfield Jan 24 '14 at 09:27
  • @Joseph Mansfield - Guess, UB is what Stroustrup meant saying - "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off". – SChepurin Jan 24 '14 at 09:49
4

Static and global variables are initialized to 0 by default so it's perfectly normal

The C standard ISO/IEC 9899:1999 a.k.a. C99 (and C++) standards say this must be so. See item 10 in section 6.7.8 ("Initialization") of WG14 N1256 for the exact text (https://stackoverflow.com/a/1294780/1938163)

By the way: it is good practice to initialize static variables, also just to render the code more readable!

static int myvar = 0;

Another drawback of not initializing them: if a compiler doesn't follow the standard, you might get in trouble

With regard to local variables that are both NOT static and NOT global, well, you might skip their initialization but that would yield undefined behavior. Don't really rely on it.

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • So you mean to say that I've declared weirdVariable as static by making it global? But the same happens when I've declared it in the main method.. – kotAPI Jan 24 '14 at 09:18
  • I forgot to add that both static and global variables are initialized as so. Fixed – Marco A. Jan 24 '14 at 09:19
  • @kotAPI In that case, you have undefined behaviour. – Joseph Mansfield Jan 24 '14 at 09:19
  • 1
    I agree, and it's terribly compiler-dependent so you can't rely on it. – Marco A. Jan 24 '14 at 09:20
  • Using a GNU Gcc 4.7.2 compiler. Depends on the compiler perhaps, I get a lot of errors when I don't initialize variables and try to increment them. I think it's a good practice to initialize a variable than to let it be 0 and have a flawed program. Don't you think? – kotAPI Jan 24 '14 at 09:23
  • non-static variables need to be initialized otherwise you might get undefined behavior. – Marco A. Jan 24 '14 at 09:24
  • 1
    @DavidKernin Yeah, especially in games. You don't want a character to spawn with 0 health rather than 100. :D – kotAPI Jan 24 '14 at 09:28
  • 1
    or -100.. just to say :) – Marco A. Jan 24 '14 at 09:29