常见关键字
一、常见关键字
C语言提供关键字,不能自己创建;
变量名不能是关键字;
define,include不是关键字,而是预处理命令
1.关键字 auto
每个局部变量都是auto修饰的,只不过省略了。
2.关键字 extern
是用来声明外部符号的
3.关键字register
寄存器关键字
register int num = 100;
//建议num的值寄放在寄存器中
4.关键字 signed
有符号
5.关键字 unsigned
无符号
6.关键字 union
联合体(公用体)
7.关键字 void
无、空
8.关键字 typedef
typedef 顾名思义是类型定义,这里应该理解为类型重命名。
//将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;
}
9.关键字static
在C语言中:
static是用来修饰变量和函数的
- 修饰局部变量-称为静态局部变量
- 修饰全局变量-称为静态全局变量
- 修饰函数-称为静态函数
修饰局部变量
#include <stdio.h>
void test()
{
int i = 1;
i++;
printf("%d ", i);
}
int main()
{
int i = 0;
for(i=0; i<10; i++)
{
test();
}
return 0;
}
#include <stdio.h>
void test()
{
//static修饰局部变量
static int i = 1;
i++;
printf("%d ", i);
}
int main()
{
int i = 0;
for (i = 0; i < 10; i++)
{
test();
}
return 0;
}
对比代码1和代码2的效果理解static修饰局部变量的意义。
结论:
static修饰局部变量改变了变量的生命周期
让静态局部变量出了作用域依然存在,到程序结束,生命周期才结束。
修饰全局变量
#include<stdio.h>
//add.c
int g_val = 2018;
//test.c
int main()
{
printf("%d\n", g_val);
return 0;
}
#include<stdio.h>
//代码2
//add.c
static int g_val = 2018;
//test.c
int main()
{
printf("%d\n", g_val);
return 0;
}
代码1正常,代码2在编译的时候会出现连接性错误。
结论:
一个全局变量被static修饰,使得这个全局变量只能在本源文件内使用,不能在其他源文件内使
用。
全局变量,在其他源文件内部可以被使用,是因为全局变量具有外部链接属性,但是被static修饰之后,就变成了内部链接属性其他源文件就不能链接到这个静态的全局变量了。
修饰函数
#include<stdio.h>
//代码1
//add.c
int Add(int x, int y)
{
return c+y;
}
//test.c
int main()
{
printf("%d\n", Add(2, 3));
return 0;
#include<stdio.h>
//代码2
//add.c
static int Add(int x, int y)
{
return c+y;
}
//test.c
int main()
{
printf("%d\n", Add(2, 3));
return 0;
}
代码1正常,代码2在编译的时候会出现连接性错误.
结论:
一个函数被static修饰,使得这个函数只能在本源文件内使用,不能在其他源文件内使用。
本质上,static是将函数的外部链接属性变成了内部链接属性,与static修饰全局变量一样。