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);
}
相关推荐
1892280486116 小时前
NV022固态MT29F16T08GWLCEM5-QBES:C
c语言·开发语言
划水的code搬运工小李16 小时前
下载CSDN到PDF
开发语言·pdf·swift
紫阡星影16 小时前
【STM32CubeMX项目】智能家居门禁系统
c语言·单片机·嵌入式硬件
不负岁月无痕16 小时前
STL-- C++ stack_queue _priority_queue类 模拟实现
开发语言·c++
半个烧饼不加肉16 小时前
JS 底层探究--上下文
开发语言·javascript·ecmascript
小满Autumn16 小时前
依赖注入设计模式速查手册
开发语言·c#·wpf·mvvm·依赖注入
周末也要写八哥16 小时前
浅谈:C++中cpp 14 ~ cpp 17
开发语言·c++·算法
不会C语言的男孩16 小时前
C++ Primer 第13章:拷贝控制
开发语言·c++
z落落17 小时前
C# 静态成员 vs 非静态成员(调用规则+内存特点)+只读和常量 const常量 / readonly / static readonly 三者终极区别
java·开发语言·c#
zhangfeng113317 小时前
超算中心 高性能计算 slurm的linux版本 centos7,如何安装docker,如何安装torch2.4
linux·运维·服务器·开发语言·人工智能·机器学习·docker