#include <stdio.h>
int main()
{
int i = 10;
return 0;
}
In the above program, where exactly the value 10 is stored ?
I understand the variable i is stored in the stack. stack is populated during run time. From "where exactly" 10 is coming from.
10 is a constant, so the compiler will use the number 10 directly in the executable part of your program as part of the CPU instructions.
Here's the assembly produced on my system with gcc
:
movl $10, -4(%rbp)
(The 4
is because an int
is 4 bytes long)
Note that all of these things are part of the implementation, but the above happens in practice. The language itself doesn't specify these details.
10
is a "literal", which will be generated by the compiler during compilation. And then it will be assigned to your variable on the stack. Like
mov eax, 10;
mov [0x12345678], eax;
This is pseude-code, though, but will assign your variable i
(address is here 0x12345678) the value 10
, previously stored in eax
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With