How to I output the name of an input?

Viewed 284

For example, if I have char character = a, how would I be able to print out it's name, which is "character", along with it's output within a single printf? I know I can do printf("character = %c", character);, but how can I still print out the input's name if I enter char something = b and make it print out "something = b" instead of "character = b"?

4 Answers

I think paulsm4's coding example from https://www.geeksforgeeks.org/how-to-print-a-variable-name-in-c/ demonstrates a method of doing this but I thought I might delve into why it is not possible to do this without pre-processor macros. The reason for this is once you compile your C program into assembly language, all variable names are completely done away with. Thus, variables are not stored in RAM or on the disk as names with values associated to them but rather addresses with values associated with them. Because of this, it is impossible once a program is compiled to find out what any of the variables were originally called, and this is one of the reasons why reverse engineering a program from assembly language is so difficult.

Consider this code:

#include<stdio.h>

int main(void) {

    int exampleVar = 16;
    printf("The memory address of a is: %p\n", (void*) &exampleVar); 
      // Note the "&" symbol makes this return the 
      // address of exampleVar rather than its actual value 
    return 0;
}

If you run this on your computer you will notice it returns a very large hexadecimal number which is its address in RAM. If you were to ask the computer to fetch the value of "exampleVar" it would not use its name but rather the location we just printed and this process would simply return 16, the value of our variable. You'll also notice if you run it multiple times this number changes every time. This is because when you run any computer program the variables are initialized and stored in RAM. Data in RAM tends to constantly be changing and because of this the variables are almost always in a different place every time, though the location will not (typically) change while running the actual program, only between sessions.

If you were to look at the compiled version of this program you would never find "exampleVar" being referred to by its name but rather its address, because for all intents and purposes its address is its name to the computer.

We can store variable name in a string using sprintf() in C.

# include <stdio.h> 
# define getName(var, str) sprintf(str, "%s", #var) 

int main() 
{ 
    int myVar; 
    char str[20]; 
    getName(myVar, str); 
    printf("%s", str); 
    return 0; 
}

Output: myVar

I think the simplest form of what you are asking for (using a macro) is as follows:

#include <stdio.h>

#define print_variable(var) printf("%s = %d\n",#var,var)

int main()
{
    int myVar = 42;

    print_variable(myVar);

    exit(0)l
}

Output:

myVar = 42

This utilized the stringizing operator ('#'), which only works in a macro, as it is a preprocessor operator. As pointed out by others here, this MUST be done at the preprocessor level, because once the program is compiled, all name information is lost.

Related