In C as many of you know, the stack is where all local variables reside. The stack being a first in last out data structure means you can only access what has been most recently pushed onto it. So given the following code:
int k = 5;
int j = 3;
short int i;
if (k > j) i = 1;
Obviously this is useless code which has no real meaning but I'm trying to wrap my head around something.
For the short int i declaration I'm assuming 2 bytes get allocated on the stack. For int k and int j for both 4 bytes get allocated with the values 5 and 3. So the stack would look as follows
---------- <- stack pointer
int i
----------
int k = 5
----------
int j = 3
----------
so for the if statement you would have to pop the int i to get to the conditions k and j, and if so where does int i go? This all seems very time consuming and tedious if this is the way that C does local variables.
So is this actually how C does it or am I mucking it all up?