# And ## operators in c

679 views 9 slides Jan 29, 2021
Slide 1
Slide 1 of 9
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9

About This Presentation

# And ## operators in c


Slide Content

# and ## Operators in C

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) #s int 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

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 );

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##b int 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.

# and ## Operators in C ? In this section we will see what are the Stringize operator(#) and Token Pasting operator(##) in C. The Stringize operator is a preprocessor operator. It sends commands to compiler to convert a token into string. We use this operator at the macro definition. Using stringize operator we can convert some text into string without using any quotes. Example # include< stdio.h > # define STR_PRINT(x) #x main () {     printf (STR_PRINT(This is a string without double quotes)); } Output This is a string without double quotes

The Token Pasting operator is a preprocessor operator. It sends commands to compiler to add or concatenate two tokens into one string. We use this operator at the macro definition. Example  Live Demo #include< stdio.h > # define STR_CONCAT(x, y) x##y main () {     printf ("%d", STR_CONCAT(20, 50)); } Output 2050
Tags