Home » C programming language

Stringizing Operator (#) in C



The operator # is known as Stringize Operator in C language, it is used in conjunction with #define directive.

Stringize Operator (#) is used to turn the argument into quoted string.

Defining Macro with Stringize Operator

#define macro_function(argument)    #argument

Consider the following example

#include <stdio.h>

#define printText(text) #text

int main()
{
	printf(printText(Hello Guys));
	printf("\n");
	return 0;
}

Output

Hello Guys

In this program, we are passing Hello Guys as argument without quotes, but Stringize Operator inserts double quotes around the string and it would be "Hello Guys".

Program to print variable name in C

This is another example of Stringize Operator (#), by using this operator we can print the name of variable. Here, we will pass the name of variable as argument and it will print as string.

#include <stdio.h>
 
#define getVariableName(x)  #x
 
int main()
{
    int student_age=21;
     
    printf("value of %s is = %d\n",getVariableName(student_age),student_age);
     
    return 0;
}

Output

value of student_age is = 21





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.