C Programming

#ifndef Directive in C

The C programming language contains certain pre-processor directives. A pre-processor directive is executed even before the execution of the “main()” function. “#ifndef” is also a very important C pre-processor directive, and this article will guide you about the usage of this pre-processor directive in C in Ubuntu 20.04.

What is #ifndef Directive in C in Ubuntu 20.04?

The “#ifndef” directive is a conditional pre-processor directive of the C programming language, i.e., it is used to provide two different execution paths depending upon the provided conditions. It stands for “if not defined”. This directive always operates on a variable that is either defined or not defined. If the specified variable is not defined already, then this particular directive and its related sections of the code will be executed; otherwise, the alternate execution path is taken. Moreover, this directive is always closed with the “endif” statement at the end. A sudo-code explaining the basic syntax of this pre-processor directive is shown below:

#ifndef Macro
//code
#else
//code
#endif

Examples of using the #ifndef Directive in C in Ubuntu 20.04:

To understand the usage of the “#ifndef” directive in the C programming language in Ubuntu 20.04, you will have to look at the following two examples that are implemented to elaborate the concept of this C directive well.

Example # 1:

In this example, we simply wanted to assign a value to a test variable depending upon the definition of another variable, i.e., if the latter variable is already defined, then the former one will be assigned one value, otherwise, the other. You will be able to grasp this example clearly by looking at the C program below:

In this C code, we have first included the “stdio.h” library that will be required to run the necessary built-in functions within our code. Then, we have defined a variable named “Sample” by using the “define” keyword. After that, we have our “main()” function that will contain the actual code. Within the body of this function, we have declared a variable “x” without assigning it any value. Then we have the “ifndef” conditional statement that will operate on the “Sample” variable, i.e., this part of our code will be executed only in the case when the “Sample” variable will not be defined. In this case, the “x” variable will be assigned the value “4”. Otherwise, the alternate execution path, which is the “else” statement, will be executed, i.e., if the “Sample” variable is already defined, then this path will be taken. In this case, we will ask the user to enter any desired value for the “x” variable.

After taking this value as input, we have simply closed the “ifndef” statement with the “endif” statement. Finally, we wanted to print the value of the “x” variable on the terminal.

After saving our C code, we have compiled it with the following command:

$ gcc Example.c –o Example

Here, “gcc” is the name of the compiler that we are using, “Example.c” is the name of our C program file, whereas “Example” refers to the name of the object file that will be created as a result of the compilation of this code.

After compiling our C code successfully, we can execute it with the command shown below:

$ ./Example

Now, in our case, since we had already defined the “Sample” variable in our code, therefore, the “else” statement was executed, because of which the user was prompted to enter any desired value for the “x” variable. We have entered “5” as an input, as shown in the following image:

As soon as we hit the Enter key after entering this value, the value of the variable “x” was displayed on the terminal as shown in the image below:

Now, we will tweak the same C code used above slightly so that it is compelled to execute the “ifndef” statement. For that, we have simply removed the definition of the “Sample” variable, i.e., we have removed the “#define Sample” statement from our code as shown in the following image:

After making this change, we compiled and executed our C code in the same manner as we did above, and this time, the value of the variable “x” turned out to be “4” i.e., the value that was assigned to it within our code. In this case, no input was taken from the user because the “ifndef” path was followed. This changed output is shown in the image below:

Example # 2:

The basic gist of this example is more or less the same as our first example; however, it simply presents a different scenario to bring more clarity in the understanding of the usage of the “ifndef” C directive. In this example, we just want to print the ticket number if it already exists; otherwise, we simply want to notify the user that no ticket exists. For that, we have written the following C program:

Again, in this program, we have first included the “stdio.h” library. After that, we have defined a variable named “TicketNum” with a value “26” by using the “define” keyword. Then, within the body of our “main()” function, we have the “ifndef” statement that is supposed to operate on the “TicketNum” variable. When this execution path is taken, then that will mean that no ticket exists. On the other hand, we have an “else” statement that will be executed only when the “TicketNum” variable is already defined. In this case, the value assigned to the “TicketNum” variable will simply be printed on the terminal. Finally, we have closed the “ifndef” statement with the “endif” statement.

After saving this C program, we compiled and executed it in the same way we shared with you in our first example. The output of this code turned out to be “26” since the “else” part of the code was executed because the “TicketNum” variable was already defined.

Now, we will tweak our same C code used above slightly in a way that it is compelled to execute the “ifndef” statement. For that, we have simply removed the definition of the “TicketNum” variable, i.e., we have removed the “#define TicketNum 26” statement from our code as shown in the image below:

After making this change, we compiled and executed our C code in the same manner as we did above, and this time, the output of our code turned out to be “No ticket exists” because the “ifndef” path was followed. This changed output is shown in the image below:

Conclusion:

In this guide, we talked about the “ifndef” pre-processor directive of the C programming language. First, we explained the purpose of this directive, followed by its basic syntax. Then, we explained to you the usage of this pre-processor directive by sharing two different examples containing C programs implemented on a Ubuntu 20.04 system. Hopefully, after going through these examples, you will easily be able to grasp the concept of using this pre-processor directive in C in Ubuntu 20.04.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.