自動記憶域期間 と 静的記憶域期間
明解C言語 入門編 > 6. 関数 >
自動記憶域期間 と 静的記憶域期間
C
#include <stdio.h>int fx = 0;
void func(void)
{
static int sx = 0;
int ax = 0;
printf("%3d%3d%3d\n", ax++, sx++, fx++);
}int main(int argc, char* argv[])
{
puts(" ax sx fx");
puts("---------");int i;
for (i = 1; i <= 10; i++)
func();return 0;
}
実行結果
R:\>lesson053\project1.exe
ax sx fx
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
0 0 0
0 1 1
0 2 2
0 3 3
0 4 4
0 5 5
0 6 6
0 7 7
0 8 8
0 9 9
Delphi
program Project1;{$APPTYPE CONSOLE}
uses
SysUtils;var
fx: Integer = 0;
i: Integer;procedure func();
const
sx: Integer = 0;
var
// ax: integer = 0; ローカル変数は初期化できません
ax: Integer;
begin
writeln(format('%3d%3d%3d', [ax, sx, fx]));
inc(ax);
inc(sx);
inc(fx);
end;begin
writeln(' ax sx fx');
writeln('---------');for i := 1 to 10 do
func;
end.
実行結果
S:\>lesson053\project1.exe
ax sx fx
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
10 0 0
9 1 1
8 2 2
7 3 3
6 4 4
5 5 5
4 6 6
3 7 7
2 8 8
1 9 9