What are #ifndef and #define Directives?

What are #ifndef and #define Directives

In C and C++ programming, #ifndef and #define are preprocessor directives used for conditional compilation. They are typically used together with #endif to include or exclude a block of code from compilation based on whether a certain condition is met.

The #ifndef directive checks if the given token has been #defined earlier in the C code. If the token has not been defined earlier then it includes the code between #ifndef and #else. If no #else is present then code between #ifndef and #endif is included.

The both #ifndef and #define directives are known as header guards or #include guard in C/C++. They are used to prevent multiple declarations of variables and other data types and header files from being included multiple times. The #ifndef also prevents recursive inclusions of header files. Another non-standard preprocessor directive named #pragma once is widely used to force the inclusion of a header file only once.

The syntax of using #ifndef is as below:

#ifndef MY_HEADER_FILE
#define MY_HEADER_FILE

// Your code here

#endif

In this example, the code between #ifndef MY_HEADER_FILE and #endif will be included only if the identifier MY_HEADER_FILE has not been defined before in the code.

The #ifndef Directive:

  • This directive checks if a particular identifier (usually a macro or a symbol) has not been defined previously in the code.
  • If the identifier has not been defined, the code following #ifndef will be included in the compilation; otherwise, it will be excluded.

The #define Directive:

  • This directive is used to define a macro or a symbol in the code.
  • In the context of conditional compilation, it is commonly used with #ifndef to create include guards to prevent header files from being included multiple times.

Use of #ifndef and #define

Here is a rather complete C program to demonstrate the use of #ifndef and #define.

#include <stdio.h>

#ifndef INTEREST_RATE
#define INTEREST_RATE 8
#endif

int main()
{
   printf("Current Interest Rate is %d percent.\n", INTEREST_RATE );

   return 0;
}

The output of the above C program is:

Current Interest Rate is 8 percent.

C program to show the use of #ifndef and #define

Here is a another C program to demonstrate the use of #ifndef and #define. As INTEREST_RATE is already defined so #ifndef does not execute.

#include <stdio.h>

#define INTEREST_RATE 9
#ifndef INTEREST_RATE
#define INTEREST_RATE 8
#endif

int main()
{
   printf("Current Interest Rate is %d percent.\n", INTEREST_RATE );

   return 0;
}

The output of the above program is:

Current Interest Rate is 9 percent.

The #ifndef and #define are commonly used in C and C++ to ensure that header files are included only once in a translation unit.

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post