初识C语言——常见关键字、#define定义常量和宏

1. 常见关键字

auto

是自动的

break

循环/switch语句

casechar

const

修饰常变量

continue

继续

default

默认

dodoubleelse

enum

枚举

extern

声明外部符号的

floatforgotoifintlongregister

return

返回

short

signed

有符号的 例:20,-1

sizeof

static

静态的

struct

结构体

switch

语句

typedef

类型定义

union

联合体(共用体)

unsigned

无符号的

void

无/空

volatile

while

循环

1.1 auto  是自动的

       auto  是自动的——每个局部变量都是auto修饰的。   新的c语言语法中也有其他的用法,暂不考虑。    例:auto int = 10;    //a是自动创建,自动销毁的—自动变量。  auto一般省略。 

1.2 extern  声明外部符号关键字

       声明函数只需给出:函数类型、函数声明、参数类型。

       例:extern int add(int x, int y);

1.3 register  寄存器关键字

       register  寄存器关键字——大量/频繁被使用的数据想放到寄存器中,提升效率。但意义不大。      例:register int num = 100;      //建议num的值存放到寄存器中,但是是否放到寄存器中是由编译器决定的。

       计算机中数据可以存储到:寄存器、高速缓存、内存、硬盘、网盘中。    

        早期的:CPU——内存         CPU运行过快,但内存速度跟不上,会导致CPU闲置。

        现在(如上图):CPU处理数据时,在内存基础上提供一个高速缓存,在高速缓存基础上提供一个寄存器。CPU拿数据时先去寄存器中拿,再去高速缓存中拿,再去内中拿。z——如图中蓝色箭头所示。  整体效率提升。

  • 当处理寄存器数据时,将高速缓存中数据拿过来;处理高速缓存数据时,将内存中数据拿过来。因此大部分数据都可以在寄存器中找到,找不到去高速缓存中找,再找不到去内存中找。

1.4 typedef  类型重命名

       typedef  类型重命名——使类型变得简单使用。

 //typedef  类型重命名
#include <stdio.h>
typedef unsigned int uint_x;      //将unsigned int重命名为uint_x,所以uint_x也是一个类型名
int main()
{
	unsigned int num1 = 4;
	uint_x num2 = 0;             //与上行的代码具有相同的意义
	printf("%d\n", num1);
	printf("%d\n", num2);
	return 0;
}                        /*输出结果为:4
                                      0*/

1.5 static  静态的

       static  静态的——用来修饰变量和函数的。

       三种用法:1. 修饰局部变量——称为静态局部变量

                         2. 修饰全局变量——称为静态全局变量

                         3. 修饰函数——称为静态函数

1.5.1 修饰局部变量——当我们希望一个变量除了它的范围后不销毁,则用此类修饰
  • 出循环后变量值不销毁,自动保留上一次的计算值。
  • 改变了局部变量的生命周期——本质上是改变了变量的存储类型(a由栈区→静态区)。
  • static修饰的局部变量让静态局部变量出了作用域依然存在,到程序结束,生命周期才结束。

       对比代码1和代码2即可理解static修饰局部变量的意义:

 //static修饰局部变量  静态局部变量
 //代码1
#include <stdio.h>
void test()
{
	int a = 7;                //a为局部变量
	a++;
	printf("%d ", a);         //循环一次打印后出循环即消失,下一次循环a值仍然为7
}
int main()
{
	int i = 0;                 //代码开始
	for (i = 0; i < 10; i++)   //循环10次结束
	{
		test();
	}
	return 0;
}              //输出结果为:8 8 8 8 8 8 8 8 8 8

 //代码2
#include <stdio.h>
void test()
{
	static int a = 7;          //a为static修饰的局部变量
	a++;                       
	printf("%d ", a);         //循环一次打印后出循环即消失,下一次循环a值为上一次循环的结果
}
int main()
{
	int i = 3;                 //代码开始
	for (i = 3; i < 10; i++)   //循环7次结束
	{
		test();
	}
	return 0;
}              //输出结果为:8 9 10 11 12 13 14
1.5.2 修饰全局变量
  • 一个全局变量被static修饰,会使得这个全局变量只能在本源文件内使用,不能在其他源文件内使用。
  • 全局变量——在其他源文件内部可以被使用,是因为全局变量具有外部链接属性。但是被static修饰后,就变成了内部链接属性,其他源文件就不能链接到这个静态的局部变量了。
 //static修饰全局变量  两个源文件
 //代码1
 //源文件1
int x = 2018;
 //源文件2
#include<stdio.h>
extern int x;
int main()
{
	printf("%d\n", x);
	return 0;
}                  //输出结果为:2018



 //代码2
 //源文件1
static int x = 2018;
 //源文件2
#include <stdio.h>
extern int x;
int main()
{
	printf("%d\n", x);
	return 0;
}                 //报错!1.无法解析的外部符号“int x”。2.无法解析的外部命令。
1.5.3 修饰函数
  • 一个函数被static修饰,会使得这个函数只能在本源文件内使用,不能在其他源文件内使用。
  • 本质上:static是将函数的外部链接属性转变为内部连接属性。(与修饰全局变量一致)
 //static修饰函数
 //代码1
 //源文件1
int add(int a, int b)
{
	return a + b;
}
 //源文件2
#include<stdio.h>
extern int add(int a, int b);
int main()
{
	printf("%d\n", add(6, 8));
	return 0;
}                   //输出结果为:14


 //代码2
 //源文件1
static int add(int a, int b)
{
	return a + b;
}
 //源文件2
#include<stdio.h>
extern int add(int a, int b);
int main()
{
	printf("%d\n", add(6, 8));
	return 0;
}                  //输出结果为:报错!1.无法解析的外部符号。2.无法解析的外部命令。

2. #define定义常量和宏

  • define是一个预处理指令,并不是关键字。——与include相同。
 //define定义常量
#define MAX = 100;    //在变量与常量部分已经涉及到,有详细代码


 //define定义宏
 //代码2
#define add(x,y) ((x)+(y))
#include<stdio.h>
int main()
{
	int sum = add(7, 5);
	printf("sum=%d\n", sum);

	printf("num=%d\n", 5*add(7,5));         //5*(7+5)
	return 0;
}                     /*输出结果为:sum = 12
                                   num = 60*/


 //代码2
#define add(x,y) x+y
#include<stdio.h>
int main()
{
	int sum = add(7, 5);
	printf("sum=%d\n", sum);

	printf("num=%d\n", 5 * add(7, 5));       //5*7+5
	return 0;
}                     /*输出结果为:sum = 12
                                   num = 40*/
  • 38
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值