C++ 预处理器

C++ 预处理器

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

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

C/C++编程语言中的宏是最令人兴奋的概念之一。这些是用#define编写的C++代码中的句子,每当调用分配的变量时,关联的值将被交换并执行。

C++代码

// Here we are writing down the simple C++ programming language code snippet
// To demonstrate the concept of C/C++ preprocessors concept with their code
// and relevant display output on the compiler
#include 

// macro definition through a simple C++ code with #define functionality
#define LIMIT 15
// the main driver code functionality starts from here
int main()
{
    // this is simple for a loop running from 0 initially to the LIMIT
    for (int i = 0; i < LIMIT; i++) {
        std::cout << i << "\n";
    }

    return 0;
}

输出:

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

C++代码中的带参数宏

// Here we are writing down the simple C++ programming language code snippet
// To demonstrate the concept of C/C++ preprocessors concept with their code
// and relevant display output on the compiler
#include <iostream>

// macro with parameter definition 
// through a simple C++ code with #define functionality

#define AREA(l, b) (l * b)
// the main driver code functionality starts from here
int main()
{
    int l1 = 310, l2 = 35, area;

    area = AREA(l1, l2);

   // this is simple output code running whenever the macro with a parameter 
   // is executed
    std::cout << "The area of the object similar to rectangle is: " << area;

    return 0;
}

输出:

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

文件包含

这是我们日常使用的常见的C++预处理指令之一,用于包含一些我们日常基本编程需求中必不可少的内置库。

语法

#include
Example: 
#include
#include

条件编译

条件编译和保留指令是一种重要且必不可少的指令类型,它们帮助我们根据一些条件来编译和运行程序或代码片段的特定部分,从而无缝地跳过程序中某些特定部分的编译。

语法

#ifdef name_of_your _macro_created
    statement 1;
    statement 2;
    statement 3;
    statement 4;
    statement 5;
    statement 6;
    statement 7;
    .
    .
    .
    statement n;
#endif

其他指令

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

C++ 代码

// Here we are writing down the simple C++ programming language code snippet
// To demonstrate the concept of C/C++ preprocessors concept with their code
// and relevant display output on the compiler
#include 
using namespace std;

void function_1_is();
void function_2_is();

#pragma startup function_1_is()
#pragma exit function_2_is()

void function_1_is()
{
    cout << "Hello coder on javaTpoint.com we are inside the 1st function created now\n";
}

void function_2_is()
{
    cout << "Hello coder on javaTpoint.com we are inside the 2nd function created now\n";
}
// the main driver code functionality starts from here
int main()
{
    void function_1_is();
    void function_2_is();
    cout << "Hello coder on javaTpoint.com we are inside the int main() function now\n";

    return 0;
}

输出:

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

C++ 代码

// Here we are writing down the simple C++ programming language code snippet
// To demonstrate the concept of C/C++ preprocessors concept with their code
// and relevant display output on the compiler
#include 
using namespace std;

void function_1_is();
void function_2_is();

void __attribute__((constructor)) function_1_is();
void __attribute__((destructor)) function_2_is();

void function_1_is()
{
    printf("hello coder we are currently inside the function_1_is()\n");
}

void function_2_is()
{
    printf("hello coder we are currently inside the function_2_is()\n");
}

// the main driver code functionality starts from here
int main()
{
    printf("hello coder we are currently inside the int main() function\n");

    return 0;
}

输出:

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()

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程