Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Compile your code with <code>gcc -Wall filename.c</code> You will see these warnings. </p> <pre><code>In function 'B': 11:9: warning: variable 'a' set but not used [-Wunused-but-set-variable] In function 'A': 6:11: warning: 'a' is used uninitialized in this function [-Wuninitialized] </code></pre> <p>In c Printing uninitialized variable Leads to Undefined behavior.</p> <p>Section 6.7.8 Initialization of C99 standard says</p> <blockquote> <p>If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then: </p> </blockquote> <pre><code>— if it has pointer type, it is initialized to a null pointer; — if it has arithmetic type, it is initialized to (positive or unsigned) zero; — if it is an aggregate, every member is initialized (recursively) according to these rules; — if it is a union, the first named member is initialized (recursively) according to these rules. </code></pre> <p><strong>Edit1</strong> </p> <p>As @Jonathon Reinhart If you disable optimization by Using <code>-O</code> flag <code>gcc-O0</code> then you might get output 5. </p> <p>But this is not at all good idea , never ever use this in production code. </p> <p><code>-Wuninitialized</code> this is one of the valuable warning You should consider this one You should not either disable or skip this warning that leads huge damage in production like causing crashes in while running daemons. </p> <hr> <p><strong>Edit2</strong></p> <p><a href="http://www.slideshare.net/olvemaudal/deep-c" rel="noreferrer">Deep C</a> slides explained Why result is 5/garbage.Adding this information from those slides with minor modifications to make this answer little more effective. </p> <blockquote> <p>Case 1: without optimization </p> </blockquote> <pre><code>$ gcc -O0 file.c &amp;&amp; ./a.out 5 </code></pre> <p>Perhaps this compiler has a pool of named variables that it reuses. Eg variable a was used and released in<code> B()</code>, then when <code>A()</code> needs an integer names <code>a</code> it will get the variable will get the same memory location. If you rename the variable in <code>B()</code> to, say <code>b</code>, then I don’t think you will get <code>5</code>.</p> <blockquote> <p>Case 2: with optimization </p> </blockquote> <p>A lot of things might happen when the optimizer kicks in. In this case I would guess that the call to <code>B()</code> can be skipped as it does not have any side effects. Also, I would not be surprised if the <code>A()</code> is inlined in <code>main()</code>, ie no function call. (But since <code>A ()</code> has linker visibility the object code for the function must still be created just in case another object file wants to link with the function). Anyway, I suspect the value printed will be something else if you optimize the code.</p> <pre><code>gcc -O file.c &amp;&amp; ./a.out 1606415608 </code></pre> <p>Garbage!</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. 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