How to stringify an expression in C

前端 未结 7 1341
刺人心
刺人心 2020-12-07 01:53

Is there a way to evaluate an expression before stringification in c?

example:

#define stringify(x)  #x
...
const char * thestring = stringify( 10 *          


        
相关标签:
7条回答
  • 2020-12-07 02:31

    Preprocessor macros are run before the compiler. It is, by definition, not possible to do exactly what you'retrying to do.

    To convert a number into a string at runtime, call the itoa function, like this:

    char thestring[8];
    
    itoa(10 * 50, thestring, 10);
    

    Note that this code declares thestring as an array, not a pointer. For more information, read about memory management in C.

    0 讨论(0)
提交回复
热议问题