Questions tagged [c-preprocessor]

A textual macro processor applied before compiling C and C++ language programs. This tag can also be used for questions about other compilers/languages derived from or that have identical features, such as the #directives in Objective-C or C#.

The C and C++ programming languages feature a preparatory phase before proper compilation, named preprocessing, which provides the ability for textual inclusion of header files, recursive macro expansions, conditional compilation, use of various constants not defined in the source nor the language itself, and line control.

This step may be provided as a standalone executable, traditionally named cpp. The preprocessing rules are slightly different for C than for C++; but common compiler collections like and the LLVM-based use a single preprocessor for both languages, which can be configured to choose the appropriate rule-set.

Resources

The GNU CPP online manual describes the syntax and use of the GNU implementation of the preprocessor.

The Pre-defined C/C++ Compiler Macros project lists pre-defined compiler macros that can be used to identify standards, compilers, operating systems and hardware architectures at compile-time.

The Microsoft C# Compiler does not have a separate preprocessor, but it has similar directives and operates as if it did.

5830 questions
3045
votes
30 answers

What is the difference between #include and #include "filename"?

What is the difference between using angle brackets and quotes in an include directive? #include #include "filename"
quest49
  • 49,608
  • 5
  • 21
  • 14
1105
votes
5 answers

Why does the C preprocessor interpret the word "linux" as the constant "1"?

Why does the C preprocessor in GCC interpret the word linux (small letters) as the constant 1? test.c: #include int main(void) { int linux = 5; return 0; } Result of $ gcc -E test.c (stop after the preprocessing…
ahmedaly50
  • 7,765
  • 3
  • 12
  • 7
924
votes
9 answers

Why use apparently meaningless do-while and if-else statements in macros?

In many C/C++ macros I'm seeing the code of the macro wrapped in what seems like a meaningless do while loop. Here are examples. #define FOO(X) do { f(X); g(X); } while (0) #define FOO(X) if (1) { f(X); g(X); } else I can't see what the do while…
jfm3
  • 36,964
  • 10
  • 32
  • 35
702
votes
5 answers

Why are #ifndef and #define used in C++ header files?

I have been seeing code like this usually in the start of header files: #ifndef HEADERFILE_H #define HEADERFILE_H And at the end of the file is #endif What is the purpose of this?
Asad Khan
  • 11,469
  • 13
  • 44
  • 59
441
votes
3 answers

Why do all the C files written by my lecturer start with a single # on the first line?

I'm going through some C course notes, and every C program source file begins with a single # on the first line of the program. Then there are blank lines, and following that other stuff followed by the main function. What is the reason for the…
The Main Man
  • 2,433
  • 2
  • 9
  • 8
408
votes
6 answers

Define preprocessor macro through CMake?

How do I define a preprocessor variable through CMake? The equivalent code would be #define foo.
Mythli
  • 5,995
  • 2
  • 24
  • 31
407
votes
16 answers

MIN and MAX in C

Where are MIN and MAX defined in C, if at all? What is the best way to implement these, as generically and type safely as possible? (Compiler extensions/builtins for mainstream compilers preferred.)
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
392
votes
4 answers

Combining C++ and C - how does #ifdef __cplusplus work?

I'm working on a project that has a lot of legacy C code. We've started writing in C++, with the intent to eventually convert the legacy code, as well. I'm a little confused about how the C and C++ interact. I understand that by wrapping the C…
dublev
  • 4,257
  • 4
  • 18
  • 12
342
votes
3 answers

How to detect reliably Mac OS X, iOS, Linux, Windows in C preprocessor?

If there's some cross-platform C/C++ code that should be compiled on Mac OS X, iOS, Linux, Windows, how can I detect them reliably during preprocessor process?
eonil
  • 83,476
  • 81
  • 317
  • 516
306
votes
17 answers

How do I check OS with a preprocessor directive?

I need my code to do different things based on the operating system on which it gets compiled. I'm looking for something like this: #ifdef OSisWindows // do Windows-specific stuff #else // do Unix-specific stuff #endif Is there a way to do this? Is…
perimosocordiae
  • 17,287
  • 14
  • 60
  • 76
300
votes
6 answers

GCC dump preprocessor defines

Is there a way for gcc/g++ to dump its default preprocessor defines from the command line? I mean things like __GNUC__, __STDC__, and so on.
Anycorn
  • 50,217
  • 42
  • 167
  • 261
294
votes
11 answers

#pragma pack effect

I was wondering if someone could explain to me what the #pragma pack preprocessor statement does, and more importantly, why one would want to use it. I checked out the MSDN page, which offered some insight, but I was hoping to hear more from people…
Cenoc
  • 11,172
  • 21
  • 58
  • 92
255
votes
14 answers

#define macro for debug printing in C?

Trying to create a macro which can be used for print debug messages when DEBUG is defined, like the following pseudo code: #define DEBUG 1 #define debug_print(args ...) if (DEBUG) fprintf(stderr, args) How is this accomplished with a macro?
jfarrell
  • 4,480
  • 3
  • 21
  • 14
244
votes
10 answers

Overloading Macro on Number of Arguments

I have two macros FOO2 and FOO3: #define FOO2(x,y) ... #define FOO3(x,y,z) ... I want to define a new macro FOO as follows: #define FOO(x,y) FOO2(x,y) #define FOO(x,y,z) FOO3(x,y,z) But this doesn't work because macros do not overload on number of…
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
232
votes
6 answers

How to make a variadic macro (variable number of arguments)

I want to write a macro in C that accepts any number of parameters, not a specific number example: #define macro( X ) something_complicated( whatever( X ) ) where X is any number of parameters I need this because whatever is overloaded and can be…
hasen
  • 161,647
  • 65
  • 194
  • 231
1
2 3
99 100