c-preprocessor

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.

Anyway to see list of preprocessor defined macros?

巧了我就是萌 提交于 2019-12-12 08:26:55
问题 I'd like to see all macros that are defined by the invocation of the compiler I'm using. Is there any way to do this? I have seen in the manual it says you can use cpp -dM but this doesn't work for me. Perhaps I'm doing something wrong? When I run: cpp -dM I get no output at all from the preprocessor. If I try adding -dM as an option on gcc, I don't notice any difference. 回答1: You can use: gcc -dM -E - < /dev/null Note that you can also get the compiler macros in addition with this command:

preprocessor directives inside define? [duplicate]

*爱你&永不变心* 提交于 2019-12-12 08:26:45
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: C preprocessor: using #if inside #define? Is there any trick to have preprocessor directives inside rhs of define ? The problem is, preprocessor folds all rhs into one long line. But maybe there is a trick ? Example of what I'd want in the rhs is #define MY_CHECK \ #ifndef MY_DEF \ # error MY_DEF not defined \ #endif ? The purpose is a shortness: to have 1-line shortcut instead of multiline sequence of checks.

Why does the 'for' loop condition fail? [duplicate]

情到浓时终转凉″ 提交于 2019-12-12 08:25:19
问题 This question already has answers here : A riddle (in C) (4 answers) Closed 2 years ago . In the code shown below, nothing gets printed, which means the condition in the for loop fails. What could be the reason? I'm wondering because when I print TOTAL_ELEMENTS separately, it gives 5 , so naturally this must be 5-2=3 => -1<=3 , so it should print something. #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int array[] = { 23, 34, 12, 17, 204, 99, 16 }; int main() { int d; for (d = -1;

Why are argument substitutions not replaced during rescanning?

房东的猫 提交于 2019-12-12 08:22:50
问题 Consider the following macro definitions and invocation: #define x x[0] #define y(arg) arg y(x) This invocation expands to x[0] (tested on Visual C++ 2010, g++ 4.1, mcpp 2.7.2, and Wave). Why? Specifically, why does it not expand to x[0][0] ? During macro replacement, A parameter in the replacement list...is replaced by the corresponding argument after all macros contained therein have been expanded. Before being substituted, each argument’s preprocessing tokens are completely macro replaced

Can I make a preprocessor directive dependent on the .NET framework version?

倖福魔咒の 提交于 2019-12-12 08:20:04
问题 Here's a concrete example of what I want to do. Consider the string.Join function. Pre-.NET 4.0, there were only two overloads, both of which required a string[] parameter. As of .NET 4.0, there are new overloads taking more flexible parameter types, including IEnumerable<string> . I have a library which includes a Join function that does essentially what the .NET 4.0 string.Join function does. I was just wondering if I could make this function's implementation dependent on the .NET framework

C++ anonymous variables

情到浓时终转凉″ 提交于 2019-12-12 08:13:30
问题 Why won't this work? 0. #define CONCAT(x, y) x ## y 1. 2. #define VAR_LINE(x) \ 3. int CONCAT(_anonymous, __LINE__) = x 4. 5. #define VAR_LINE2(x) \ 6. int _anonymous ## x = 1 7. 8. int main() 9. { 10. VAR_LINE(1); 11. VAR_LINE(1); 12. VAR_LINE(1); 13. VAR_LINE2(__LINE__); 14. } The result from the above macro expansion int _anonymous__LINE__ = 1; int _anonymous__LINE__ = 1; int _anonymous__LINE__ = 1; int _anonymous13 = 1; It would be convenient if I didn't have to write that __LINE__ macro

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

Confused by squaring macro SQR in c [duplicate]

六眼飞鱼酱① 提交于 2019-12-12 07:27:39
问题 This question already has answers here : The need for parentheses in macros in C [duplicate] (8 answers) Confusion with Macro expansion [duplicate] (3 answers) Closed 6 years ago . This question was asked to me in a mock interview...Really got surprised to find awkward answers... consider a macro: #define SQR(x) (x*x) Example 1: SQR(2) //prints 4 Example 2: If SQR(1+1) is given it doesn't sum (1+1) to 2 but rather ... SQR(1+1) //prints 3 Awkward right? What is the reason? How does this code

Spaces inserted by the C preprocessor

三世轮回 提交于 2019-12-12 07:14:36
问题 Suppose we are given this input C code: #define Y 20 #define A(x) (10+x+Y) A(A(40)) gcc -E outputs like that (10+(10+40 +20)+20) . gcc -E -traditional-cpp outputs like that (10+(10+40+20)+20) . Why the default cpp inserts the space after 40 ? Where can I find the most detailed specification of the cpp that covers that logic ? 回答1: The C standard doesn't specify this behaviour, since the output of the preprocessing phase is simply a stream of tokens and whitespace. Serializing the stream of