c-preprocessor

Array format for #define (C preprocessor)

人走茶凉 提交于 2019-12-18 04:42:31
问题 Probably a naïve question - I used to program 20 years ago and haven't coded much since. My memory of how the C preprocessor works has atrophied significantly since then... I am writing a very simple C program and I am trying to declare a few static global arrays, but the size of the arrays would be dependent (on a non-trivial way) on a MODE variable. Something like the simplified example below. Two quick points: I know I could just size the arrays according to the largest size needed by any

Can a C macro contain temporary variables?

冷暖自知 提交于 2019-12-18 04:40:10
问题 I have a function that I need to macro'ize. The function contains temp variables and I can't remember if there are any rules about use of temporary variables in macro substitutions. long fooAlloc(struct foo *f, long size) { long i1, i2; double *data[7]; /* do something */ return 42; } MACRO Form: #define ALLOC_FOO(f, size) \ {\ long i1, i2;\ double *data[7];\ \ /* do something */ \ } Is this ok? (i.e. no nasty side effect - other than the usual ones : not "type safe" etc). BTW, I know "macros

Determine optimization level in preprocessor?

十年热恋 提交于 2019-12-18 04:31:46
问题 -Og is a relatively new optimization option that is intended to improve the debugging experience while apply optimizations. If a user selects -Og , then I'd like my source files to activate alternate code paths to enhance the debugging experience. GCC offers the __OPTIMIZE__ preprocessor macro, but its only set to 1 when optimizations are in effect. Is there a way to learn the optimization level, like -O1 , -O3 or -Og , for use with the preprocessor? 回答1: I believe this is not possible to

How do I check if one of multiple macros is defined in a single #ifdef?

给你一囗甜甜゛ 提交于 2019-12-18 03:56:51
问题 I have some C++ code, and want to perform an action if the __APPLE__ or __linux macros are defined. If I did it as a normal if conditional, it would be easy using || : if (something || something) { .. code .. } But as of what I know there is no || operator for #ifdef statements. How would I check if __APPLE__ or __linux is defined using a single #ifdef statement? 回答1: You can't in a single #ifdef would a single #if do instead? #if defined(__APPLE__) || defined(__linux) this also works if you

Should preprocessor instructions be on the beginning of a line?

こ雲淡風輕ζ 提交于 2019-12-18 03:53:19
问题 A while ago I have discovered an (rather ancient) C Compiler, which scanned macros this way (Pseudo code): if line.startswith("#include") or line.startswith("#define"): ... .. Which kind of raised the question for me where macros should really be placed, at the beginning of a line, like so: void stuff() { #if defined(WIN32) || defined(_WIN32) ... #else #if defined(__GNUC__) ... #else ... #endif #endif } Or rather like so (as that's the way I do it, for improved readability): void stuff() {

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

#include anywhere

不想你离开。 提交于 2019-12-18 02:40:22
问题 Is the #include <file> meant to be used for headers only or is it simply a mechanical "inject this code here" that can be used anywhere in the code? What if I use it in the middle of a cpp function to just "inject" code from a single source? will this work or will compilers scream about this? 回答1: It is a mechanical inject the code here device. You can include a text file containing Goethe's Faust if you wish to. You can put it anywhere, even in the middle of a function (of course, #include

Creating a string list and an enum list from a C++ macro

安稳与你 提交于 2019-12-17 23:27:31
问题 In order to make my code shorter and easier to change I want to replace something like enum{ E_AAA, E_BBB, E_CCC }; static const char *strings{"AAA", "BBB", "CCC" }; With a macro, like INIT(AAA, BBB, CCC); but when I try doing a macro with variable arguments, and stringification I get an error as the arguments are not declared. Any idea on how to do this? 回答1: Here a solution I learned a few days ago. The simplified version that attends your question is: #define ENUM_MACRO(name, v1, v2, v3,

C preprocessor, recursive macros

与世无争的帅哥 提交于 2019-12-17 22:42:14
问题 Why does M(0) and N(0) have different results? #define CAT_I(a, b) a ## b #define CAT(a, b) CAT_I(a, b) #define M_0 CAT(x, y) #define M_1 whatever_else #define M(a) CAT(M_, a) M(0); // expands to CAT(x, y) #define N_0() CAT(x, y) #define N_1() whatever_else #define N(a) CAT(N_, a)() N(0); // expands to xy 回答1: In fact, it depends on your interpretation of the language standard. For example, under mcpp, a preprocessor implementation that strictly conforms to the text of the language standard,

How to check (via the preprocessor) if a C source file is being compiled as C++ code

牧云@^-^@ 提交于 2019-12-17 22:00:03
问题 The question title should say it all, but here's an example of what sort of thing I'm looking for: #ifndef THE_IDENTIFIER_THAT_WOULD_INDICATE_BEING_COMPILED_AS_CPLUSPLUS /* * Example of something that would matter. */ typedef enum _bool bool; enum _bool { false, true }; #endif What is the identifier? It's bugging me severely, as I know I've seen code that does this before. I'm using GCC, by the way. (I'm surprised I couldn't find a duplicate somewhere on SO. If someone else can find one, feel