Home »
C programs »
C advance programs
Stringizing Operator in C | How to print a variable name in C?
What is Stringizing Operator in C?
Stringizing Operator: '#' in preprocessor directive is known as Stringizing Operator it is used to convert an argument into string format.
Print variable name using C program
To print variable name in C, use the stringizing operator and define macro. Below is the syntax:
Defining Macro:
#define macro_function(argument) #argument
C program to print a variable name
Consider the example
/*
C program to demonstrate example of
Stringizing Operator
*/
#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
Write a code for Debugging – print variable name with their values
#include <stdio.h>
#define printDebug(x) printf("\nvalue of \"%s\" is: %d\n", #x, x);
int main()
{
int value1;
int value2;
value1 = 10;
value2 = 20;
printDebug(value1);
printDebug(value2);
return 0;
}
Output
value of "value1" is: 10
value of "value2" is: 20
printDebug
In this macro variable is passing in x, The statement printf("\nvalue of \"%s\" is: %d\n",#x,x); #x will convert the given argument in string and x will return the value.
C Advance Programs »