fork(1) download
  1. #include <stdio.h>
  2.  
  3. int x; // グローバル変数(初期値は自動的に0)
  4.  
  5. void mondai1(int b) {
  6. x = b;
  7. }
  8.  
  9. void mondai2(void) {
  10. static int c = 10;
  11. x = c;
  12. c++;
  13. }
  14.  
  15. int mondai3(int d) {
  16. x++;
  17. d++;
  18. return d;
  19. }
  20.  
  21. int main(void) {
  22. printf("x = %d [GS:初期値がないから0]\n", x);
  23.  
  24. x = 101;
  25. printf("x = %d [GS:直前で101を代入したから101]\n", x);
  26.  
  27. mondai1(102);
  28. printf("x = %d [GS:mondai1(102)内でグローバル変数xに102が代入されたから102]\n", x);
  29.  
  30. mondai2();
  31. mondai2();
  32. mondai2();
  33. printf("x = %d [GS:mondai2が3回実行され static変数cの12が代入されたから12]\n", x);
  34.  
  35. for (int i = 103; i < 104; i++) {
  36. int x = i; // ローカル変数x(グローバル変数xをシャドーイング)
  37. printf("x = %d [LA:ローカル変数xにi(103)を代入したから103]\n", x);
  38.  
  39. x = mondai3(i);
  40. printf("x = %d [LA:mondai3(103)の戻り値104がローカル変数xに代入されたから104]\n", x);
  41. }
  42.  
  43. printf("x = %d [GS:for文を抜けグローバル変数xを参照。mondai3内でインクリメントされたため13]\n", x);
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
x = 0 [GS:初期値がないから0]
x = 101 [GS:直前で101を代入したから101]
x = 102 [GS:mondai1(102)内でグローバル変数xに102が代入されたから102]
x = 12 [GS:mondai2が3回実行され static変数cの12が代入されたから12]
x = 103 [LA:ローカル変数xにi(103)を代入したから103]
x = 104 [LA:mondai3(103)の戻り値104がローカル変数xに代入されたから104]
x = 13 [GS:for文を抜けグローバル変数xを参照。mondai3内でインクリメントされたため13]