Ifndef in C
Introduction
Ifndef is an example of a directive in C that checks to see whether the specified macro has been defined. It is a conditional inclusion. In C, a directive is a distinct instruction from the user to the compiler. At the same time, a macro is a particular kind of directive that allows the user to provide a name for a program element (which in turn may include an actual, variable, function, expression, et cetera), so the programmer may utilize the given name rather than the actual code.
Ifndef will include a guard. That serves as a define command in C that determines whether or not a MACRO has already been defined. This will set up the MACROs and additional statements when it still needs to do so. This is utilized during conditional compilation.
- If not defined is usually referred to as "ifndef," therefore continue till endif.
Syntax
#ifndef <macro_definition>
... some code statements
#endif
Endif is the correct method for ending it.
How does #ifndef Work in C?
Using the preprocessor directives in the programming language C, the user can give the compiler particular directions before starting the program. The two most common kinds of directives are condition inclusions and macro definitions.
- The macro definition involves determining a macro, a substitute for the program's code.
- When the compiler comes upon a macro, it replaces it with the name of the portion of code for where it was defined or defines it.
The #define, and #undef directives can be used to define and undefine macros, respectively. The developer can only utilize an instruction in an undertaking if they are sure of its definition because they're afraid of compilation. Ifndef is a member of the condition inclusions that can be used to solve this problem.
Example
Using this illustration, we'll see an easy application of ifndef for creating a macro called DATA in C code. Because the DATA macro has become undefined, it will be defined.
#include <stdio.h>
#ifndef VAR
#define VAR "Example."
#endif
int main()
{
printf (VAR);
return 0;
}
Output
![Ifndef in C](https://static.tutorialandexample.com/c/ifndef-in-c1.png)
- You will encounter the following compilation time problem when we do not end using the endif:
Output
![Ifndef in C](https://static.tutorialandexample.com/c/ifndef-in-c2.png)
The ifndef block also allows us to define a global variable:
Example
#include <stdio.h>
#ifndef DATA
#define VAR "Example"
int global_x = 3;
#endif
int main()
{
printf("%s\n", VAR);
printf("%d", global_x);
return 0;
}
Output
![Ifndef in C](https://static.tutorialandexample.com/c/ifndef-in-c3.png)
Example 2
This is another C program to demonstrate how to use #ifndef and #define. Because INTEREST was previously declared, #ifndef is not executed.
#include <stdio.h>
#define INTEREST 11
#ifndef INTEREST
#define INTEREST 9
#endif
int main ()
{
printf("Current Interest is %d percent.\n", INTEREST );
return 0;
}
Output
![Ifndef in C](https://static.tutorialandexample.com/c/ifndef-in-c4.png)
Use of #ifndef and #define
This is another example of a C program utilizing #ifndef and #define.
Example
#include <stdio.h>
#ifndef INTEREST
#define INTEREST 13
#endif
int main()
{
printf("Present Interest is %d percent.\n", INTEREST );
return 0;
}
Output
![Ifndef in C](https://static.tutorialandexample.com/c/ifndef-in-c5.png)
Application
Tracking if def is frequently used might be challenging, as in big codebases.
- A compilation error will result when we redefine a macro. Look at the code in the following section, where we declare the VAR macro twice.
- MACROs are platform-specific since they can be utilized in a specific system if several prerequisites are met, like as
- runtime libraries
- operating systems.
- Due to this, code may compile in one environment and fail in another in such situations. As a result, utilize ifndef to guarantee your code works in any situation.
Restrictions
While utilizing #ifdef and #ifndef statements, keep the following limitations in mind:
• If there are #ifdef and #ifndef instructions on a line, comments cannot be placed there.
• Avoid using a #ifdef or #ifndef statement when checking a variable with 0 (zero).
• A #ifdef or #ifndef statement is limited to testing numeric constants.
Conclusion
- Fndef is a C++ directive that checks to see if a given macro is referred to. It is a conditional included in the C programming language.
- In C, an instruction is a special instruction user gives a compiler, and macros are a specific sort of directive that enables the user to name a specific portion of the program.
- When a macro isn't specified, the Ifndef may recognize it because it may be divided into "if not defined" and "if not defined."
- When the macro written before indef is not defined, the source code between indef and a single among the elif, else, or endif conditions inclusions goes to compilation.