# and ## Operators in C - GeeksforGeeks

03 Dec, 2019
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 thisExamples :
The following preprocessor turns the line printf(mkstr(geeksforgeeks)); into printf(“geeksforgeeks”);// CPP program to illustrate (#) operator#include <stdio.h>#define mkstr(s) #sint main(void){    printf(mkstr(geeksforgeeks));    return 0;}Output:geeksforgeeks
In this program, value of a is replaced by macro.// CPP program to illustrate (#) operator#include <iostream>using namespace std;  #define a 8.3297  int main(){    cout << "Value of a is " << a << endl;      return 0;}Output:Value of a is 8.3297
This program finds out maximum out of two numbers using macro// CPP program to illustrate (#) operator#include <iostream>using namespace std;  #define MAX(i, j) (((i) > (j)) ? i : j)  int main(){    int a, b;      a = 250;    b = 25;      cout << "The maximum is " << MAX(a, b) << endl;      return 0;}Output:The maximum is 250

The following preprocessor turns the line printf(mkstr(geeksforgeeks)); into printf(“geeksforgeeks”);// CPP program to illustrate (#) operator#include <stdio.h>#define mkstr(s) #sint main(void){    printf(mkstr(geeksforgeeks));    return 0;}Output:geeksforgeeks

// CPP program to illustrate (#) operator#include <stdio.h>#define mkstr(s) #sint main(void){    printf(mkstr(geeksforgeeks));    return 0;}
Output:
geeksforgeeks

In this program, value of a is replaced by macro.// CPP program to illustrate (#) operator#include <iostream>using namespace std;  #define a 8.3297  int main(){    cout << "Value of a is " << a << endl;      return 0;}Output:Value of a is 8.3297

// CPP program to illustrate (#) operator#include <iostream>using namespace std;  #define a 8.3297  int main(){    cout << "Value of a is " << a << endl;      return 0;}
Output:
Value of a is 8.3297

This program finds out maximum out of two numbers using macro// CPP program to illustrate (#) operator#include <iostream>using namespace std;  #define MAX(i, j) (((i) > (j)) ? i : j)  int main(){    int a, b;      a = 250;    b = 25;      cout << "The maximum is " << MAX(a, b) << endl;      return 0;}Output:The maximum is 250

// CPP program to illustrate (#) operator#include <iostream>using namespace std;  #define MAX(i, j) (((i) > (j)) ? i : j)  int main(){    int a, b;      a = 250;    b = 25;      cout << "The maximum is " << MAX(a, b) << endl;      return 0;}
Output:
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##bint main(void){    int xy = 30;    printf("%d", concat(x, y));    return 0;}Output:30

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##bint main(void){    int xy = 30;    printf("%d", concat(x, y));    return 0;}Output:30

// CPP program to illustrate (##) operator#include <stdio.h>#define concat(a, b) a##bint main(void){    int xy = 30;    printf("%d", concat(x, y));    return 0;}
Output:
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.
shruti_sharma
C-Operators
cpp-operator
C Language
cpp-operator
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Multidimensional Arrays in C / C++
rand() and srand() in C/C++
Left Shift and Right Shift Operators in C/C++
fork() in C
Command line arguments in C/C++
Substring in C++
Function Pointer in C
Different methods to reverse a string in C/C++
TCP Server-Client implementation in C
Structures in C