c-preprocessor

Strange behavior of Macro-expansion

≡放荡痞女 提交于 2020-02-10 16:01:46
问题 Here's the code: #include <stdio.h> #include <stdio.h> #define VAL1(a,b) a*b #define VAL2(a,b) a/b #define VAL3(a,b) ++a%b int main() { int a = 1; int b = 2; int c = 3; int d = 3; int e = 5; int result = VAL2(a,d)/VAL1(e,b)+VAL3(c,d); // result = 1 //int result = a/d/e*b+++c%d; // result = 0 printf("%d\n", result); return 0; } Why aren't the results of two statements the same? 回答1: In one case you have + ++ and in the other case you have ++ + . + ++ and ++ + are different streams of tokens .

Strange behavior of Macro-expansion

半城伤御伤魂 提交于 2020-02-10 15:58:46
问题 Here's the code: #include <stdio.h> #include <stdio.h> #define VAL1(a,b) a*b #define VAL2(a,b) a/b #define VAL3(a,b) ++a%b int main() { int a = 1; int b = 2; int c = 3; int d = 3; int e = 5; int result = VAL2(a,d)/VAL1(e,b)+VAL3(c,d); // result = 1 //int result = a/d/e*b+++c%d; // result = 0 printf("%d\n", result); return 0; } Why aren't the results of two statements the same? 回答1: In one case you have + ++ and in the other case you have ++ + . + ++ and ++ + are different streams of tokens .

Is it possible to stringify a C macro that contains a comma?

ⅰ亾dé卋堺 提交于 2020-02-05 05:18:24
问题 Say I have this: #define CAKE , something and the result I want is ", something" . Can it be done? The following doesn't work in gcc: #define MAKE_STRING(x) #x #define STRING(x) MAKE_STRING(x) STRING(CAKE) The compiler thinks I'm passing two arguments into MAKE_STRING() and balks. 回答1: If your preprocessor supports variadic macros, __VA_ARGS__ will do the trick: #define CAKE , something #define MAKE_STRING(...) #__VA_ARGS__ #define STRING(x) MAKE_STRING(x) #include <stdio.h> int main() {

Comma in C/C++ macro passed to another macro

无人久伴 提交于 2020-02-04 04:43:10
问题 I have these macros which generate error in Visual Studio 2015. #define log_params __FILE__, __LINE__ #define log(file, line, message, ...) _snprintf_s(nullptr, 0, 0, message, __VA_ARGS__) Now calling this never works log(log_params, "testing %d", 4) Any thoughts? I also checked output of preprocessor and it is: _snprintf_s(nullptr, 0, 0, 4 ); EDIT 1 Intresting finding #define log(file, line, message, ...) file line will produce this : "service.cpp", 164 "testing %d" Is it normal? 回答1: The

Using comma as macro name in C or C++

会有一股神秘感。 提交于 2020-02-02 03:02:09
问题 I'd like to do something like: #define , #define MAX 10,000,000 // ... #undef , is there any trick to do so? EDIT: I know about the ' digit separator in C++14. I'm looking for a trick to do the same for uncompliant compilers. EDIT2: Please consider Variadic Macros . 回答1: Warning, black magic ahead. Macros can indeed be used, albeit with a preset number of arguments. This number can be arbitrary, but each must be written by hand: #include <stdio.h> #include <stdlib.h> #define MERGE_EXPAND( a ,

#define inside an enum or how to extend an enum

江枫思渺然 提交于 2020-01-25 10:56:05
问题 I have several classes each of them uses the same enum but extends it a bit, based on its requirements. For example : class skirtColor{ enum Color{ red = 1, blue = 10, green = 12 }; }; class dressColor { enum Color{ red = 1, pink, yellow, blue = 10, green = 12 }; }; class pantsColor { enum Color { red = 1, brown, blue = 10, green = 12 }; }; Since there is no inheritance for enum in C++, I would like to use define for a common part #define COLOR\ // red color \ red = 1,\ // blue color \ blue =

How does expansion of the macro parameters work in c++

可紊 提交于 2020-01-25 02:52:11
问题 I just noticed an interesting thing about the expansion of the macro parameters in C++. I defined 4 macros; 2 of them turn given parameter into string and another 2 try to separate 2 arguments. I passed them argument with macro which expands into , and got the following results: #define Quote(x) #x #define String(x) Quote(x) #define SeparateImpl(first, second) first + second #define Separate(pair) SeparateImpl(pair) #define comma , int main(){ Quote(1 comma 2); // -> "1 comma 2" String(1

What is the point of saying “#define FOO FOO” in C?

微笑、不失礼 提交于 2020-01-24 20:45:06
问题 I came across some C code where the author uses the following idiom all over the place: typedef __int32 FOO_INT32; #define FOO_INT32 FOO_INT32 What is the point of doing this? Shouldn't the typedef be enough? It is a workaround for some wonky C compilers out there? 回答1: With the #define instruction, you'll then be able to test if the typedef has been done somewhere else in the code using : #ifdef FOO_INT32 FOO_INT32 myfoo; #else int myfoo; #endif 回答2: It's a practice that's sometimes done in

C preprocessor macro doesn't parse comma separated tokens?

白昼怎懂夜的黑 提交于 2020-01-24 20:21:29
问题 I want to choose one of two functions depending on the number of arguments: nargs = 0 ----> f1 nargs > 0 ----> f2. Macros do the following: get the first argument, then if no argument supplied ,it would add two commas " ,NULL,NULL ". Then it would select the second argument from the returned list of arguments. for example: f("Hello, world%i%s", x , s) ----> " Hello, world%i%s " ----> void f() ----> ,NULL,NULL ----> NULL so I can get null or void depending the on number of arguments. Here is

C preprocessor macro doesn't parse comma separated tokens?

六月ゝ 毕业季﹏ 提交于 2020-01-24 20:20:07
问题 I want to choose one of two functions depending on the number of arguments: nargs = 0 ----> f1 nargs > 0 ----> f2. Macros do the following: get the first argument, then if no argument supplied ,it would add two commas " ,NULL,NULL ". Then it would select the second argument from the returned list of arguments. for example: f("Hello, world%i%s", x , s) ----> " Hello, world%i%s " ----> void f() ----> ,NULL,NULL ----> NULL so I can get null or void depending the on number of arguments. Here is