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




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.