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));
Updated on: 2020-06-26T09:00:16+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements