palavra-chave estática.
1. Static modifica variáveis locais;
① Todo o ciclo de vida é estendido,
② A variável local estática será inicializada apenas uma vez, e toda vez que a variável local estática for chamada no futuro, o valor salvo após a última chamada será usado.
③ Só pode ser acessado por variáveis e funções do escopo, embora exista em todo o ciclo de vida do programa, não pode ser acessado por outras funções ou arquivos.
//没有static修饰变量
#include<stdio.h>
#include<windows.h>
int func(int c){
int a = 0;
int b = 3;//修饰
int sum;
a++;
b++;
sum =a + b + c;
return sum;
}
int main()
{
int c = 1;
for (int i = 1; i <= 4;i++){
printf("%d\n",func(c));
}
system("pause");
return 0;
}
//输出结果为6
6
6
6
请按任意键继续. . .
static修饰变量;
#include<stdio.h>
#include<windows.h>
int func(int c){
int a = 0;
static int b = 3;
int sum;
a++;
b++;
sum =a + b + c;
return sum;
}
int main()
{
int c = 1;
for (int i = 1; i <= 4;i++){
printf("%d\n",func(c));
}
system("pause");
return 0;
}
//输出结果为:
6
7
8
9
请按任意键继续. . .·
2. Estático modifica variáveis globais
Variáveis globais são modificadas por variáveis estáticas.As variáveis globais estáticas só podem ser usadas no arquivo fonte onde são definidas, e não são visíveis no arquivo fonte do mesmo projeto.
//test1.c源文件
#include<stdio.h>
#include<windows.h>
int a = 1;
//我们在test1.c文件文件中定义一个整型变量,在test2c中使用
//test2源文件
#include<stdio.h>
#include<windows.h>
int main(){
extern int a;
printf("%d\n",a);
system("pause");
return 0;
}
//运行结果为
1
请按任意键继续. . .
//运行就会报搓
Variáveis globais comuns são visíveis para todo o projeto, e outros arquivos podem ser usados diretamente após serem declarados externamente por extern
O seguinte usa modificação estática para as variáveis no exemplo acima
//test1.c文件
#include<stdio.h>
#include<windows.h>
static int a = 1;
//test2.c文件
#include<stdio.h>
#include<windows.h>
int main(){
extern int a;
printf("%d\n",a);
system("pause");
return 0;
//运行结果会报错
//1>LINK : fatal error LNK1168: 无法打开 D:\c 语言\VS //code\static\Debug\static.exe 进行写入========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
3. Função estática modificada
Função estática modificada, a função é invisível para outros arquivos de origem, exceto este arquivo de origem. Quando a função não quiser ser vista pelo mundo externo, use estática para modificar esta função.
//test1.c文件
#include <stdio.h>
#include<windows.h>
void fun(void)
{
printf("hello\n");
system("pause");
}
//test2.c文件
#include <stdio.h>
#include<windows.h>
int main(void)
{
fun();
system("pause");
return 0;
}
//运行结果为:
hello
请按任意键继续. . .
A função não é decorada com estática, a função pode ser chamada por outros arquivos.
Abaixo, usamos estática para modificar a função no exemplo acima
//test1.c文件
#include <stdio.h>
#include<windows.h>
static void fun(void)
{
printf("hello\n");
system("pause");
}
//test2.c文件
#include <stdio.h>
#include<windows.h>
int main(void)
{
fun();
system("pause");
return 0;
}
//运行就会报错
//1>D:\c 语言\VS code\static\Debug\static.exe : fatal error LNK1120: 1 个无法解析的外部命令
//此时的fun()函数只能在test1.c文件内使用,test2.c是看不见test1.c文件内的fun()函数