Search code examples
cinitializationdeclaration

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


If in C I write:

int num;

Before I assign anything to num, is the value of num indeterminate?


Solution

  • 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 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.