# and ## Operators in C
The stringizing operator (#) is a preprocessor directive that encloses the actual argument in double quotation marks. This operator, also called the # operator, transforms the preceding input into a string enclosed in quotes. Typically associated with macros in the C programming language, it is commonly referred to as the stringification operator.
Syntax:
It has the following syntax:
#define mkstr(s) #s
Example:
// Program implement the use of # operator in C
#include <stdio.h>
// the definition of the macro in c
#define mkstr(s) #s
int main(void)
{
// display of the value
printf(mkstr(Programming));
return 0;
}
Output:
Programming
Explanation:
- The mkstr macro is invoked with the parameter Programming.
- The macro's # operator turns Programming into the character literal "Programming".
- After that, the printf function writes the string "Programming" to the terminal.
As a consequence, upon execution of an application, the text "Programming" is displayed on the terminal. The # operator within the macros definition converts the argument's value into a string literal, allowing the text to be shown as a string.
What is the token-pasting operator (##)?
When employing tokens as actual parameters, they can be merged together through the Token-pasting operator (##). When expanding macros, it is often beneficial to unite two tokens as a single entity. This process is known as token pasting or token concatenation.
Token pasting is achieved using the '##' pre-processing operator in C programming. When a macro is expanded, the '##' operator merges the two adjacent tokens into one, effectively combining them and replacing the '##' along with the original tokens within the macro expansion.
Syntax:
It has the following syntax:
// the macro header using ##
#define concat(a, b) a##b
Example:
// Program to implement the ## operator in C
#include <stdio.h>
// the macro header using ##
#define concat(a, b) a##b
int main(void)
{
//The combined value
int mn = 39;
// The display of the concatenated values
printf("%d", concat(m, n));
return 0;
}
Output:
Explanation:
- A macro concat(a, b) that concatenates a and b without any spaces in between using the ## operator. After that, defined a variable of type integer mn with the value 39 in the main function.
- The printf command concatenates m and n using the concat macro, producing the resultant token mn. It essentially replaces concat(m, n) with mn.
- When the code is performed and printf is called, the current value of the variable mn will be shown, which is 39, because mn was defined as an integer containing an integer value of 39.
Advantages of # and ## Operators:
There are numerous benefits associated with the # and ## operators. Some key advantages of these operators include:
# Operator
- Debugging and Logging: It is extremely useful for debugging and logging . For example, it can be used to transform macro parameters into strings that can be indicated for debugging purposes.
- General Macros: It allows for the design of more general macros that interact with various kinds of values, transforming them to string representation for specified operations.
- Creating New Identifiers: It enables the creation of new identifiers or symbols by merging previously used ones. It is very useful for dynamically creating variable or function names.
- Code Reusability: It helps in the creation of more generic and reusable code using macros. For example, similar functions or structures must be produced under different names in data structure implementations.