Stringizing Operator in C - Program to print variable name 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

Defining Macro

#define	macro_function(argument) 	#argument
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;
}
    
    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;
}

    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.