Introduction
We have already discussed the #define preprocessor directive in C. There are two special preprocessor directives in the C programming language for string operations.
These are:
- Stringizing operator (#)
- Token-Pasting operator (##)
Both these operators are mainly used to deal with strings. In this article, we will discuss both of them in great detail and also look into code examples of them.
The Stringizing Operator (#)
Definition
The stringizing operator (#) converts macro parameters to string literals without expanding the parameter definition. It is only used in conjunction with function-like macros (macros that take arguments). When a macro parameter is preceded by a leading '#,' the preprocessor replaces it with the literal text of the actual argument, converted to a string constant.
Syntax
The syntax of the stringizing operator (#) is as follows:
#define MACRO_NAME(param) #param
- #define: It is a preprocessor directive in C.
- MACRO_NAME: It represents the macro that has to be replaced by its value throughout the program.
- Param: It represents the parameter of the macro. We use the macro anywhere in the program and provide the value of the param between the braces () of the macro, which has to be converted to string literal.
- #: It is the Stringizing operator.
Example 1
// C program to demonstrate Stringizing operator (#)
#include <stdio.h>
// Stringizing operator #
// it converts the input into a String literal
#define STRINGIZING_OPERATOR(input) #input
int main()
{
printf(STRINGIZING_OPERATOR(Coding Ninjas));
printf("\n" STRINGIZING_OPERATOR(12345));
printf("\n" STRINGIZING_OPERATOR(CodingNinjas @ 12345));
return 0;
}
Output:
Coding Ninjas
12345
CodingNinjas @ 12345
Explanation
In C, we use the stringize operator to convert a token to a string. It converts the macro parameter to a string. The preprocessor turns the line printf(demoOfStringizingOperator(Coding Ninjas)); into printf(“Coding Ninjas”);
Example 2
// C program to demonstrate Stringizing operator (#)
#include <stdio.h>
// Stringizing operator #
// it converts the input into a String literal
#define DEMO1(val) #val
int main()
{
printf(DEMO1( Message without quotes));
// The above statement is equivalent to
printf("\nMessage without quotes");
return 0;
}
Output:
Message without quotes
Message without quotes
Example 3
// C program to demonstrate Stringizing operator (#)
#include <stdio.h>
// Stringizing operator #
// it converts the input into a String literal
#define DEMO2(val) #val
int main()
{
printf(DEMO2(DEMO2(Nothing will happen)));
// The above line will simply print DEMO2(Nothing will happen) because
// the stringizing operator does not expand the macros
return 0;
}
Output:
DEMO2(Nothing will happen)
Explanation
The macros passed as a value to the macro are not expanded. It will simply print the message as it is inside the macro.
Example 4
// C program to demonstrate Stringizing operator (#)
#include <stdio.h>
// Stringizing operator #
// it converts the input into a String literal
#define DEMO3(val) printf(#val)
int main()
{
DEMO3(I am an Indian);
printf("\n");
// This will print the message with the double quotes
DEMO3("I am a proud Indian");
return 0;
}
Output:
I am an Indian
"I am a proud Indian"
Also see, Short int in C Programming