How to print a variable name in C?

CProgrammingServer Side Programming

The following is an example to print variable name.

Example

 Live Demo

#include <stdio.h>
#define VariableName(name) #name
int main() {
   int name;
   char ch;
   printf("The variable name : %s", VariableName(name));
   printf("\nThe variable name : %s", VariableName(ch));
   return 0;
}

Output

The variable name : name
The variable name : ch

In the above program, the variable names are printed by defining the method before main()

#define VariableName(name) #name

Two variables of different datatypes are declared. By using the defined function, variable names are printed.

int name;
char ch;
printf("The variable name : %s", VariableName(name));
printf("\nThe variable name : %s", VariableName(ch));
raja
Published on 06-Nov-2018 14:05:05
Advertisements