fork download
  1. #include <stdio.h>
  2.  
  3. int allval1 = 0; // 全局变量
  4. int allval2 = 0;
  5.  
  6. static int sallval1 = 0; // 静态全局变量
  7. static int sallval2 = 0;
  8.  
  9. void displayAddresses() {
  10. static int sval1 = 0; // 静态局部变量
  11. static int sval2 = 0;
  12.  
  13. int val1 = 0; // 局部变量
  14. int val2 = 0;
  15.  
  16. printf("全局变量的存放顺序:\n");
  17. printf(" allval1: %p allval2: %p\n", (void*)&allval1, (void*)&allval2);
  18.  
  19. printf("静态全局变量的存放顺序:\n");
  20. printf(" sallval1: %p sallval2: %p\n", (void*)&sallval1, (void*)&sallval2);
  21.  
  22. printf("静态局部变量的存放顺序:\n");
  23. printf(" sval1: %p sval2: %p\n", (void*)&sval1, (void*)&sval2);
  24.  
  25. printf("局部变量的存放顺序:\n");
  26. printf(" val1: %p val2: %p\n", (void*)&val1, (void*)&val2);
  27. }
  28.  
  29. int main() {
  30. displayAddresses();
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
全局变量的存放顺序:
 allval1: 0x563e50d67018 allval2: 0x563e50d67014
静态全局变量的存放顺序:
 sallval1: 0x563e50d67028 sallval2: 0x563e50d67024
静态局部变量的存放顺序:
 sval1: 0x563e50d67020 sval2: 0x563e50d6701c
局部变量的存放顺序:
 val1: 0x7fff96fbf690 val2: 0x7fff96fbf694