C语言-宏

C预处理器与宏

这篇文章,将介绍C预处理器,你将学到#include,#define和条件编译。

C预处理器是一个宏处理器(允许你定义宏),它在编译之前转换程序。这些转换可以包含头文件,宏扩展等。

所有的预处理指令都已#符号开始,例如:

1
#define PI 3.14

C预处理器的常见用途是:

Including Header Files

#include指令将头文件包含到C程序中,例如:

1
#include <stdio.h>

在这里”stdio.h”是一个头文件。#include预处理程序指令将上面的行替换为包含函数和宏定义的stdio.h头文件的内容。

这就是为什么在使用scanf()和printf()之类的函数之前需要使用#include <stdio.h>的原因。

Visit this page to learn on using header files.

Macros using #define

您可以使用#define预处理程序指令在C中定义宏。

宏是一个给出名称的代码片段。您可以使用名称在程序中使用该代码片段。例如,

1
#define c 299792458  // speed of light

在这里,当我们在程序中使用c时,它(PI)被替换为299792458。

Example 1: #define preprocessor

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#define PI 3.1415

int main()
{
float radius, area;
printf("Enter the radius: ");
scanf("%d", &radius);
// Notice, the use of PI
area = PI*radius*radius;
printf("Area=%.2f",area);
return 0;
}

您还可以定义像函数调用一样工作的宏,称为类函数宏。例如,

1
#define circleArea(r) (3.1415*(r)*(r))

每次程序遇到circleArea(参数)时,它都被(3.1415 (argument)(argument))替换。

假设,我们将5作为参数传递,然后扩展如下:

1
circleArea(5) expands to (3.1415*5*5)

Example 2: Using #define preprocessor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#define PI 3.1415
#define circleArea(r) (PI*r*r)

int main()
{
int radius;
float area;

printf("Enter the radius: ");
scanf("%d", &radius);
area = circleArea(radius);
printf("Area = %.2f", area);

return 0;
}

Conditional Compilation

在C编程中,您可以指示预处理器是否包含某些代码。为此,可以使用条件指令。
它类似于if语句。但是,您需要了解一个很大的区别。

在执行期间测试if语句以检查是否应该执行代码块,而条件用于在执行之前包含(或跳过)程序中的某些代码块。

Uses of Conditional

  • 根据机器,操作系统使用不同的代码
  • 在两个不同的程序中编译相同的源文件
  • 从程序中排除某些代码,但将其作为参考以备将来使用

How to use conditional?

要使用条件,请使用#ifdef,#if,#define,#else和#elseif指令。

#ifdef Directive

1
2
3
#ifdef MACRO
conditional codes
#endif

这里,仅当定义了MACRO时,条件代码才包含在程序中。

#if, #elif and #else Directive

1
2
3
#if expression
conditional codes
#endif

这里,expression是整数类型的表达式(可以是整数,字符,算术表达式,宏等)。仅当表达式被计算为非零值时,条件代码才包含在程序中。

可选的#else指令可以与#if指令一起使用。

1
2
3
4
5
#if expression
conditional codes if expression is non-zero
#else
conditional if expression is 0
#endif

您还可以使用#elif将嵌套条件添加到#if … #else

1
2
3
4
5
6
7
8
9
10
#if expression
conditional codes if expression is non-zero
#elif expression1
conditional codes if expression is non-zero
#elif expression2
conditional codes if expression is non-zero
... .. ...
else
conditional if all expressions are 0
#endif

#defined

特殊运算符#defined用于测试是否定义了某个宏。它通常与#if指令一起使用。

1
2
#if defined BUFFER_SIZE && BUFFER_SIZE >= 2048
conditional codes

Predefined Macros

有一些预定义的宏可以很容易地用于C编程。

Predefined macro Value
__DATE__ String containing the current date
__FILE__ String containing the file name
__LINE__ Integer representing the current line number
__STDC__ If follows ANSI standard C, then value is a nonzero integer
__TIME__ String containing the current date.

Example 3: Get current time using TIME

以下程序使用TIME宏输出当前时间。

1
2
3
4
#include <stdio.h>
int main() {
printf("Current time: %s",__TIME__); //calculate the current time
}

Output

1
Current time: 19:54:39

引用自

https://www.programiz.com/c-programming/c-preprocessor-macros#example-define