English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to Print Variable Name in C?

The following is an example of printing variable names.

Example

#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 Result

The variable name: name
The variable name: ch

In the above program, the variable name is printed through the method defined earlier main()

#define VariableName(name) #name

Two variables of different data types are declared. Variable names can be printed using the defined function.

int name;
char ch;
printf("The variable name: %s", VariableName(name));
printf("\nThe variable name: %s", VariableName(ch));