C++ 预处理器

2024 年 8 月 28 日 | 3 分钟阅读

C++ 编程语言中主要有四种预处理器指令,它们是:-

  1. 文件包含
  2. 条件编译
  3. 其他指令

C/C++ 编程语言中的宏是最令人兴奋的概念之一。它们是使用 #define 编写在 C++ 代码中的语句,每当调用已分配的变量时,相关的值将被替换并执行。

C++ 代码

输出

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14

带参数的宏 C++ 代码

输出

The area of the object similar to a rectangle is: 10850

2. 文件包含

这是 C++ 预处理器中常见的指令之一,我们每天都在使用它,包括一些对我们日常基本编程需求至关重要的内置库。

语法

3. 条件编译

条件独特的编译和保留指令是指令中一个重要且不可或缺的组成部分,它帮助我们无缝地编译和运行程序的特定部分或代码片段,以便根据某些条件跳过程序某些特定部分的编译。

语法

4. 其他指令

除了 C++ 编程语言中的指令之外,还有两个鲜为人知且不常用的指令:#undef 指令、#pragma startup 和 #pragma exit: 指令。它们的实现如下所示

C++ 代码

输出

/tmp/ Lj D4 TT sh NS.o
Hello, coder on javaTpoint.com. We are inside the int primary () function now.

C++ 代码

输出

hello coder we are currently inside the function_1_is()
hello coder, we are currently inside the int main() function
hello coder we are currently inside the function_2_is()

祝您编码愉快!