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);
}
相关推荐
RuoZoe7 天前
重塑WPF辉煌?基于DirectX 12的现代.NET UI框架Jalium
c语言
祈安_11 天前
C语言内存函数
c语言·后端
郑州光合科技余经理12 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo12312 天前
matlab画图工具
开发语言·matlab
dustcell.12 天前
haproxy七层代理
java·开发语言·前端
norlan_jame12 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone12 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ40220549612 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
czy878747512 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
遥遥江上月12 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js