■課題
C言語にて自動変数 (auto 変数)は自動的に0では初期化されないことを確認する。
■ソースコード
#include <stdio.h>
int main(void){
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
printf("%d\n",a);
printf("%d\n",b);
printf("%d\n",c);
printf("%d\n",d);
printf("%d\n",e);
printf("%d\n",f);
printf("%d\n",g);
printf("%d\n",h);
printf("%d\n",i);
printf("%d\n",j);
printf("%d\n",k);
printf("%d\n",l);
printf("%d\n",m);
printf("%d\n",n);
printf("%d\n",o);
printf("%d\n",p);
printf("%d\n",q);
printf("%d\n",r);
printf("%d\n",s);
printf("%d\n",t);
printf("%d\n",u);
printf("%d\n",v);
printf("%d\n",w);
printf("%d\n",x);
printf("%d\n",y);
printf("%d\n",z);
}
■コンパイル(コンパイル時点で警告が出ている)
>cl helloauto.c
Microsoft(R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
helloauto.c
c:\myc\helloauto.c(5) : warning C4700: 初期化されていないローカル変数 'a' が使用されます
c:\myc\helloauto.c(6) : warning C4700: 初期化されていないローカル変数 'b' が使用されます
c:\myc\helloauto.c(7) : warning C4700: 初期化されていないローカル変数 'c' が使用されます
c:\myc\helloauto.c(8) : warning C4700: 初期化されていないローカル変数 'd' が使用されます
c:\myc\helloauto.c(9) : warning C4700: 初期化されていないローカル変数 'e' が使用されます
c:\myc\helloauto.c(10) : warning C4700: 初期化されていないローカル変数 'f' が使用されます
c:\myc\helloauto.c(11) : warning C4700: 初期化されていないローカル変数 'g' が使用されます
c:\myc\helloauto.c(12) : warning C4700: 初期化されていないローカル変数 'h' が使用されます
c:\myc\helloauto.c(13) : warning C4700: 初期化されていないローカル変数 'i' が使用されます
c:\myc\helloauto.c(14) : warning C4700: 初期化されていないローカル変数 'j' が使用されます
c:\myc\helloauto.c(15) : warning C4700: 初期化されていないローカル変数 'k' が使用されます
c:\myc\helloauto.c(16) : warning C4700: 初期化されていないローカル変数 'l' が使用されます
c:\myc\helloauto.c(17) : warning C4700: 初期化されていないローカル変数 'm' が使用されます
c:\myc\helloauto.c(18) : warning C4700: 初期化されていないローカル変数 'n' が使用されます
c:\myc\helloauto.c(19) : warning C4700: 初期化されていないローカル変数 'o' が使用されます
c:\myc\helloauto.c(20) : warning C4700: 初期化されていないローカル変数 'p' が使用されます
c:\myc\helloauto.c(21) : warning C4700: 初期化されていないローカル変数 'q' が使用されます
c:\myc\helloauto.c(22) : warning C4700: 初期化されていないローカル変数 'r' が使用されます
c:\myc\helloauto.c(23) : warning C4700: 初期化されていないローカル変数 's' が使用されます
c:\myc\helloauto.c(24) : warning C4700: 初期化されていないローカル変数 't' が使用されます
c:\myc\helloauto.c(25) : warning C4700: 初期化されていないローカル変数 'u' が使用されます
c:\myc\helloauto.c(26) : warning C4700: 初期化されていないローカル変数 'v' が使用されます
c:\myc\helloauto.c(27) : warning C4700: 初期化されていないローカル変数 'w' が使用されます
c:\myc\helloauto.c(28) : warning C4700: 初期化されていないローカル変数 'x' が使用されます
c:\myc\helloauto.c(29) : warning C4700: 初期化されていないローカル変数 'y' が使用されます
c:\myc\helloauto.c(30) : warning C4700: 初期化されていないローカル変数 'z' が使用されます
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:helloauto.exe
helloauto.obj
■実行結果
>helloauto.exe
4203936
1251903668
4248712
1245032
-337470327
2147348480
0
4215927
10
1244976
4215933
8
4215806
1245104
-342189936
0
0
4204584
4207837
4210620
1244988
4215947
1245056
1254933012
-2
4215927
■解説
コンピュータのメモリ(記憶領域)は、そろばんでたとえると「リセットされていない」状態である。
──────────
◆◆◆◆ ◆ ◆
◆◆ ◆◆
──────────
◆◆◆◆ ◆◆◆ ◆
◆ ◆ ◆◆◆ ◆◆
◆◆◆◆◆◆ ◆◆◆
◆◆◆◆◆◆◆◆
◆◆ ◆◆ ◆◆◆◆
──────────
こんな感じ。なので上記の実行結果ではでたらめな数字が出力される。
ちなみに上の図は 3141592653 となっている。
int n=0;
と初期化してやることは、そろばんをリセットするのと同じことである。
最初にじゃらら、とやる、あれである。
──────────
◆◆◆◆◆◆◆◆◆◆
──────────
◆◆◆◆◆◆◆◆◆◆
◆◆◆◆◆◆◆◆◆◆
◆◆◆◆◆◆◆◆◆◆
◆◆◆◆◆◆◆◆◆◆
──────────
こんな感じ。
C言語では、この初期化操作(=リセット)をやってやらないと warning C4700: 初期化されていないローカル変数 が出てしまう。
解説おわり。