Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax
3.
How does #ifndef directive works?
4.
Example
5.
FAQs
5.1.
What are Pre-Processors?
5.2.
What is #ifndef?
6.
Conclusion 
Last Updated: Mar 27, 2024
Easy

C #ifndef

Author Nishant Rana
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

C++ is a powerful programming language for your competitive programming. To get flawless control in this language, you can refer to this amazing article on Coding Ninjas Studio. What are preprocessors? Preprocessors are powerful tools which help in the inclusion of header files, line control, conditional compilation, and macro expansion. In C/C++ we have a lot of preprocessors. In this blog, we will be looking at the #ifndef preprocessor.

The #ifndef preprocessor directive checks for the conditional compilation. It checks if the macro is defined by #define or not. If the macro is not defined by #define, then it executes the code. If it is defined by #define, then the #else is executed if present.

Also see: C Static Function, and Tribonacci Series

Syntax

#ifndef MACRO  
//code  
#endif  

Syntax with #else:

#ifndef MACRO  
//successful code  
#else  
//else code  
#endif  
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

How does #ifndef directive works?

This directive is used to check if a particular identifier is defined or not. Hence, it works as a conditional compilation in the C language. It helps to check the #ifdef’s opposite condition. It will return True if the specified identifier is not defined or removed else it will return False.

Example

#include <stdio.h>

#define YEARS_OLD 18
#ifndef YEARS_OLD
#define YEARS_OLD 15
#endif

int main()
{
   printf("One can vote if one's age is over %d years old.\n", YEARS_OLD);

   return 0;
}

Output:

One can vote if one's age is over 18 years old.
On removing the line #define YEARS_OLD 18, the output will be:-
One can vote if one's age is over 15 years old.

You can also read about dynamic array in c.

FAQs

What are Pre-Processors?

These are the set of lines that directs the compiler to do the pre-computation before the actual compilation.

What is #ifndef?

The #ifndef preprocessor directive checks for the conditional compilation. It checks if the macro is defined by #define or not.

Conclusion 

In this article, we have extensively discussed the following things:

  1. We first discussed what are C #ifndef.
  2. Then we saw an example of C #ifndef.

We hope that this blog has helped you enhance your knowledge regarding C #ifndef and if you would like to learn more, check out our articles here. Do upvote our blog to help other ninjas grow. Happy Coding!








 

Previous article
C #ifdef
Next article
C #if
Live masterclass