文章目录
一、常见关键字
注意define include这样的不是关键字,而是预处理指令
关键字不能做变量名:
int char;//这样的写法是不对的。
auto break case char const continue default do double else enum
extern float for goto if int long register return short signed
sizeof static struct switch typedef union unsigned void volatile while
初始介绍:
- auto 自动
int main()
{
int a = 0; //这里的a是自动创建、自动销毁的,代码省略了auto,完整写法应该是 auto int a = 0;
return 0;
}
- register 建议将变量放入寄存器
int main()
{
register int a = 100; //这里表达的意思是建议将整型a放入寄存器中
return 0;
}
- typedef 类型重定义
typedef unsigned int u_int; //这里是将unsigned int重定义了一次。
int main()
{
unsigned int a = 10; //unsigned是无符号类型的 “unsigned int”较长可以做一个重定义
u_int b = 10; //这里的u_int 和unsigned int是同一个作用
return 0;
}
-
static 是用来修饰变量和函数的
(1)修饰局部变量-称为静态局部变量
(2) 修饰全局变量-称为静态全局变量
(3) 修饰函数-称为静态函数
【1】修饰静态局部函数:
void test() { int b = 1; //每次循环,b出了作用域就会自动销毁,因此每次b的初始值就是1. b++; printf("%d\n",b); } int main() { int a = 0; while(a<10) { test(); a++; } return 0; }
如果加上static,那结果会成为:
出现这样的原因可以理解为static使得局部变量的作用域范围扩大,使每次循环都不会销毁static后所赋的值。
【2】修饰静态全局变量
一般情况下引用外部变量只需要如图,写一个“extern”+“定义的函数”;
但是如果加上static,结果变成:
此时,及时加上“extern”也无法引用外部函数。
这是由于static把定义的静态全局变量由外部函数变成内部函数,使其无法被引用到另一个文件,看似是作用域变小了。
【3】static修饰静态函数
其作用类似于对静态全局变量的修饰:
一般引用外部函数时,使用extern就可以,
但是加上static后:
与静态全局变量一样,此时static使得函数的外部链接变成了内部变量。
二、define定义关键字和宏
1.定义关键字:
#include <stdio.h>
#define max 200
int main()
{
printf("%d", max);
return 0;
}
2.定义宏:
#include <stdio.h>
#define Add(x,y)((x)+(y)) //一定要加括号
int main()
{
int a = 1;
int b = 2;
int c = Add(a, b);
printf("%d\n", c);
return 0;
}
以上就是今天所学的内容整理,内容有不当之处,请各位大佬多多指正,不吝赐教!