How are variable values stored in C?
Your understanding is completely wrong.
When you write int x = 4;
, the x
represents an actual memory location on the stack, that then gets filled with the value 4
. x
is irrevocably linked with that piece of memory - when x
goes out of scope the memory also disappears.
When you write int y = x;
again y
represents an actual piece of memory. It does not 'refer' to x
, instead, the contents of x
are copied into y
.
Is it the same for all languages?
No, different languages can and do have completely different semantics. However the way C does it is usually called value semantics.
y
never references x
. The assignment operator, =
, copies values. x
is just a value of 4
, of int
type. int y = x
is assigning the current value of x to y, copying it in the process.
To behave like you're describing, y
would need to be a pointer to an int, int *
, and it would be assigned the address of x
, like so:
#include <stdio.h>
int main(int argc, char *argv[]) {
int x = 4;
int *y = &x;
printf("before: x: %d, y: %d\n", x, *y);
x = 123; // modify x
printf("after: x: %d, y: %d\n", x, *y);
}
X now references the memory location where the '4' is stored
No, 4 isn't stored anywhere, it's a parameter to a mov
. x
has its own memory location that holds an integer value, in this case 4.
y references x
No, y
also has its own memory location that stores an integer, also in this case 4.
So why is it that when I change the value of x, eg x=6;, y doesn't get changed
They're both different memory locations, changing one has no impact on the other.