I have been noticing that some of the programs that I write in C have not been working on my machine, but they works other others. So to test this, I wrote a simple program to test how the stack is pushing and popping local variables:
#include <stdio.h>
#include <string.h>
int main() {
char char_test[10];
int int_test = 0;
strcpy(char_test, "Hello");
}
I then debugged this program and found that int_test had a higher memory address than char_test, even though, according to my knowledge, the first declared local variable is supposed to have a higher memory address. I then added two print functions that printed the memory addresses of the variables:
#include <stdio.h>
#include <string.h>
int main() {
char char_test[10];
int int_test = 0;
strcpy(char_test, "Hello");
printf("Address of char_test: 0x%x\n", &char_test);
printf("Address of int_test: 0x%x\n", &int_test);
}
Now, the first local variable has a higher memory address than the second. Does printing the addresses change the ordering of the variables? Am I doing something wrong?