fork download
  1. #include <stdio.h>
  2.  
  3. int x;
  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 [グローバル変数の初期値]¥n", x);
  23. x = 101;
  24. printf("x = %d [xに101を代入]¥n", x);
  25. mondai1(102);
  26. printf("x = %d [mondai1でxに102を代入]¥n", x);
  27. mondai2();
  28. mondai2();
  29. mondai2();
  30. printf("x = %d [mondai2を3回繰り返し、xは最終的に12]¥n", x);
  31. for (int i = 103; i<104; i++){
  32. int x = i; // ループ内だけのローカル変数
  33. printf("x = %d [ループ内のローカル変数xにi(103)を代入]¥n", x);
  34. x = mondai3(i);
  35. printf("x = %d [mondai3の戻り値(104)をローカル変数xに代入]¥n", x)
  36. ;
  37. }
  38. printf("x = %d [グローバル変数xの値 (mondai3内で13に更新済)]¥n", x);
  39. return 0;
  40. }
  41.  
  42.  
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout
x = 0 [グローバル変数の初期値]¥nx = 101 [xに101を代入]¥nx = 102 [mondai1でxに102を代入]¥nx = 12 [mondai2を3回繰り返し、xは最終的に12]¥nx = 103 [ループ内のローカル変数xにi(103)を代入]¥nx = 104 [mondai3の戻り値(104)をローカル変数xに代入]¥nx = 13 [グローバル変数xの値 (mondai3内で13に更新済)]¥n