Stringizing operator (#)
This operator causes the corresponding actual argument to be enclosed in double quotation marks. The # operator, which is generally called the stringize operator, turns the argument it precedes into a quoted string. For more on pre-processor directives – refer this
Examples :
- The following preprocessor turns the line printf(mkstr(geeksforgeeks)); into printf(“geeksforgeeks”);
// CPP program to illustrate (#) operator#include <stdio.h>#define mkstr(s) #sintmain(void){printf(mkstr(geeksforgeeks));return0;}chevron_rightfilter_noneOutput:
geeksforgeeks
- In this program, value of a is replaced by macro.
// CPP program to illustrate (#) operator#include <iostream>usingnamespacestd;#define a 8.3297intmain(){cout <<"Value of a is "<< a << endl;return0;}chevron_rightfilter_noneOutput:
Value of a is 8.3297
- This program finds out maximum out of two numbers using macro
// CPP program to illustrate (#) operator#include <iostream>usingnamespacestd;#define MAX(i, j) (((i) > (j)) ? i : j)intmain(){inta, b;a = 250;b = 25;cout <<"The maximum is "<< MAX(a, b) << endl;return0;}chevron_rightfilter_noneOutput:
The maximum is 250
Token-pasting operator (##)
Allows tokens used as actual arguments to be concatenated to form other tokens. It is often useful to merge two tokens into one while expanding macros. This is called token pasting or token concatenation. The ‘##’ pre-processing operator performs token pasting. 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.
Examples :
- The preprocessor transforms printf(“%d”, concat(x, y)); into printf(“%d”, xy);
// CPP program to illustrate (##) operator#include <stdio.h>#define concat(a, b) a##bintmain(void){intxy = 30;printf("%d", concat(x, y));return0;}chevron_rightfilter_noneOutput:
30
Application: The ## provides a way to concatenate actual arguments during macro expansion. If a parameter in the replacement text is adjacent to a ##, the parameter is replaced by the actual argument, the ## and surrounding white space are removed, and the result is re-scanned.
This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Operators in C | Set 2 (Relational and Logical Operators)
- Operators in C | Set 1 (Arithmetic Operators)
- What are the differences between bitwise and logical AND operators in C/C++?
- What are the operators that can be and cannot be overloaded in C++?
- Difference between Increment and Decrement Operators
- Left Shift and Right Shift Operators in C/C++
- Increment (Decrement) operators require L-value Expression
- Order of operands for logical operators
- Advanced C++ | Conversion Operators
- const_cast in C++ | Type Casting operators
- Bitwise Operators in C/C++
- How to sum two integers without using arithmetic operators in C/C++?
- Execution of printf with ++ operators
- C | Operators | Question 2
- C | Operators | Question 3
- C | Operators | Question 4
- C | Operators | Question 5
- C | Operators | Question 6
- C | Operators | Question 7
- C | Operators | Question 8
Improved By : shruti_sharma

