C语言-static关键词,寄存器变量,define宏定义

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);
}
相关推荐
LDR0069 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术9 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园9 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob9 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享9 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.9 天前
C语言--day30
c语言·开发语言
玖玥拾9 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
何以解忧,唯有..9 天前
Go语言循环语句详解:for、range与循环控制
开发语言·算法·golang
謓泽9 天前
C语言不是语法,是通往机器的地图。
c语言·开发语言
云水一下9 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php