How to print a variable name in C?


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("
The 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("
The variable name : %s", VariableName(ch));

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements