• C Programming Video Tutorials

Preprocessor Operators in C



Preprocessor operators are special symbol(s) that are used in the context of the #define directive. These operators are also called preprocessor-specific operators.

In C, a set of preprocessor operators have been defined, each with an important operation attached with it. In this chapter, we will explain the different types of preprocessor operators used in C.

The following preprocessor operators are implemented by most of the modern C compilers −

Operator Action
Continuation operator (/) Used to continue a macro that is too long for a single line.
Stringizing operator (#) Causes the corresponding actual argument to be enclosed in double quotation marks
Token-pasting operator (##) Allows tokens used as actual arguments to be concatenated to form other tokens
defined operator Simplifies the writing of compound expressions in certain macro directives

Let us now discuss in detail about each of these preprocessor operators.

Continuation Operator (/)

This operator is used where the macro is quite complex, and spreads over multiple lines. In case of a complex logic inside macro expansion, you’ll need to break the line and write code that spreads over more than one lines. In such cases macro continuation operator is very helpful.

Example 1: Preprocessor Continuation Operator (/)

In the example below, we are writing a part of the macro in the next line, so we are making use of macro continuation preprocessor operator (\).

#include <stdio.h>

#define  message() { \
   printf("TutorialsPoint Library contains\n"); \
   printf("High quality Programming Tutorials"); \
}

int main() {

   message();
   return 0;
}

Output

When you run this code, it will produce the following output −

TutorialsPoint Library contains
High quality Programming Tutorials

Example 2: Preprocessor Continuation Operator (/)

In the following example, the macro definition involves evaluation of a switch case statement, hence it spreads over multiple lines, requiring the macro continuation character.

#include <stdio.h>

#define SHAPE(x) switch(x) { \  
   case 1: printf("1. CIRCLE\n"); break; \  
   case 2: printf("2. RECTANGLE\n"); break; \  
   case 3: printf("3. SQUARE\n"); break; \  
   default: printf("default. LINE\n"); \  
} 

int main() {

   SHAPE(1);
   SHAPE(2);
   SHAPE(3);
   SHAPE(0);
   return 0;
}

Output

When you run this code, it will produce the following output −

1. CIRCLE
2. RECTANGLE
3. SQUARE
default. LINE

Stringizing Operator (#)

Sometimes you may want to convert a macro argument into a string constant. The number-sign or "stringizing" operator (#) converts macro parameters to string literals without expanding the parameter definition. This operator may be used only in a macro having a specified argument or parameter list.

Example 1: Stringizing Operator

Take a look at the following example −

#include <stdio.h>
#define stringize(x) printf(#x "\n")

int main() {

   stringize(Welcome To TutorialsPoint);
   stringize("The Largest Tutorials Library");
   stringize("Having video and Text Tutorials on Programming Languages");
}

Output

When you run this code, it will produce the following output −

Welcome To TutorialsPoint
"The Largest Tutorials Library"
"Having video and Text Tutorials on Programming Languages"

Example 2: Stringizing Operator

The following code shows how you can use the stringize operator to convert some text into string without using any quotes.

#include <stdio.h>
#define STR_PRINT(x) #x

main() {
   printf(STR_PRINT(This is a string without double quotes));
}

Output

Run the code and check its output −

This is a string without double quotes

Token Pasting Operator (##)

The double-number-sign or token-pasting operator (##), which is sometimes called the merging or combining operator. It is often useful to merge two tokens into one while expanding macros.

When a macro is expanded, the two tokens on either side of each "##" operator are combined into a single token, which then replaces the "##" and the two original tokens in the macro expansion.

Example 1: Token Pasting Operator (##)

Take a look at the following example −

#include <stdio.h>

#define PASTE(arg1,arg2) arg1##arg2

main() {

   int value_1 = 1000;
   printf("value_1 = %d\n", PASTE(value_,1));
}

Output

When you run this code, it will produce the following output −

value_1 = 1000

Example 2: Token Pasting Operator (##)

In the example below, we pass two arguments to the macro.

#include <stdio.h>
#define TOKENS(X, Y) X##Y

int main() {

   printf("value1: %d\n",TOKENS(12,20));
   printf("value2: %d\n",TOKENS(12,20)+10);  
   return 0;
}

Output

When you run this code, it will produce the following output −

value1: 1220
value2: 1230

The defined Operator

The defined preprocessor operator can only be used as a part of #if and #elif directives. The syntax of defined operator is as follows −

#if defined(macro1)   
// code
#elif defined(macro2)   
// code
#endif  

It is used in constant expressions to determine if an identifier is defined. If the specified identifier is defined, the value is true (non-zero). If the symbol is not defined, the value is false (zero).

Example 1: defined Operator

In this example, the defined operator is used to check if the DEBUG macro is defined. If it is, the program prints "DEBUG mode is on." Otherwise, it prints "DEBUG mode is off."

#include <stdio.h>

#define DEBUG 1

int main() {

   #if defined(DEBUG)
   printf("DEBUG mode is on.\n");
   #else
      printf("DEBUG mode is off.\n");
   #endif

   return 0;
}

Output

Run the code and check its output −

DEBUG mode is on.

Example 2: defined Operator

The following code checks if the square macro has been already defined, and if so, expands it with the given value of "x" as 5.

#include <stdio.h>

#define square(x) ((x) * (x))

int main(){

   #if defined square
   printf("The square of the given number is: %d", square(5));
   #endif

   return 0;
}

Output

When you run this code, it will produce the following output −

The square of the given number is: 25
Advertisements