Opposite of C preprocessor “stringification”
No, you can’t unstringify something.
No, you can’t unstringify something.
You could try typeid(T).name() Edit: Fixed based on comments.
Like this: #include <stdio.h> #define QUOTE(str) #str #define EXPAND_AND_QUOTE(str) QUOTE(str) #define TEST thisisatest #define TESTE EXPAND_AND_QUOTE(TEST) int main() { printf(TESTE); } The reason is that when macro arguments are substituted into the macro body, they are expanded unless they appear with the # or ## preprocessor operators in that macro. So, str (with value TEST … Read more
Because that is how the preprocessor works. A single ‘#’ will create a string from the given argument, regardless of what that argument contains, while the double ‘##’ will create a new token by concatenating the arguments. Try looking at the preprocessed output (for instance with gcc -E) if you want to understand better how … Read more
In this context (applied to a parameter reference in a macro definition), the pound sign means to expand this parameter to the literal text of the argument that was passed to the macro. In this case, if you call PRINT(5) the macro expansion will be printf(“5” “%d\n”, 5); which will print 5 5; not very … Read more
The relevant steps of macro expansion are (per C 2011 [n1570] 6.10.3.1 and C++ 1998 16.3.1): Process tokens that are preceded by # or ##. Apply macro replacement to each argument. Replace each parameter with the corresponding result of the above macro replacement. Rescan for more macros. Thus, with xstr(foo), we have: The replacement text, … Read more
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);
One thing to be aware of when you’re using the token-paste (‘##‘) or stringizing (‘#‘) preprocessing operators is that you have to use an extra level of indirection for them to work properly in all cases. If you don’t do this and the items passed to the token-pasting operator are macros themselves, you’ll get results … Read more
Yes, it’s guaranteed. It works because arguments to macros are themselves macro-expanded, except where the macro argument name appears in the macro body with the stringifier # or the token-paster ##. 6.10.3.1/1: … After the arguments for the invocation of a function-like macro have been identified, argument substitution takes place. A parameter in the replacement … Read more
Classical C preprocessor question…. #define STR_HELPER(x) #x #define STR(x) STR_HELPER(x) #define MAJOR_VER 2 #define MINOR_VER 6 #define MY_FILE “/home/user/.myapp” STR(MAJOR_VER) STR(MINOR_VER) The extra level of indirection will allow the preprocessor to expand the macros before they are converted to strings.