title: Memory allocation of functions and variables in C
body: <p>Depended of the version of the C compiler and compiler flags it is possible to initialize variables on any place in your functions (As far as I am aware).</p>
  
  <p>I'm used to put it all the variables at the top of the function, but the discussion started about the memory use of the variables if defined in any other place in the function.</p>
  
  <p>Below I have written 2 short examples, and I wondered if anyone could explain me (or verify) how the memory gets allocated. </p>
  
  <p>Example 1: Variable y is defined after a possible return statement, there is a possibility this variable won't be used for that reason, as far as I'm aware this doesn't matter and the code (memory allocation) would be the same if the variable was placed at the top of the function. Is this correct?</p>
  
  <p>Example 2: Variable x is initialized in a loop, meaning that the scope of this variable is only within this loop, but what about the memory use of this variable? Would it be any different if placed on the top of the functions? Or just initialized on the stack at the function call?</p>
  
  <p>Edit: To conclude a main question:
  Does reducing the scope of the variable or change the location of the first use (so anywhere else instead of top) have any effects on the memory use?</p>
  
  <p>Code example 1</p>
  
  <pre><code>static void Function(void){
   uint8_t x = 0;
  
   //code changing x
   if(x == 2)
   {
    return;
   }
  
   uint8_t y  = 0;    
   //more code changing y
  }
  </code></pre>
  
  <p>Code example 2</p>
  
  <pre><code>static void LoopFunction(void){
   uint8_t i = 0;
  
   for(i =0; i &lt; 100; i ++)
   {
    uint8_t x = i;
    // do some calculations
    uartTxLine("%d", x); 
   }
  
   //more code
  }
  </code></pre>