stringification

Token pasting in C

自闭症网瘾萝莉.ら 提交于 2019-12-05 08:17:52
After reading about VA_NARG I tried to implement function overloading depending on number of arguments in C using macros. Now the problem is: void hello1(char *s) { ... } void hello2(char *s, char *t) { ... } // PP_NARG(...) macro returns number of arguments :ref to link above // does not work #define hello(...) hello ## PP_NARG(__VA_ARGS__) int main(void) { hello("hi"); // call hello1("hi"); hello("foo","bar"); // call hello2("foo","bar"); return 0; } I've read this from C-faq. But still could not get it to work... Jens Gustedt This is because of the evaluation rules for macros. You would

Why ENOUGH is enough? (storing an int in a char array)

对着背影说爱祢 提交于 2019-12-04 09:58:40
In one of the answers (and its comments) on How to convert an int to string in C the following solution is given char str[ENOUGH]; sprintf(str, "%d", 42); in the comments caf mentions that ENOUGH can be determined at compile time with: #define ENOUGH ((CHAR_BIT * sizeof(int) - 1) / 3 + 2) I get the + 2 because you need to be able to display the minus sign and null terminator but what is the logic behind the other part? Specifically CHAR_BIT ? If int type is 32-bit, how many bytes do you need to represent any number (without sign and null terminator)? If int type is 32-bit the maximum int value

Error when defining a stringising macro with __VA_ARGS__

删除回忆录丶 提交于 2019-12-04 02:17: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. EDIT After giving up on stringising arguments, and double-hashing ( ## ) __VA_ARGS__ I now have this

Accessing the value of a Preprocessor Macro definition

天涯浪子 提交于 2019-12-03 16:18:13
问题 If I add a macro "FOO=bar" under GCC_PREPROCESSOR_DEFINITIONS (or Preprocessor Macros if you use XCode"), what would be the best way to access the value of "FOO"? Currently, I use the clumsy: #define MACRO_NAME(f) #f #define MACRO_VALUE(f) MACRO_NAME(f) #ifdef FOO NSLog(@"%s", MACRO_VALUE(FOO)); #else NSLog(@"undefined"); #endif This will output "bar" Surely, there must be a better/cleaner way? 回答1: What you are doing is the way to stringize (or stringify ) macro values. The indirection is

How does this C code work?

自古美人都是妖i 提交于 2019-12-03 14:54:51
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)? The ## concatenates two tokens together. It can only be used in the preprocessor. f(1,2) becomes 1 ## 2 becomes 12 . The # operator by itself stringifies tokens: #a becomes "a" . Therefore, g(f(1,2)) becomes "f(1,2)" when the preprocessor is

Preprocessor tomfoolery (stringifying a #include)

余生颓废 提交于 2019-12-03 11:21:56
问题 Note: This question has nothing to do with OpenCL per se... check the last paragraph for a succinct statement of my question. But to provide some background: I'm writing some C++ code that makes use of OpenCL. I like to keep the source for my OpenCL kernels in their own files, to keep coding and maintenance easy (as opposed to embedding the sources directly as string constants in associated C++ code). This inevitably leads to the question of how to load them into the OpenCL runtime once it

Accessing the value of a Preprocessor Macro definition

安稳与你 提交于 2019-12-03 05:24:43
If I add a macro "FOO=bar" under GCC_PREPROCESSOR_DEFINITIONS (or Preprocessor Macros if you use XCode"), what would be the best way to access the value of "FOO"? Currently, I use the clumsy: #define MACRO_NAME(f) #f #define MACRO_VALUE(f) MACRO_NAME(f) #ifdef FOO NSLog(@"%s", MACRO_VALUE(FOO)); #else NSLog(@"undefined"); #endif This will output "bar" Surely, there must be a better/cleaner way? What you are doing is the way to stringize (or stringify ) macro values. The indirection is unavoidable. This is even mentioned in the GCC preprocessor manual section that Rob linked to. NSLog(@"%s",

What is the compiler seeing with this macro? [closed]

浪尽此生 提交于 2019-12-01 15:49:38
Consider this: #define STRINGIFY(A) #A If I then later write: STRINGIFY(hello) Is the compiler actually seeing this: #hello I think it is that additional hash in front of #A that is confusing me. No, the compiler will put the argument between quotes, resulting in this: "hello" However, beware that macro substitution doesn't take place near # and ## , so if you really need to stringify the argument ( even if it's another macro ) it's better to write two macros, the first one to expand the argument and the other one to add quotes: #define STRINGIFY(x) STRINGIFY_AUX(x) #define STRINGIFY_AUX(x) #x

What is the compiler seeing with this macro? [closed]

◇◆丶佛笑我妖孽 提交于 2019-12-01 14:50:54
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . Consider this: #define STRINGIFY(A) #A If I then later write: STRINGIFY(hello) Is the compiler actually seeing this: #hello I think it is that additional hash in front of #A that is confusing me. 回答1: No, the

Differences in Macro ## concatenation operator between Visual-C++ and gcc

女生的网名这么多〃 提交于 2019-12-01 04:28:05
问题 I'm having a macro like this ( not exactly, but function is quite equivalent): #define STRUCTMEMBER(Member,Value) GlobalStructInstance. ## Member = Value ... STRUCTMEMBER(Item,1); This works perfectly in Visual C++, but gcc 3.4.5 (MingGW) yield the following error: pasting "." and "Item" does not give a valid preprocessing token This also happens when I use the "->" operator. I didn't found hints on concatenation, that the usage of these operators is forbidden. Does anyone have an idea ? 回答1: