fork download
  1. #include <stdio.h>
  2. int x;
  3.  
  4. void mondai1(int b){
  5. x = b;
  6. }
  7.  
  8. void mondai2(void){
  9. static int c = 10;
  10. x = c;
  11. c++;
  12. }
  13.  
  14. int mondai3(int d){
  15. x++; // グローバル x
  16. d++; // ローカル d
  17. return d;
  18. }
  19.  
  20. int main(void) {
  21.  
  22. // 1回目:x はグローバル、初期値 0(GA)
  23. printf("x = %d [GA: グローバル変数は0で初期化される]\n", x);
  24.  
  25. // 2回目:x = 101 を代入(GA)
  26. x = 101;
  27. printf("x = %d [GA: mainで101を代入した]\n", x);
  28.  
  29. // 3回目:mondai1(102) により x = 102(GA)
  30. mondai1(102);
  31. printf("x = %d [GA: mondai1で102を代入した]\n", x);
  32.  
  33. // 4回目:mondai2 を3回呼ぶ → static c が 10→11→12→13 と変化
  34. mondai2(); // x = 10
  35. mondai2(); // x = 11
  36. mondai2(); // x = 12
  37. printf("x = %d [GS: static変数cが3回インクリメントされて12になった]\n", x);
  38.  
  39. // 5回目:for文内のローカル変数 x(LA)
  40. for (int i = 103; i < 104; i++){
  41. int x = i; // ローカル x
  42. printf("x = %d [LA: for文内でi(103)を代入した]\n", x);
  43.  
  44. // 6回目:mondai3 によりローカル x に戻り値 104 を代入(LA)
  45. x = mondai3(i);
  46. printf("x = %d [LA: mondai3の戻り値104を代入した]\n", x);
  47. }
  48.  
  49. // 7回目:グローバル x は mondai3 により 12→13(GA)
  50. printf("x = %d [GA: mondai3でグローバルxが++され13になった]\n", x);
  51.  
  52. return 0;
  53. }
  54.  
  55.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
x = 0 [GA: グローバル変数は0で初期化される]
x = 101 [GA: mainで101を代入した]
x = 102 [GA: mondai1で102を代入した]
x = 12 [GS: static変数cが3回インクリメントされて12になった]
x = 103 [LA: for文内でi(103)を代入した]
x = 104 [LA: mondai3の戻り値104を代入した]
x = 13 [GA: mondai3でグローバルxが++され13になった]