C语言提供了丰富的关键字,而这些关键字都是事先预定好的,用户自己是不能创造关键字的
下面我会介绍几个关键字,其它没介绍的后期都会有讲到
关键字typedey
可以把一个类型简化,重新定义
//将unsigned int 重命名为uint_32, 所以uint_32也是一个类型名
typedef unsigned int uint_32;
int main()
{
//观察num1和num2,这两个变量的类型是一样的
unsigned int num1 = 0;
uint_32 num2 = 0;
return 0; }
关键字static
在C语言中: static是用来修饰变量和函数的
- 修饰局部变量-称为静态局部变量
- 修饰全局变量-称为静态全局变量
- 修饰函数-称为静态函数
关键字static – 修饰局部变量
```c
//代码1
#include <stdio.h>
void test()
{
int a = 5;
//存放在内存的栈区
a++;
printf("%d ", a);
}
//出去就被销毁
int main()
{
int i = 0;
for(i=0; i<10; i++)
{
test();
}
return 0; }
//6 6 6 6 6 6 6 6 6 6
//代码2
#include <stdio.h>
void test()
{
//static修饰局部变量
static int a = 5;
//存放在内存的静态区
a++;
printf("%d ", a);
}
//出去以后不被销毁
int main()
{
int i = 0;
for(i=0; i<10; i++)
{
test();
}
return 0;
}
//6 - 15
结论:
static修饰局部变量的时候,局部变量就变成了静态的局部变量出了局部变量出了局部的范围,不会销毁,下一次进入函数依旧存在
其实是因为:static修饰的局部变量是存储在静态区的
static修饰局部变量时,实际改变的是变量的存储位置,本来一个局部变量是放在栈区的,被static修饰后放在了静态区。
从而导致出了作用域依然存在,生命周期并没用结束
关键字static – 修饰全局变量
static修饰函数的作用:
一个函数本来是具有外部链接属性的,但是被static修饰后,外部链接属性就变成了内部链接属性,这时这个函数只能在自己所在的源文件内部使用,其他文件是无法使用的。
作用域变小了
//代码1
//add.c
int g_val = 2022;
//test.c
int main()
{
printf("%d\n", g_val);
return 0; }
//代码2
//add.c
static int g_val = 2022;
//test.c
int main()
{
printf("%d\n", g_val);
return 0; }