Stringize (#) and Token pasting (##) Operators in C
In this article you will learn about # operator and ## Operator in C with its complete explanation including various sample programs on these operators. Generally, # operator is known as Stringize operator and ## operator is known as Token Pasting operator in C.
The Stringize operator is a preprocessor operator works as the corresponding actual argument to be enclosed in double quotation mark.
This # operator is used to convert some text into string without double quotes.
It sends commands to compiler to converts a token into string.
This operator # is used at macro definition or conjunction with #define directive.
Let's considered some examples to get it more clearly:
These are some example on Stringize operator given above helps you to sharpen your concept about Stringize operator.
After understanding these examples you can try another problem on Stringize operator by yourself.
Let's considered some examples to get it more clearly:
In sample problem 2. given above is little bit confusing.
Now I am clearing it, here you can see token pasting ## operator is used.
operandX is declared as int type and In the body of main() when X is passed in token() then it reached the place where operand##n is implemented and argument in token n at macro defnition is replaced by X and after merging operand and X with the help ## operator then it becomes operandX.
Hence, printf() method perform its action and print the value of operandX.
In sample problem 3. given above works as:
Firstly, it merge 10 and 20 with help of Token Pasting operator defined at macro definition with #define directive.
Then with the help of arithmetic operator + 20 is added after concatenating 10 and 20 ( it becomes 1020 ). And the result is 1090 (1020 + 70).
Conclusion:
Preprocessor Operators |
In C programming, there are two special preprocessor or operator are Stringize operator and Token pasting operator.
These both operator perform different task, the first operator Stringize converts argument into string and the second one Token pasting operator perform concatenation means merging of two or more argument in a single string.
Let's start learning these operators one by one:
Let's start learning these operators one by one:
STRINGIZE (#) OPERATOR
The Stringize operator is a preprocessor operator works as the corresponding actual argument to be enclosed in double quotation mark.
This # operator is used to convert some text into string without double quotes.
It sends commands to compiler to converts a token into string.
This operator # is used at macro definition or conjunction with #define directive.
Let's considered some examples to get it more clearly:
#include<stdio.h>
#define str(nm) #nm // Implementing Operator # at macro definition with #define directive.
int main()
{
printf(str(ComputerNotes4u)); //passing argument in str()
return 0;
}
ComputerNotes4u
#include<stdio.h>
#define str(nm) printf( #nm ) // Implementing Operator # with printf() at macro definition with #define directive.
int main()
{
str(Learn2Earn); // passing argument in str
return 0;
}
Learn2Earn
#include<stdio.h>
#define str(a,b) printf( #a "_" #b ) // Implementing two # operator with printf() at macro definition with #define directive.
int main()
{
str(Learn, Computernotes4u); // passing two argument in str
return 0;
}
Learn_Computernotes4u
These are some example on Stringize operator given above helps you to sharpen your concept about Stringize operator.
After understanding these examples you can try another problem on Stringize operator by yourself.
TOKEN PASTING (##) OPERATOR
The Token pasting operator is a preprocessor operator. This operator sends commands to compiler to merge or concatenate two or more tokens ( smallest individual unit of C program) into one string.
It is used at macro definition. This ## operator provides a way to concatenate actual arguments during macro expansion.
If a parameter in the replacement text adjacent to a ## operator, the parameter is replaced by the actual argument, ## operator and surrounding white space are removed and result is re scanned.
It is sometimes called merging or combining operator is used in the both object and function like macros.
Read more about What are Operators in C?
Let's considered some examples to get it more clearly:
#include
#define token(x,y) x##y // Implementing Operator ## at macro definition with #define directive.
int main()
{
int xy=2020;
printf("Concating x & y = %d",token(x,y));
return 0;
}
Concating x & y = 2020
#include
#define token(n) printf("Value of operandX is %d ", operand##n) // Implementing Operator ## at macro definition with #define directive.
int main()
{
int operandX=2021;
token(X);
return 0;
}
Value of operandX is 2021
In sample problem 2. given above is little bit confusing.
Now I am clearing it, here you can see token pasting ## operator is used.
operandX is declared as int type and In the body of main() when X is passed in token() then it reached the place where operand##n is implemented and argument in token n at macro defnition is replaced by X and after merging operand and X with the help ## operator then it becomes operandX.
Hence, printf() method perform its action and print the value of operandX.
#include<stdio.h>
#define merge(x, y) x##y
int main()
{
printf("Result = %d", merge(10,20) + 70);
return 0;
}
Result = 1090
In sample problem 3. given above works as:
Firstly, it merge 10 and 20 with help of Token Pasting operator defined at macro definition with #define directive.
Then with the help of arithmetic operator + 20 is added after concatenating 10 and 20 ( it becomes 1020 ). And the result is 1090 (1020 + 70).
Conclusion:
In this article you have thoroughly read about (Stringize) # operator and (Token Pasting) ## operator in C with its various sample problems on it to get it more clear on these operators. These both operators are also called Preprocessor operator.
If you are interested then also read:
If you find this article helpful then please share!
If you are interested then also read:
Read more about What is Data Types in C?
Tags: # operator in C, ## operator in C, Stringize operator, Token pasting operator, Preprocessor operators
Stringize () And Token Pasting () Operators In C - Computernotes4U >>>>> Download Now
ReplyDelete>>>>> Download Full
Stringize () And Token Pasting () Operators In C - Computernotes4U >>>>> Download LINK
>>>>> Download Now
Stringize () And Token Pasting () Operators In C - Computernotes4U >>>>> Download Full
>>>>> Download LINK