How to print the variable name in C?

4

Example:

I have an integer variable called menino , how do I print to a printf the name of this variable, ie "menino"

    
asked by anonymous 12.08.2015 / 00:49

1 answer

4

You can do with a trick on the preprocessor. I found this macro in answer in SO :

#define DUMP(varname) fprintf(stderr, "%s = %x", #varname, varname);

The secret is in # that transforms the text of the code (part of the written code) into literal text (a string ) that can be used by the code. Obviously you do not have to use strderr or even fprintf .

int i = 0;
DUMP(i); //será convertido para fprintf(stderr, "%s = %x", "i", i);

I see little utility for this in normal codes.

    
12.08.2015 / 01:00