stringification

C preprocessor: stringize macro and identity macro

风格不统一 提交于 2019-12-18 03:39:06
问题 I want to know the reason behind the output of this code. I couldn't come up with an answer. #define f(a,b) a##b #define g(a) #a #define h(a) g(a) void main() { printf("%s %s",h(f(1,2)),g(f(1,2))); } PS: output is 12 f(1,2) . I thought it was 12 12 or f(1,2) f(1,2) . 回答1: h(f(1,2)) f(1,2) is substituted for a . a is not the subject of a # or ## operator so it's expanded to 12 . Now you have g(12) which expands to "12" . g(f(1,2)) f(1,2) is substituted for a . The # operator applied to a

C Macros to create strings

ⅰ亾dé卋堺 提交于 2019-12-17 07:22:23
问题 Alternative Titles (to aid search) Convert a preprocessor token to a string How to make a char string from a C macro's value? Original Question I would like to use C #define to build literal strings at compile time. The string are domains that change for debug, release etc. I would like to some some thing like this: #ifdef __TESTING #define IV_DOMAIN domain.org //in house testing #elif __LIVE_TESTING #define IV_DOMAIN test.domain.com //live testing servers #else #define IV_DOMAIN domain.com /

What does ## mean for the C(C++) preprocessor?

廉价感情. 提交于 2019-12-17 04:07:54
问题 I have a C program below: #define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); } when I run just the preprocessor it expands this as { int var12=100; printf("%d",var12); } which is the reason why the output is 100. Can anybody tell me how/why the preprocessor expands var##12 to var12 ? 回答1: nothing too fancy: ## tells the preprocessor to concatenate the left and right sides see http://en.wikipedia.org/wiki/C_preprocessor#Token_concatenation 回答2: because ## is a token

Convert a preprocessor token to a string

拜拜、爱过 提交于 2019-12-17 02:35:16
问题 I'm looking for a way to convert a preprocessor token to a string. Specifically, I've somewhere got: #define MAX_LEN 16 and I want to use it to prevent buffer overrun: char val[MAX_LEN+1]; // room for \0 sscanf(buf, "%"MAX_LEN"s", val); I'm open to other ways to accomplish the same thing, but standard library only. 回答1: 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(_

How, exactly, does the double-stringize trick work?

你离开我真会死。 提交于 2019-12-17 02:05:51
问题 At least some C preprocessors let you stringize the value of a macro, rather than its name, by passing it through one function-like macro to another that stringizes it: #define STR1(x) #x #define STR2(x) STR1(x) #define THE_ANSWER 42 #define THE_ANSWER_STR STR2(THE_ANSWER) /* "42" */ Example use cases here. This does work, at least in GCC and Clang (both with -std=c99 ), but I'm not sure how it works in C-standard terms. Is this behavior guaranteed by C99? If so, how does C99 guarantee it? If

What are the applications of the ## preprocessor operator and gotchas to consider?

限于喜欢 提交于 2019-12-16 20:03:28
问题 As mentioned in many of my previous questions, I'm working through K&R, and am currently into the preprocessor. One of the more interesting things — something I never knew before from any of my prior attempts to learn C — is the ## preprocessor operator. According to K&R: The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion. If a parameter in the replacement text is adjacent to a ## , the parameter is replaced by the actual argument, the ## and

What are the applications of the ## preprocessor operator and gotchas to consider?

烂漫一生 提交于 2019-12-16 20:01:14
问题 As mentioned in many of my previous questions, I'm working through K&R, and am currently into the preprocessor. One of the more interesting things — something I never knew before from any of my prior attempts to learn C — is the ## preprocessor operator. According to K&R: The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion. If a parameter in the replacement text is adjacent to a ## , the parameter is replaced by the actual argument, the ## and

Error when defining a stringising macro with __VA_ARGS__

强颜欢笑 提交于 2019-12-12 08:26:59
问题 I have been trying to implement a function macro in C that prepends "DEBUG: ", to the argument, and passes its arguments to printf: #define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__) This gives me this error in gcc: src/include/debug.h:4:70: error: expected expression before ‘)’ token #define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__) ^ Supposedly, it should stringise format, and pass its variable arguments to printf, but so far I can't get past this error.

How does this C code work?

跟風遠走 提交于 2019-12-12 08:13:08
问题 What is a##b & #a ? #define f(a,b) a##b #define g(a) #a #define h(a) g(a) main() { printf("%s\n",h(f(1,2))); //how should I interpret this?? [line 1] printf("%s\n",g(f(1,2))); //and this? [line 2] } How does this program work? The output is 12 f(1, 2) now I understand how a##b & #a work. But why is the result different in the two cases (line 1 and line 2)? 回答1: The ## concatenates two tokens together. It can only be used in the preprocessor. f(1,2) becomes 1 ## 2 becomes 12 . The # operator

GNU C preprocessor: Stringify the result of a macro evaluation

北战南征 提交于 2019-12-11 12:59:05
问题 I have a common string macro that I want to convert to a length-value string, all within macros, if possible, so everything ends up in .rodata . #define PAYLOAD "xyz" #define PAYLOAD_LEN (sizeof(PAYLOAD)-1) I would like to use PAYLOAD_LEN, as a string, in part of another string, e.g. const char lv_macro[] = "<preamble>" PAYLOAD_LEN ":" PAYLOAD; const char lv_wanted[] = "<preamble>3:xyz"` I suspect that this is not possible, and that I should just define PAYLOAD_LEN as a literal, e.g. #define