C/C++ Preprocessor Directives | Set 2

C/C++ Preprocessor Directives | Set 2

In C and C++, preprocessor directives are lines of code that start with a # symbol and are processed by the preprocessor before the actual compilation of the program. These directives are not terminated by semicolons like regular C/C++ statements. They are used for various purposes, such as

Types of preprocessor directives are given below:

  1. Conditional Compilation
  2. Line control
  3. Error directive
  1. Conditional Compilation: The #ifdef directive is used to conditionally include a block of code in the preprocessor output based on whether a macro name is defined. The #ifdef directive checks if a macro is defined. If the macro is defined, the code within the conditional block is included in the preprocessor output. If the macro is not defined, the code within the block is skipped. The #ifndef directive is the opposite of #ifdef. It checks if a macro is not defined. If the macro is not defined, the code within the conditional block is included in the preprocessor output. The #if directive allows you to conditionally include code based on an expression that evaluates to either true or false. If the expression is true, the code is included; otherwise, it’s skipped.
ifndef macro_name
    statement1;
    statement2;
    statement3;
    .
    .
    .
    statementN;
endif

Whereas, a macro with the name ‘macro name’ has not been defined using the #define directive. Therefore, only the block of statements will be executed.

  • #if: The #if directive allows you to specify a condition, and if that condition evaluates to a non-zero value (true), the code block immediately following #if is executed. If the condition is false (evaluates to zero), the code block is skipped.
  • #elif (short for “else if”): The #elif directive provides an alternative condition to be checked if the preceding #if or #elif condition is false. If the #elif condition evaluates to a non-zero value, the code block immediately following #elif is executed.
  • #else: The #else directive is used to specify a block of code that is executed when none of the previous conditions (specified in #if or #elif) are met. In other words, if all previous conditions are false, the code block following #else is executed.

Syntax

#if macro_condition
   statements
#elif macro_condition
   statements
#else
   statements
#endif

Example

#include<iostream>
 
#define gfg 7
  
#if gfg > 200
   #undef gfg
   #define gfg 200
#elif gfg < 50
   #undef gfg
   #define gfg 50
#else
   #undef gfg
   #define gfg 100
#endif
 
int main()
{
    std::cout << gfg;  // gfg = 50
}    

Output

50

2. Line control ( #line ): The #line directive in C and C++ allows you to control error reporting by specifying the filename and line number associated with error messages. It is useful for custom error reporting and code generation tools.

Syntax

#line number "filename"

The #line directive in C and C++ is used to set the line number for the next code line and can also specify an optional filename to be displayed in error messages. The line numbers of successive lines are increased one by one from this point on, and the “filename” parameter is used to redefine the file name shown in error messages.

3. Error directive ( #error ):The #error directive is used in C and C++ to intentionally halt the compilation process and generate an error message, which can be optionally specified as a parameter. It is often used for creating custom error messages or for controlling compilation based on specific conditions.

Syntax:

#error optional_error

Optional_error is an error that is specified by the user and will be displayed when the directive is found in the program. Example:

Output

error: #error Skill Vertex not found !

FAQ- C/C++ Preprocessor Directives | Set 2

Q1. What are the types of C++ preprocessing directives?

Ans. In C++, preprocessor directives serve various purposes, including defining macros, including files, controlling compilation, handling errors, and more. They are processed before the program’s actual compilation.

Q2. What is #ifdef and #endif in C++?

Ans. #ifdef is used to check if a symbol is defined. If the specified symbol is defined, the code enclosed between #ifdef and #endif is included in the preprocessor output and compiled.
#if is used to evaluate a Boolean expression. If the expression following #if evaluates to true, the enclosed code is included in the preprocessor output and compiled.

Q3.What is a preprocessor directive and its types?

Ans. Preprocessor directives in programming languages, such as C and C++, are lines that start with the # character. They are not part of the standard source code but are instructions for the compiler to process certain aspects of the program before actual compilation. Preprocessor directives are used for tasks like macro definitions, conditional compilation, including header files, and other preprocessing tasks to prepare the code for compilation.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment