Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Static variables (file scope and function static) are initialized to zero:</p> <pre><code>int x; // zero int y = 0; // also zero void foo() { static int x; // also zero } </code></pre> <p>Non-static variables (local variables) are <em>indeterminate</em>. Reading them prior to assigning a value results in undefined behavior.</p> <pre><code>void foo() { int x; printf("%d", x); // the compiler is free to crash here } </code></pre> <p>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 <a href="http://catb.org/jargon/html/N/nasal-demons.html" rel="noreferrer">demons through your nasal passages</a>.</p> <p>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 <a href="http://blogs.msdn.com/oldnewthing/archive/2004/01/19/60162.aspx" rel="noreferrer">the Itanium, which has a "Not a Thing" bit in its registers</a>; of course, the C standard drafters were considering some older architectures.</p> <p>Attempting to work with a value with these flag bits set can result in a CPU exception in an operation that <em>really</em> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload