Memory Allocation
返回
static用法
#include
void mod1_disp(void);
void mod2_disp(void);
static int err_ct = 0;
int main(void)
{
err_ct = 333;
mod1_disp();
mod2_disp();
mod1_disp();
return 0;
}
void mod1_disp(void)
{
printf("mod1:err_ct=%d\n", err_ct);
}
// list6_3.c
#include
void mod2_disp(void);
void mod2_disp2(void);
static int err_ct = 0;
void mod2_disp(void)
{
err_ct = 888;
mod2_disp2();
}
void mod2_disp2(void)
{
printf("mod2:err_ct=%d\n", err_ct);
}
/*
运行结果
mod1:err_ct=333
mod2:err_ct=888
mod1:err_ct=333
*/
用static保存数值
#include
void sum_auto(int d);
void sum_static(int d);
int main(void)
{
sum_auto(100);
sum_auto(100);
sum_auto(100);
sum_static(100);
sum_static(100);
sum_static(100);
return 0;
}
void sum_auto(int d)
{
int dt = 0;
dt = dt + d;
printf("sum_auto=%d\n", dt);
}
void sum_static(int d)
{
static int dt = 0;
dt = dt + d;
printf("sum_static=%d\n", dt);
}
/*
运行结果
sum_auto=100
sum_auto=100
sum_auto=100
sum_static=100
sum_static=200
sum_static=300
*/
#include
int main(void) {
char *p;
strcpy(p,"abc");
printf("%s\n",p);
}
C:test>cl test080331_1.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
test080331_1.c
test080331_1.c(4) : warning C4700: 値が割り当てられていないローカルな変数 'p' に対し
て参照が行われました。
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
C:test>test080331_1
abc
#include
int main(void) {
char *s1 = "Hello,";
char *s2 = "world!";
char *s3 = strcat(s1,s2);
printf("%s\n",s1);
printf("%s\n",s2);
printf("%s\n",s3);
}
C:test>cl test080331_1.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
test080331_1.c
test080331_1.c(5) : warning C4047: 'initializing' : 間接参照のレベルが 'char *' と
'int ' で異なっています。
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:test080331_1.exe
test080331_1.obj
C:test>test080331_1
Hello,world!
rld!
Hello,world!
返回