13-常见的关键字
13-常见的关键字
| char | double | float | int | long | short | signed | unsigned |
|---|---|---|---|---|---|---|---|
| void | auto | break | case | const | continue | default | do |
| else | enum | extern | for | goto | if | register | return |
| sizeof | static | struct | switch | typedef | union | volatile | while |
【特点】C语言提供的关键字:①不能自己创建关键字、②变量名不能是关键字。
一、auto
表示自动的,每一个局部变量都是 auto 修饰的。
【注】自动变量是自动创建,自动销毁的。在实际使用过程中 auto 基本是省略的。而在新的 C语言 语法中也有其他用法(暂不做考虑)!!!
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
{
int a = 10; //自动创建,自动销毁的(自动变量)
}
return 0;
}
二、break
用于循环语句,switch语句中。
三、case
用于 switch 语句。
四、char
字符类型。详见:数据类型的用法 ——> 字符类型(char)。
五、const
六、continue
继续。
七、default
默认。用于 switch 语句。
八、do
用于 do while 循环。
九、double
双精度浮点型。详见:数据类型的用法 ——> 双精度浮点型(double)。
十、else
用于 if else 语句。
十一、enum
枚举类型,详见:常量 ——> enum 枚举常量。
十二、extern
用来 声明外部符号 的。详见:变量的作用域 ——> 求证(全局变量的作用域)。
十三、float
单精度浮点型。详见:数据类型的用法 ——> 单精度浮点型(float)。
十四、register
寄存器关键字。
1、疑问
计算机中数据可以存储到哪里呢?
2、解答
计算机中使用 CPU(中央处理器)处理数据,即进行相关的计算工作。早期在计算机计算过程中的数据来自于内存,后来随着技术的发展 CPU 处理数据的速度越来越快,而内存的读写速度没有跟上 CPU 的处理速度,这时候 CPU 很长的时间处于闲置状态,因此这时候使用 寄存器 来读写数据。
因为 寄存器 读写数据的速度更快。

故,CPU 从寄存器中读写数据,寄存器从高速缓存器中读写数据,高速缓存器从内存中读写数据。
【注】大量频繁被使用的数据,就放在寄存器中,为了提高效率。现在的编辑器可以不使用这个关键字了,因为编译器会自动分配!!!
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
register int num = 100; //建议 num 的值,存放在 寄存器 中
return 0;
}
十五、return
返回。详见:C语言代码简介 ——> 返回值 (return)。
十六、short
短整型。详见:数据类型的用法 ——> 短整型(short int)。
十七、signed
有符号的。描述的数字是有正负区分的符号。
十八、unsigned
无符号的。描述的数字是没有正负区分的符号,都是正值。
十九、static
静态的。
1、static 修饰局部变量
static 修饰局部变量,改变了局部变量的生命周期。

【注】在静态区中的全局变量的生命周期是整个程序的生命周期,因此 static 本质上是改变了局部变量的存储类型!!!
(1)未用 static 例程
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
void test()
{
/* 局部变量 a 创建 */
int a = 1; //a 为 1
a++; //a = a++
printf("%d\n", a); //打印 a++
} //局部变量 a 销毁
int main()
{
int i = 0;
while (i < 10)
{
/* 每次调用 test 函数都是重新创建 test 函数中的局部变量 a */
test(); //打印 10 个 2
i++;
}
return 0;
}

创建局部变量 int a = 1; 是有反汇编语句的。

(2)对比使用 static 例程
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
void test()
{
/* 使用 static 修饰创建的局部变量 a */
static int a = 1; //a 为 1
a++; //a = a++
printf("%d\n", a); //打印 a++
} //static 修饰创建的局部变量 a 没有销毁
int main()
{
int i = 0;
while (i < 10)
{
/* 每次调用 test 函数 test 函数中的局部变量 a 没有销毁(还在) */
test(); //打印 2 3 4 5 6 7 8 9 10 11
i++;
}
return 0;
}

创建全局变量 static int a = 1; 是没有反汇编语句的。

2、static 修饰全局变量
static 修饰全局变量,使得这个全局变量只能在自己所在的源文件(.c)内部使用,其他源文件不能使用。
【注】全局变量,在其他源文件内部是可以被使用的,是因为全局变量具有外部链接属性。但是被 static 修饰之后,就变成了内部链接属性,其他源文件就不能链接到这个静态的全局变量了!!!
(1)未用 static 例程
【1】add.c 文件
在 add.c 的源文件中定义一个全局变量 g_val。并在 test.c 的源文件中调用这个全局变量。
#define _CRT_SECURE_NO_WARNINGS 1
/* 全局变量 */
int g_val = 2022;
【2】test.c 文件
在 test.c 的源文件中调用 add.c 中定义的 g_val 全局变量。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
printf("%d\n", g_val);
return 0;
}
【3】运行结果


(2)对比使用 extern 例程
【1】add.c 文件
在 add.c 的源文件中定义一个全局变量 g_val。并在 test.c 的源文件中调用这个全局变量。
#define _CRT_SECURE_NO_WARNINGS 1
/* 全局变量 */
int g_val = 2022;
【2】test.c 文件
在 test.c 的源文件中调用 add.c 中定义的 g_val 全局变量。在调用前使用 extern 进行外部符号声明。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
/* extern 声明外部符号 */
extern int g_val;
int main()
{
printf("%d\n", g_val);
return 0;
}
【3】运行结果

【注】全局变量在整个工程中都可以使用!!!
(3)对比使用 static 例程
【1】add.c 文件
在 add.c 的源文件中定义一个全局变量 g_val。并在 test.c 的源文件中调用这个全局变量。
#define _CRT_SECURE_NO_WARNINGS 1
/* 使用 static 修饰全局变量 */
static int g_val = 2022;
【2】test.c 文件
在 test.c 的源文件中调用 add.c 中定义的 g_val 全局变量。在调用前使用 extern 进行外部符号声明。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
/* extern 声明外部符号 */
extern int g_val;
int main()
{
printf("%d\n", g_val);
return 0;
}
【3】运行结果


3、static 修饰函数
static修饰函数,使得函数只能在自己所在的源文件(.c)内部使用,不能在其他源文件内部使用。
【注】本质上:static 是将函数的外部链接属性变成了内部链接属性,和 static 修饰全局变量一样!!!
(1)未用 static 例程
【1】add.c 文件
在 add.c 的源文件中定义一个求和函数。并在 test.c 的源文件中调用这个函数。
#define _CRT_SECURE_NO_WARNINGS 1
int Add(int x, int y)
{
return x + y;
}
【2】test.c 文件
在 test.c 的源文件中调用 add.c 中定义的求和函数。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
/* 声明函数 */
extern int Add(int x, int y);
int main()
{
int a = 10;
int b = 20;
int sum = Add(a, b);
printf("sum = %d\n", sum);
return 0;
}
【3】运行结果

(2)对比使用 static 例程
【1】add.c 文件
在 add.c 的源文件中使用 static 定义一个求和函数。并在 test.c 的源文件中调用这个函数。
#define _CRT_SECURE_NO_WARNINGS 1
/* static 修饰函数 */
static int Add(int x, int y)
{
return x + y;
}
【2】test.c 文件
在 test.c 的源文件中调用 add.c 中定义的求和函数。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
/* 声明函数 */
extern int Add(int x, int y);
int main()
{
int a = 10;
int b = 20;
int sum = Add(a, b);
printf("sum = %d\n", sum);
return 0;
}
【3】运行结果


二十、struct
结构体关键字。
二十一、switch
switch 选择语句。
二十二、typedef
类型重定义。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
/* unsigned int 名字太长,重新定义为 u_int */
typedef unsigned int u_int; //uisigned int <==> u_int
int main()
{
unsigned int num1 = 100;
u_int num2 = 100;
printf("num1 = %d\t num2 = %d\n", num1, num2);
return 0;
}

二十三、union
联合体,又称共用体。
二十四、void
无、没有、空。
二十五、volatile
volatile 影响编译器编译的结果,volatile 指出变量是随时可能发生变化的,与 volatile 变量有关的运算,不要进行编译优化,以免出错。(咱不详细介绍)
二十六、while
while 循环语句。
二十七、define & include
1、疑问
define 和 include 是不是关键字?
2、解答
不是。它们是预处理指令。

浙公网安备 33010602011771号