static
static修饰局部变量
#include<stdio.h>
//static修饰局部变量
void test() {
//int a = 1;
//a++;
//printf("%d ", a); //输出: 2 2 2 2 2 2 2 2 2 2
static int a = 1; //static修饰局部变量的时候,局部变量出了作用域,不销毁的;本质上,static修饰局部变量的时候,改变了变量的存储位置
a++;
printf("%d ", a); //输出: 2 3 4 5 6 7 8 9 10 11
}
int main() {
int i = 0;
while (i<10) {
test();
i++;
}
return 0;
}
static修饰全局变量
创建一个add.c文件,内容如下:
//全局变量
static int g_val = 2026;
//全局函数
static int Add(int x, int y) {
return x + y;
}
//创建一个main.c文件,内容如下
#include<stdio.h>
//static修饰全局变量(全局变量的外部链接属性就变成了内部连接属性,其他源文件(.c)就不能在使用这个全局变量)
//声明外部符号
extern int g_val;
int main(){
printf("%d\n",g_val);
}
static修饰函数
创建一个add.c文件,内容如下:
//全局变量
static int g_val = 2026;
//全局函数
static int Add(int x, int y) {
return x + y;
}
#include<stdio.h>
//static修饰函数(全局函数的外部链接属性就变成了内部连接属性,其他源文件(.c)就不能在使用这个全局变量)
extern int Add(int x, int y);
int main() {
int a=10;
int b = 20;
int z = Add(a,b);
printf("%d\n", z);
}
寄存器变量
#include<stdio.h>
//寄存器变量
int main() {
register int num = 3;//建议: 3 存放在寄存中
return 0;
}
define宏定义
#include<stdio.h>
//define定义标识符常量
#define NUM = 100;
//define定义宏
//宏是有参数的
#define ADD(x,y) x+y
int main() {
int a = 10;
int b = 20;
int c = ADD(a,b);
printf("%d\n", c);
}