C Programming

#ifdef, #ifndef, ## C Preprocessor

Several preprocessor commands exist in the C language. The #define and #undef commands are the most important preprocessor commands present in the C language. Each of them has some unique features. In this topic, we will discuss in detail about #ifdef, #ifndef, and ## commands.

The Concept of Preprocessor

Before we discuss the #ifdef, #ifndef, and ## commands, we have to know about the preprocessor.

A set of programs executed before our program is compiled, and it is called a preprocessor. The main function of the preprocessor is only to observe the # symbolic statements. The rest of the lines remains unchanged by the processor. This # has a unique name in the C language: the preprocessor directive. The function of this preprocessor directive is to give a direction to that particular statement. The # is followed by the preprocessor command.

Preprocessor Diagram

The preprocessor resolves all the # lines. It means there is no line left with # that is compiled by the compiler.

Now, we will discuss about the #ifdef, #ifndef, and ## commands in detail. These listed preprocessor commands are:

Preprocessor Commands

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

Programming Example 1

In this programming example, we will see the application of the preprocessor command #if, #else.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

#define COUNTRY India

int main ()

{

#if COUNTRY == Pakistan     // using of # if command.
    Printf ("Pakistani Rupees \n");
#elif COUNTRY == bangladesh
    printf ("Taka \n");
#elif COUNTRY == nepal
    printf ("Nepali Rupees \n");
#else                   // using of # else command.
    Printf ("Indian Rupees \n");

#endif // COUNTRY
return 0 ;
}

Output

Explanation

In that particular programming language, we give an example of #if, #else. The #if does not need any () parenthesis. It is used to execute the only true statement. Otherwise, we will use #elif to set different types of conditions in the programming code. When we use the if true condition, then no other condition will be checked and it will print the right condition in our monitor.

For the #if, it is determined whether this line is complied or not. #elif is as good as else if.

Programming Example 2

Here, we will see another example of the #if, #else command:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>

#define A 100 // preprocessor command is used here.

#define B 0

#define C 10

int main(void)

{
    int a = 100, b = 0, c = 10;

#if A // # if statement is used here.

Printf(“Value of A is %d\n”, A);

#elif B

printf(“Value of B is %d\n”, B);

#else // # else statement is used here.

Printf(“Value of C is %d\n”, C);

#endif

return 0;

}

Output

Explanation

In this programming example, the #if and #else are used. Earlier, we said that these commands are different from the normal if-else used in the program. This command is executed by the processor and executes before compilation. But only the true statement is executed and ready to compile.

1
#ifdef, #ifndef Macros

#ifdef is a macro. If any macro is contained to a #define statement, then the rest of the statement has to be compiled by the compiler.

#ifndef is also a macro like #ifdef.

Programming Example 3

In this programming example, we will see the application of preprocessor command, #ifdef, #ifndef:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

#define COUNTRY "India"

int main ()

{

#ifdef COUNTRY      // using of # ifdef command.
    printf ("%s is a great country", COUNTRY);

#endif              // COUNTRY

#ifndef COUNTRY     // using of # ifndef command.
    printf ("I love my nation");
#endif // COUNTRY
return 0 ;
}

Output

Explanation

In this particular programming, we give an example of #ifdef, #ifndef. The #if and #else are different from normal if else that are used normally in the C language. They are executed by the preprocessor to decide which condition will be ready for compilation. It is used to execute only true statement. Otherwise, we will use #elif to set different types of condition in the programming code. When we use this if true condition, no other condition will be checked and it will print the right condition in our monitor.

#ifdef actually uses if macro is defined with any character sequence. If macro is not defined, then #ifndef executes. Here, a macro is used in the program named country. The macro country is defined with the character sequence named “India”. For this, #ifdef executes in this program.

Programming Example 4

In this programming example, we will see another application of preprocessor command #ifdef, #ifndef.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

int main()

{

int x=0;

#ifdef NOINPUT // using of # ifdef command.

#else

printf("Enter value of the variable  x:");

scanf("%d", &x);

#endif

printf("Value of x is: %d\n", x);

return 0 ;

}

Output

Explanation

In that particular programming, we give an example of #ifdef, #ifndef. As I mentioned earlier, #ifdef actually uses if macro is defined with any character sequence. If macro is not defined, then #ifndef executes. Here, a macro is used in the program named NOINPUT. So, it executes and ready for compilation.

## Operator

The ## is also an operator which is resolved by the preprocessor. With the help of ## operator, we can concatenate two statements or two strings easily.

Programming Example 5

In this programming example, we will see the application of preprocessor command ## operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

#define ACTION(a,b) a##b+a*b

int main ()

{

printf ("%d", ACTION(3,4));

return 0 ;

}

Output

Explanation

In this programming example, we use ## operator to concatenate two strings. This operator is also executed by the processor. It actually concatenates two strings.

Here, the expression is given ACTION (3, 4), and it is replaced by a##b + a *b = 34 + 3 * 4 = 34 + 12 = 46.

## concatenate the two operands.

Conclusion

In this article, we discussed the processor and the ready-for-compilation and execute preprocessor commands by the compiler. These previously discussed preprocessor commands are very important for the complier, which things it will be compiled. Based on these things, our program will be executed.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com