Questions tagged [preprocessor-directive]

Instructions to a preprocessor that control how it modifies source text

The C and C++ preprocessors recognise directives such as #include and #define, part of the C language syntax, which control how the preprocessor modifies the source text.

The C and C++ preprocessors use the following directives:

  • source file inclusion: #include
  • macro definition: #define, #undef
  • conditional inclusions: #if, #elif, #ifdef, #ifndef, #else and #endif
  • diagnostic messages: #error (and as a common non-standard extension, #warning)
  • implementation-defined details: #pragma
  • operators: #, ##, defined and _Pragma
589 questions
869
votes
19 answers

#ifdef replacement in the Swift language

In C/C++/Objective C you can define a macro using compiler preprocessors. Moreover, you can include/exclude some parts of code using compiler preprocessors. #ifdef DEBUG // Debug-only code #endif Is there a similar solution in Swift?
mxg
  • 20,946
  • 12
  • 59
  • 80
153
votes
6 answers

Can gcc output C code after preprocessing?

I'm using an open source library which seems to have lots of preprocessing directives to support many languages other than C. So that I can study what the library is doing I'd like to see the C code that I'm compiling after preprocessing, more like…
LGTrader
  • 2,349
  • 4
  • 23
  • 29
103
votes
3 answers

Can #if pre-processor directives be nested in C++?

I have a question about Pre-processor directives in c++: For example: #ifndef QUESTION //some code here #ifndef QUESTION //some code here #endif #endif Can we use it in this way, and can the C++ compiler match the ifndef and endif in the right…
user707549
68
votes
13 answers

Is #define banned in industry standards?

I am a first year computer science student and my professor said #define is banned in the industry standards along with #if, #ifdef, #else, and a few other preprocessor directives. He used the word "banned" because of unexpected behaviour. Is this…
psrag anvesh
  • 1,257
  • 2
  • 12
  • 18
56
votes
4 answers

What is double evaluation and why should it be avoided?

I was reading that in C++ using macros like #define max(a,b) (a > b ? a : b) can result in a 'double evaluation'. Can someone give me an example of when a double evaluation occurs and why it's bad? P.S.: Surprisingly I couldn't find any detailed…
AnkithD
  • 743
  • 6
  • 9
53
votes
8 answers

Force the compiler to ignore some lines in the program

Suppose that I have 10,000 lines of C++ code. 200 lines of this code are for testing purpose (for example, check the program and show an error message). Is there an way in C++ to ignore or consider some lines of the code (maybe with preprocessor…
user1436187
  • 3,252
  • 3
  • 26
  • 59
53
votes
4 answers

How are the __cplusplus directive defined in various compilers?

My compiler expands it to 199711L. What does that mean? I read that __cplusplus > 199711L signifies C++11. What are the possible expansions of this macro and what does it signify?
unj2
  • 52,135
  • 87
  • 247
  • 375
53
votes
3 answers

Is there a preprocessor directive for detecting C++11x support?

If have some code where I would like to use C++11x extensions as much as possible, but have a fallback if this is not supported. Currently the OSX version of GCC and the VisualC compiler has little to no support for C++11x, so I use: #if…
Kenneth
  • 718
  • 1
  • 5
  • 7
47
votes
4 answers

#if preprocessor directive for directives other than DEBUG

I know that I can use preprocessor directives to check for Debug/Release by doing this: #if DEBUG //debug mode #elif //release mode #endif but what about checking for other configurations, like Test. In VB you can do this: #If CONFIG =…
Johnie Karr
  • 2,744
  • 2
  • 35
  • 44
45
votes
8 answers

Escaping a # symbol in a #define macro?

Without going into the gory details I want to use a #define macro that will expand to a #include but the '#' sign is confusing the preprocessor (as it thinks I want to quote an argument.) For example, I want to do something like this: #define…
Rob
  • 76,700
  • 56
  • 158
  • 197
45
votes
3 answers

Objective-C preprocessor directive for 'if not'

I understand how to use a preprocessor directive like this: #if SOME_VARIABLE // Do something #else // Do something else #endif But what if I only want to do something IF NOT SOME_VARIABLE. Obviously I still could do this: #if…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
43
votes
4 answers

C Preprocessor testing definedness of multiple macros

I searched the site but did not find the answer I was looking for so here is a really quick question. I am trying to do something like that : #ifdef _WIN32 || _WIN64 #include #endif How can I do such a thing? I know that _WIN32 is…
Lefteris
  • 3,196
  • 5
  • 31
  • 52
42
votes
1 answer

Swift preprocessor if not flag

Is there a Swift compiler directive for #if not? I know this could work #if myFlag // ignore #else bar() #endif But it is not pretty if there is nothing between if and else.
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
39
votes
6 answers

What's the meaning of #line in C language?

What's the meaning of #line in the C language? Where would it be used?
Yongwei Xing
  • 12,983
  • 24
  • 70
  • 90
33
votes
2 answers

Why #if 0 vs #if (1 > 1)?

I am working with a legacy code and found this: #if (1 > 1) //define some function #endif Not sure, how this can be any different from the more typical #if 0, to comment out the code? Any thoughts?
MIbrah
  • 1,007
  • 1
  • 9
  • 17
1
2 3
39 40