Table of contents
1.
Introduction
2.
The Stringizing Operator (#)
2.1.
Definition
2.2.
Syntax
2.3.
Example 1
2.4.
Example 2
2.5.
Example 3
2.6.
Example 4
3.
The Token-Pasting Operator (##)
3.1.
Definition
3.2.
Syntax
3.3.
Example 1
3.4.
Example 2
3.5.
Example 3
3.6.
Example 4
3.7.
Application
4.
Frequently Asked Questions
4.1.
The last parameter in Func delegate represents?
4.2.
In which namespace Func delegate is specified?
4.3.
How many parameters can a Func delegate accept?
4.4.
Can Func delegate be used with an anonymous function?
4.5.
Can Func delegate be used with lambda expression?
5.
Conclusion
Last Updated: Oct 28, 2024
Medium

Stringizing and Token-Pasting Operator

Author ANKIT KUMAR
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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
You can also try this code with Online C Compiler
Run Code
  • #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;
}
You can also try this code with Online C Compiler
Run Code

Output: 

Coding Ninjas
12345
CodingNinjas @ 12345
You can also try this code with Online C Compiler
Run Code

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;
}
You can also try this code with Online C Compiler
Run Code

Output:

Message without quotes
Message without quotes
You can also try this code with Online C Compiler
Run Code

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;
}
You can also try this code with Online C Compiler
Run Code

Output:

DEMO2(Nothing will happen)
You can also try this code with Online C Compiler
Run Code

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;
}
You can also try this code with Online C Compiler
Run Code

Output:

I am an Indian
"I am a proud Indian"
You can also try this code with Online C Compiler
Run Code


Also see, Short int in C Programming

The Token-Pasting Operator (##)

Definition

The Token Pasting operator is also a preprocessor operator. It instructs the compiler to add or concatenate two tokens into a single string. This operator is used in the macro definition.

When expanding macros, it is frequently useful to combine two tokens into one. This is known as token concatenation or token pasting. Token pasting is performed by the '##' pre-processing operator. When a macro is expanded, the two tokens on either side of each '##' operator are combined to form a single token that replaces the '##' and the two original tokens in the macro expansion.

The token pasting (##) operator simply removes any surrounding white space and concatenates (joins together) the non-whitespace characters. 

Syntax

The syntax of the token-pasting operator (##) is as follows:

#define MACRO_NAME(param1, param2) param1##param2
You can also try this code with Online C Compiler
Run Code
  • #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.
  • Param1 / param2: The parameters that are passed to the macros for concatenation.
  • ##: This is the token-pasting operator.

Example 1

// C program to demonstrate token-pasting operator (##)


#include <stdio.h>


#define TOKEN_PASTING(param1, param2) param1##param2
int main()
{
    
    printf("The concatenated result is: %d",TOKEN_PASTING(1234,56789));
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output: 

The concatenated result is: 123456789
You can also try this code with Online C Compiler
Run Code

Example 2

// C program to demonstrate token-pasting operator (##)


#include <stdio.h>


#define TOKEN_PASTING(param1, param2) param1##param2
int main()
{
    int val1=100;
    
    //val2 value will be 15
    int val2=TOKEN_PASTING(1,5); 
    
    printf("The value of variable val2 is : %d", val2);
    
    // equivalent to 100+15
    printf("\nSum of val1 and val2 is : %d", (val1+val2)); 
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output:

The value of variable val2 is : 15
Sum of val1 and val2 is : 115
You can also try this code with Online C Compiler
Run Code

Let us see an interesting example with strings!

Example 3

// C program to demonstrate token-pasting operator (##)

#include <stdio.h>

#define TOKEN_PASTING(param1, param2) param1##param2
int main()
{
    // The below statement will give a compilation error.
    //You do not need to use ## to concatenate strings. 
    //C already concatenates adjacent strings.
    
    printf("%s",TOKEN_PASTING(Coding, Ninjas));
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output: 

Compilation error.
You can also try this code with Online C Compiler
Run Code

Explanation

The above code will result in a compilation error. The reason is that C already concatenates adjacent strings. For e.g. printf("Coding""Ninjas"); will print CodingNinjas. 

However, we can use the stringizing operator (#) to concatenate two strings. The below example demonstrates the same.

Example 4

// C program to demonstrate string concatenation 


#include <stdio.h>


#define STRINGIZING_OPERATOR(param) #param
int main()
{
    
    // Using # operator to concatenate two strings
    // Simply call STRINGIZING_OPERATOR() twice with two parameters and 
    // it will concatenate the two parameters as strings.
    
    printf(STRINGIZING_OPERATOR(Coding) STRINGIZING_OPERATOR(Ninjas));
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output:

CodingNinjas
You can also try this code with Online C Compiler
Run Code

Application

## operator allows you to concatenate actual arguments during macro expansion. If a parameter in the replacement text is adjacent to a ##, the ## and surrounding white space are removed, and the result is re-scanned.

It is also used for logs in our program and to avoid repetitive typing.

Check out this article - C++ String Concatenation, Tribonacci Series

Frequently Asked Questions

The last parameter in Func delegate represents?

The last parameter in the Func delegate represents the return type. You need to specify the return type at the end of the parameters.

In which namespace Func delegate is specified?

Function delegate is specified in the System namespace. System.Runtime.dll. Encapsulates a single-parameter method that returns a result of the type defined by the TResult parameter.

How many parameters can a Func delegate accept?

It can have as less as 0 input parameters and as many as 16 input parameters, but only one output parameter. The Func delegate's final parameter is the out argument, which is a return type and is used for the outcome.

Can Func delegate be used with an anonymous function?

Yes, you can use Fun delegate with an anonymous function. The function delegate does not support ref and out arguments. An anonymous method or lambda expression can be utilized with the Func delegate type.

Can Func delegate be used with lambda expression?

Yes, Func delegate can be used with a lambda expression.  An anonymous method or lambda expression can be utilized with the Func delegate type.

Conclusion

In this article, we had a look at Func Delegate in C# and learned its implementation. We hope that this blog has helped you enhance your knowledge regarding Func Delegate and if you would like to learn more, check out Delegates in C#  and Methods in C# also other articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass