title: Are C stack variables stored in reverse?
body: <p>I'm trying to understand how C allocates memory on stack. I always thought variables on stack could be depicted like structs member variables, they occupy successive, contiguous bytes block within the Stack. To help illustrate this issue I found somewhere, I created this small program which reproduced the phenomenon.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  void function(int  *i) {
      int *_prev_int =  (int *) ((long unsigned int) i -  sizeof(int))  ;
      printf("%d\n", *_prev_int );    
  }
  
  void main(void) 
  {
      int x = 152;
      int y = 234;
      function(&amp;y);
  }
  </code></pre>
  
  <p>See what I'm doing? Suppose <code>sizeof(int)</code> is 4: I'm looking 4 bytes behind the passed pointer, as that would read the 4 bytes before where <code>int y</code> in the <em>caller's</em> stack.</p>
  
  <p>It did not print the 152. Strangely when I look at the next 4 bytes:</p>
  
  <pre><code>int *_prev_int =  (int *) ((long unsigned int) i +  sizeof(int))  ;
  </code></pre>
  
  <p>and now it works, prints whatever in <code>x</code> inside the caller's stack. Why <code>x</code> has a lower address than <code>y</code>? Are stack variables stored upside down?</p>