在C语言中,外部引用宏
通常是通过使用#define
指令在一个头文件中定义宏,然后在其他源文件中包含这个头文件来实现的。
在macros.h头文件中定义了一个求和的宏
//macros.h文件
#ifndef MACEOS_H//涉及到包含保护(Include Guard)后面一篇文章讲解
#define MACEOS_H
#define ADDDD(x,y)((x)+(y))
#endif
要在test.c文件使用这个宏,在源文件中用包含这个头文件来实现;
//test.c文件
#include <stdio.h>
#include "macros.h"//包含这个头文件来实现引用宏
int main()
{
int a;
int b;
scanf_s("%d %d", &a, &b);
int d = ADDDD(a, b);
printf("%d\n", d);
return 0;
}
在macros.h头文件文件中定义宏:求和
在源文件test.c中使用这个宏:
运算结果: