
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - if...else if Ladder
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Predefined Identifier __func__
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Function Pointers
- C - Array of Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- C - Flexible Array Members in Structures
- C - Structures vs Unions
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Layout
- C - Memory Management
- C - Memory Address
- C - Memory Leaks
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
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, youll 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