c-preprocessor

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

北慕城南 提交于 2019-12-04 01:10:09
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 being targeted. If 4.0, it could simply call string.Join internally. If 3.5 or older, it could call

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

删除回忆录丶 提交于 2019-12-04 00:57:36
This question already has answers here : Closed 2 years ago . A riddle (in C) (4 answers) 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; d <= (TOTAL_ELEMENTS - 2); d++) { printf("%d\n", array[d + 1]); } return 0; } Can someone explain this

Advantages of conditional-preprocessor over conditional statements

亡梦爱人 提交于 2019-12-04 00:50:08
问题 I have never worked with #if , #ifdef , #ifndef , #else , #elif and #endif . As I was going through some source-codes, I found an extensive use of these directives. Did some reading on conditional-preprocessors but found no clue like how are they different from normal conditional statements . So I was wondering what is the advantage of following code: #include<iostream> int main() { int i = 0; #if i == 0 std::cout<<"This"; #else std::cout<<"That"; #endif return 0; } over this: #include

C preprocessors and order of operations

六眼飞鱼酱① 提交于 2019-12-04 00:32:37
问题 I'm learning C, but I do not understand this: #define square(x) x*x a = square(2+3) //a = 11 When this is run, why does a end up being 11 ? 回答1: It expands to 2+3*2+3 , which is equivalent to 2+(3*2)+3 . Use parentheses to fix it: #define square(x) ((x)*(x)) Now try it with square(x++) and you'll run into more problems (undefined behavior). Avoid doing this as a macro if you can. 回答2: square(2+3) expands to 2+3*2+3 which is equivalent to 2+(3*2)+3 [ * has higher precedence than + ] On gcc you

Preprocessor output on Qt Creator

谁都会走 提交于 2019-12-04 00:31:50
问题 I am compiling C code in Qt Creator and I need to look at the preprocessor output. I added the -E flag to the make, but I don't see the *.i files: mingw32-make.exe -e -w in \qt\qt-build-desktop Please help. 回答1: -E is a gcc option, not a make option, so passing it to make won't do anything. Also, using -E works fine for a single file, but will break your build as no proper .o file is generated (it contains the preprocessed source). What works fine though is adding the following to the .pro

Macro evaluation order [duplicate]

和自甴很熟 提交于 2019-12-04 00:09:46
This question already has answers here : Closed 7 years ago . Possible Duplicate: # and ## in macros why the output of second printf is f(1,2) what is the order in which macro is evaluated? #include <stdio.h> #define f(a,b) a##b #define g(a) #a #define h(a) g(a) int main() { printf("%s\n",h(f(1,2))); printf("%s\n",g(f(1,2))); return 0; } output 12 f(1,2) From http://gcc.gnu.org/onlinedocs/cpp/Argument-Prescan.html#Argument-Prescan Macro arguments are completely macro-expanded before they are substituted into a macro body, unless they are stringified or pasted with other tokens. After

Why no warning with “#if X” when X undefined?

岁酱吖の 提交于 2019-12-03 23:55:32
I occasionally write code something like this: // file1.cpp #define DO_THIS 1 #if DO_THIS // stuff #endif During the code development I may switch the definition of DO_THIS between 0 and 1. Recently I had to rearrange my source code and copy some code from one file to another. But I found that I had made a mistake and the two parts had become separated like so: // file1.cpp #define DO_THIS 1 and // file2.cpp #if DO_THIS // stuff #endif Obviously I fixed the error, but then thought to myself, why didn't the compiler warn me? I have the warning level set to 4. Why isn't #if X suspicious when X

Passing an initialization list to a macro

我的梦境 提交于 2019-12-03 22:55:56
Why doesn't the commented out line in the following program compile? #include <iostream> #include <vector> using namespace std; #define F1(a) 1 int F2(vector<int>) { return 2; } int main() { vector<int> v; v = vector<int>{1,2,3}; cout << F1( v ) << endl; //The following line doesn't compile. The error is: //error: macro "F" passed 3 arguments, but takes just 1 //cout << F1( vector<int>{1,2,3} ) << endl; // <- error! cout << F1( vector<int>({1,2,3}) ) << endl; cout << F1( (vector<int>{1,2,3}) ) << endl; cout << F2( v ) << endl; //The following line compiles fine cout << F2( vector<int>{1,2,3} )

Difference between gcc and Microsoft preprocessor

做~自己de王妃 提交于 2019-12-03 22:52:04
I discovered that Microsoft Visual Studio compiler and gcc preprocess the following small snippet differently: # define M3(x, y, z) x + y + z # define M2(x, y) M3(x, y) # define P(x, y) {x, y} # define M(x, y) M2(x, P(x, y)) M(a, b) 'gcc -E' gives the following: a + {a + b} , while 'cl /E' issues a warning about missing macro argument and produces the following output: a + {a, b} + It seems that commas that came from nested macro expansions are not considered to be argument separators. Unfortunately, I found no description of the algorithm implemented in cl preprocessor, and so I'm not sure

How to disable #line directives being written to the T4 generation output file

二次信任 提交于 2019-12-03 22:31:02
I have encountered a small problem with my T4 code generation. I have broken my T4 templates up into separate files and placed them in various directories, I have done this so parts of my code generation may be re-used in multiple projects, e.g. model generation, repository generation and service generation all include a core EntityGeneration.tt file. Unfortunately, when TextTemplating resolves my nested includes, it builds up a long #line pre-processor directive in its generated .cs file, combining all of the relative paths to the lowest level included file. Unfortunately, as this path is