devxlogo

Using Preprocessor Directives in C++

Using Preprocessor Directives in C++

Preprocessors are programs that accept input and produce output that in turn becomes input for the next cycle. Preprocessor directives are statements in a program that are preceded by the hash sign (#). Such directives facilitate portability and conditional compilation of your source code. In this article we will explore the preprocessor directives in C++ with code examples where applicable.

What Are Preprocessor Directives?

When your source code is compiled, the preprocessor statements are evaluated first and if you have specified any header file using preprocessor statements, for example, those header files are included in your code before the compiler can start the process. Preprocessor directives provide support for inclusion of header files, macro expansions, conditional compilation, and line control. Note that the preprocessor directives are specified in a single line of code and they don’t need an ending semicolon.

#include

One common use of the preprocessor directive is in the inclusion of a header file. The #include preprocessor directive is used to tell the preprocessor to include the contents of the specified file directly in the source code, at the point where the directive appears. The code example below shows how the stdio.h (standard input output header file) is included in your code.

#include int main(void){    printf("Hello, world!
");    return 1;}

#define

This directive is used to specify constants?literals that contain values that cannot change in your source code. Here is an example:

#define MAX_LENGTH 100

The above preprocessor directive specifies a constant named MAX_LENGTH that contains the value 100. Now, you can use this constant to create arrays as shown in the code snippet below.

char arr[MAX_LENGTH];

The above statement defines a string array that can store a maximum of 100 characters.

#undef

This preprocessor directive works exactly the inverse of #define directive. The #undef directive fulfills the inverse functionality of #define. You can use this directive to remove previously defined constants. Here is an example.

#define MAX_LENGTH 100char firstarray[MAX_LENGTH];#undef MAX_LENGTH#define MAX_LENGTH 50char secondarray[MAX_LENGTH]; 

Note that you can also use the #undef directive to an identifier that has not been defined?this implies that the identifier is undefined.

#ifdef, #ifndef, #if, #endif, #else and #elif

These are also known as conditional directives?you can use them in your code to perform conditional compilation. MSDN states, “Preprocessor directives, such as #define and #ifdef, are typically used to make source programs easy to change and easy to compile in different execution environments. Directives in the source file tell the preprocessor to perform specific actions. For example, the preprocessor can replace tokens in the text, insert the contents of other files into the source file, or suppress compilation of part of the file by removing sections of text.”

As an example, the preprocessor directive #ifdef is used to perform conditional compilation if the constant specified is defined. Here is an example:

#ifdef INTEL//Code that would run only on Intel family of microprocessors.#endif

Macros

You can also use the #define preprocessor directive to define a macro as shown in the code snippet below:

#include using namespace std;#define MIN(x,y) (((x)

#line

The #line preprocessor directive is used to provide line numbers for compiler messages. This preprocessor directive enables you to modify the compiler's line number for errors and warnings.

#pragma

The #pragma preprocessor directive in C++ is another interesting directive that you can use to access compiler-specific preprocessor extensions. A nice feature of this directive is that you can ensure that a header file is included only once by the compiler, irrespective of the number of times it has been imported.

Suggested Reading

https://gcc.gnu.org/onlinedocs/cpp/

Summary

Preprocessor directives help you to facilitate changing source code seamlessly and also compile the source code in different execution environments easily. In this article we presented a discussion on preprocessor directives in C++ and how they can be implemented.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist