1

For example lets say I defined a constant as below using #define macro in C

#define MAXSIZE 10

Is there any function in C that I can invoke by passing 10 as input and retrieve that constant name "MAXSIZE". Thanks

John Bode
  • 119,563
  • 19
  • 122
  • 198
Aniketh
  • 1,411
  • 3
  • 12
  • 15
  • 1
    There's no function in the standard library to do that, no. – John Bode Mar 15 '17 at 20:35
  • It would not be possible to write a function to do this as `#define` is a preprocessor directive, meaning that by the time we get to runtime, it is gone – Ben Wainwright Mar 15 '17 at 20:36
  • 1
    Is the the question just out of curiosity? If not, describe the actual problem you try to solve so we can give good advice. – Hans Mar 15 '17 at 20:39

2 Answers2

5

No for many reasons:

  • C is not a reflective language
  • what if there are several constants with the same 10 value?

but the actual reason that kills it is:

  • preprocessor actually replaces MAXSIZE by 10 before compiling, so this information is lost
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • C has no built-in facility for this, but you can write your own function to do as the OP asks, subject to some non-trivial limitations (among them that such a function can support at most one macro name for any given value). – John Bollinger Mar 15 '17 at 20:48
2

The answer is no, the c preprocessor replaces all occurences of MAXSIZE with 10 when run.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
KevinDTimm
  • 14,226
  • 3
  • 42
  • 60