C++ Preprocessor

Preprocessor directives are one of the unique features of C++. It provides many tools that other high-level languages do not, and programmers can use them to create efficient, easy-to-read, easy-to-modify, and portable C ++ programs.

Before the compiler compiles a C++ program, the source code is processed by the compiler. This technique is called preprocessor, and the process is called preprocessing. It is a separate program part that the C++ compiler invokes in the first part of the translation. This technique is not a part of the compiler but a separate method that comes under the compilation process. It directs the compiler that the information should be preprocessed before the actual compilation starts.

All preprocessor directives in C++ begin with #, and they do not need to end with a semicolon(;) because this is not a statement in C++.

The #define directive creates a symbolic constant, and these symbolic constants are called macro.

The general form of the directive is:

Syntax:

#define macro-name replacement-text

Simple Program of C++ Preprocessor Directive

Example:

#include<iostream> 
#define val 12

using namespace std;

int main()
{
    cout << "Value is :" << val << endl;
}

Different preprocessor directives perform different tasks. You can categorize these Preprocessor Directives as follows:

  • Inclusion Directives:
    • #include: specifies the files to be included, especially header files.
  • Macro Definition Directives:
    • #define: define a macro substitution.
    • #undef: It is used for undefining a macro.
  • Conditional Compilation Directives:
    • #if: It tests a compile-time condition.
    • #elif
    • #endif: It specifies the end of #if.
    • #ifdef: It is used to test for macro definition.
    • #ifndef: It tests whether a macro is not defined.
    • #else: It provides an alternative option when #if fails.
  • Other Directives:
    • #error
    • #line: Supplies a line number for compiler messages.
    • #pragma: It specifies implementation-defined instructions to the compiler.


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.