Convert a preprocessor token to a string

I found an answer online.

#define VERSION_MAJOR 4
#define VERSION_MINOR 47

#define VERSION_STRING "v" #VERSION_MAJOR "." #VERSION_MINOR

The above does not work but hopefully illustrates what I would like to do, i.e. make VERSION_STRING end up as "v4.47".

To generate the proper numeric form use something like

#define VERSION_MAJOR 4
#define VERSION_MINOR 47

#define STRINGIZE2(s) #s
#define STRINGIZE(s) STRINGIZE2(s)
#define VERSION_STRING "v" STRINGIZE(VERSION_MAJOR) \
"." STRINGIZE(VERSION_MINOR)

#include <stdio.h>
int main() {
    printf ("%s\n", VERSION_STRING);
    return 0;
}

see http://www.decompile.com/cpp/faq/file_and_line_error_string.htm specifically:

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define AT __FILE__ ":" TOSTRING(__LINE__)

so your problem can be solved by doing sscanf(buf, "%" TOSTRING(MAX_LEN) "s", val);


It's been a while, but this should work:

 sscanf(buf, "%" #MAX_LEN "s", val);

If not, it'll need to "double expansion" trick:

 #define STR1(x)  #x
 #define STR(x)  STR1(x)
 sscanf(buf, "%" STR(MAX_LEN) "s", val);