### Initialize command array Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Concatenation.html Example of an array of command structures. ```c struct command commands[] = { { "quit", quit_command }, { "help", help_command }, … }; ``` -------------------------------- ### Header file example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Include-Operation.html A simple header file containing a function declaration. ```c char *test (void); ``` -------------------------------- ### Example of #embed Expansion Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Binary-Resource-Inclusion.html Demonstrates how the preprocessor expands the directive into a sequence of integer literals. ```c #embed __FILE__ limit(4) ``` -------------------------------- ### Compile with search include path Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Invocation.html Example command for compiling C++20 modules using the -fsearch-include-path option. ```bash g++ -c -std=c++20 -fmodules -fsearch-include-path bits/stdc++.h bits/std.cc ``` -------------------------------- ### Macro Argument Substitution Example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Misnesting.html Demonstrates how a macro call can be constructed by substituting an argument into a macro body. ```c #define twice(x) (2*(x)) #define call_with_1(x) x(1) call_with_1 (twice) → twice(1) → (2*(1)) ``` -------------------------------- ### Include user header files Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Include-Syntax.html Use quotes to search for files starting in the current directory, followed by quote directories and system directories. ```cpp #include "file" ``` -------------------------------- ### Specify Dependency Target with -MT Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Invocation.html Example output showing a custom target defined using the -MT option. ```makefile $(objpfx)foo.o: foo.c ``` -------------------------------- ### Use #include_next directive Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Wrapper-Headers.html The #include_next directive searches for the specified file starting from the directory after the current file's directory. ```c #include_next ``` -------------------------------- ### Generate Phony Targets with -MP Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Invocation.html Example output generated when using the -MP option to create dummy rules for header files. ```makefile test.o: test.c test.h test.h: ``` -------------------------------- ### Pathological Macro Redefinition Example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Directives-Within-Macro-Arguments.html Demonstrates the behavior of redefining a macro during its own invocation. ```c #define f(x) x x f (1 #undef f #define f 2 f) ``` ```text 1 2 1 2 ``` -------------------------------- ### Handling empty and invalid macro arguments Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Macro-Arguments.html Examples of passing empty arguments to a macro and the resulting errors when the argument count does not match the definition. ```c min(, b) → (( ) < (b) ? ( ) : (b)) min(a, ) → ((a ) < ( ) ? (a ) : ( )) min(,) → (( ) < ( ) ? ( ) : ( )) min((,),) → (((,)) < ( ) ? ((,)) : ( )) min() error→ macro "min" requires 2 arguments, but only 1 given min(,,) error→ macro "min" passed 3 arguments, but takes just 2 ``` -------------------------------- ### Unbalanced Parentheses Macro Example Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Misnesting.html Shows how an unbalanced parenthesis in a macro body allows a call to span across the macro invocation. ```c #define strange(file) fprintf (file, "%s %d", … strange(stderr) p, 35) → fprintf (stderr, "%s %d", p, 35) ``` -------------------------------- ### Conflicting Macro Definitions Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Undefining-and-Redefining-Macros.html Examples of macro redefinitions that are not effectively the same, which will trigger a preprocessor warning. ```cpp #define FOUR (2 + 2) #define FOUR ( 2+2 ) #define FOUR (2 * 2) #define FOUR(score,and,seven,years,ago) (2 + 2) ``` -------------------------------- ### Test for machine endianness Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Common-Predefined-Macros.html Example of using the __BYTE_ORDER__ macro to check for a little-endian machine architecture. ```c /* Test for a little-endian machine */ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ``` -------------------------------- ### Effectively Same Macro Definitions Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Undefining-and-Redefining-Macros.html Examples of macro redefinitions that are considered effectively the same by the preprocessor, allowing them to be redefined without warnings. ```cpp #define FOUR (2 + 2) #define FOUR (2 + 2) #define FOUR (2 /* two */ + 2) ``` -------------------------------- ### Handling Endif Labels Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Invocation.html Example of legacy code patterns where text follows #else or #endif directives, which triggers a warning by default. ```cpp #if FOO … #else FOO … #endif FOO ``` -------------------------------- ### Specify Quoted Dependency Target with -MQ Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Invocation.html Example output showing a target defined using the -MQ option, which automatically quotes special characters for Make. ```makefile $$(objpfx)foo.o: foo.c ``` -------------------------------- ### Inspect Default Search Directories Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Search-Path.html Use the -v option to display the preprocessor's current search path configuration. ```bash cpp -v /dev/null -o /dev/null ``` -------------------------------- ### Main program using #include Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Include-Operation.html A C source file that includes a header file using the #include directive. ```c int x; #include "header.h" int main (void) { puts (test ()); } ``` -------------------------------- ### Non-portable __has_attribute usage Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/_005f_005fhas_005fattribute.html Example of an invalid approach that may fail on compilers lacking the operator. ```c #if defined __has_attribute && __has_attribute (nonnull) /* not portable */ … #endif ``` -------------------------------- ### Defining a Macro Without Parentheses Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Operator-Precedence-Problems.html An example of a macro definition that is susceptible to operator precedence errors. ```c #define ceil_div(x, y) (x + y - 1) / y ``` -------------------------------- ### List Predefined Macros Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Invocation.html Displays all macros defined during preprocessor execution by creating an empty file and running the -dM flag. ```bash touch foo.h; cpp -dM foo.h ``` -------------------------------- ### Non-portable header inclusion check Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/_005f_005fhas_005finclude.html Example of a combined expression that is not portable because it assumes the compiler supports the operator. ```cpp #if defined __has_include && __has_include ("header.h") /* not portable */ … #endif ``` -------------------------------- ### Basic #embed Syntax Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Binary-Resource-Inclusion.html Standard syntax for including binary files using angle brackets or double quotes. ```c #embed ``` ```c #embed "file" ``` -------------------------------- ### Include system header files Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Include-Syntax.html Use angle brackets to search for files in standard system directories, which can be extended via the -I option. ```cpp #include ``` -------------------------------- ### Test an assertion in a conditional Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Obsolete-Features.html Example of using an assertion within an #if directive to check for specific machine types. ```cpp #if #machine (vax) || #machine (ns16000) ``` -------------------------------- ### Splitting tokens with backslash-newline Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Initial-processing.html Demonstrates how backslash-newline sequences can be used to split preprocessor directives across multiple lines. ```c /\ * */ # /* */ defi\ ne FO\ O 10\ 20 ``` -------------------------------- ### Include header with absolute path Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Wrapper-Headers.html An alternative to wrapper headers using an absolute path to reference the original file. ```c #include "/usr/include/old-header.h" ``` -------------------------------- ### Basic Computed Include Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Computed-Includes.html Using a macro to define the header filename for inclusion. ```c #define SYSTEM_H "system_1.h" … #include SYSTEM_H ``` -------------------------------- ### Macro Usage with Side Effects Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Duplication-of-Side-Effects.html Example of a macro call where an argument containing a function call is evaluated twice. ```c next = min (x + y, foo (z)); ``` -------------------------------- ### Coexistence of macro and function Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Function-like-Macros.html Demonstrates how a macro is only expanded when followed by parentheses, allowing the function name to be used as a pointer. ```c extern void foo(void); #define foo() /* optimized inline version */ … foo(); funcptr = foo; ``` -------------------------------- ### Nested macro expansion Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Macro-Arguments.html Shows the multi-step expansion process for nested macro calls. ```c min (((a) < (b) ? (a) : (b)), (c)) ``` ```c ((((a) < (b) ? (a) : (b))) < (c) ? (((a) < (b) ? (a) : (b))) : (c)) ``` -------------------------------- ### Sequential macro definition behavior Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Object-like-Macros.html Demonstrates that macro definitions take effect at the point they are written in the source file. ```c foo = X; #define X 4 bar = X; ``` ```c foo = X; bar = 4; ``` -------------------------------- ### Token Concatenation with Macros Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Tokenization.html Demonstrates how the preprocessor handles token boundaries and the effect of macro expansion on concatenation. ```c #define foo() bar foo()baz → bar baz _not_ → barbaz ``` -------------------------------- ### Using the _Pragma operator Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Pragmas.html Demonstrates how to use the _Pragma operator to embed pragmas within macros. ```c _Pragma ("GCC dependency \"parse.y\"") ``` ```c #define DO_PRAGMA(x) _Pragma (#x) DO_PRAGMA (GCC dependency "parse.y") ``` -------------------------------- ### Conditional Header Inclusion Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Computed-Includes.html A standard approach using preprocessor conditionals to select a header file. ```c #if SYSTEM_1 # include "system_1.h" #elif SYSTEM_2 # include "system_2.h" #elif SYSTEM_3 … #endif ``` -------------------------------- ### Dependency Generation Options Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Invocation.html Options for generating makefile-compatible dependency rules during preprocessing. ```APIDOC ## Dependency Generation Options ### Description These options control how the preprocessor generates dependency rules for build systems like `make`. ### Options - **--dependencies (-M)**: Outputs a make rule describing dependencies of the main source file. - **--user-dependencies (-MM)**: Similar to -M, but excludes system header files. - **-MF file**: Specifies the output file for dependency rules. - **-MG**: Assumes missing header files are generated files and adds them to the dependency list. - **-Mno-modules**: Disables dependency generation for compiled module interfaces. - **-MP**: Adds a phony target for each dependency to prevent errors when header files are removed. - **-MT target**: Sets the target of the dependency rule. - **-MQ target**: Same as -MT, but quotes characters special to Make. - **-MD**: Equivalent to -M -MF, but does not imply -E (preprocessor output). - **-MMD**: Equivalent to -MM -MF, but does not imply -E. ``` -------------------------------- ### Demonstrate macro usage error Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Swallowing-the-Semicolon.html Example of how a trailing semicolon after a compound statement macro causes invalid C code when used before an else statement. ```c if (*p != 0) SKIP_SPACES (p, lim); else … ``` -------------------------------- ### Token Pasting via Comments Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Traditional-macros.html Demonstrates using comments to effect token pasting in traditional macro expansion. ```c #define suffix(x) foo_/**/x suffix(bar) → foo_bar ``` -------------------------------- ### C Preprocessor Digraphs and Punctuators Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Tokenization.html Mapping of C/C++ digraphs to their corresponding standard punctuators. ```text Digraph: <% %> <: :> %: %:%: Punctuator: { } [ ] # ## ``` -------------------------------- ### Recursive Macro Expansion Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Traditional-macros.html Demonstrates how traditional macros expand to text and can result in concatenated tokens. ```c #define PLUS + #define INC(x) PLUS+x INC(foo); → ++foo; ``` -------------------------------- ### Testing binary resource inclusion with __has_embed Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/_005f_005fhas_005fembed.html Use this pattern to verify if a resource and its parameters are supported before attempting an #embed directive. ```cpp #ifdef __has_embed #if __has_embed (__FILE__ limit (4) myvendor::myattr (42)) != __STDC_EMBED_NOT_FOUND__ #embed "foo.dat" limit (4) myvendor::myattr (42) #endif #endif ``` -------------------------------- ### Define and use a simple object-like macro Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Object-like-Macros.html Defines a macro for a numeric constant and demonstrates its expansion in a C statement. ```c #define BUFFER_SIZE 1024 ``` ```c foo = (char *) malloc (BUFFER_SIZE); ``` ```c foo = (char *) malloc (1024); ``` -------------------------------- ### Define and invoke a min macro Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Macro-Arguments.html Demonstrates a standard macro definition for finding the minimum of two values and its expansion with various argument types. ```c #define min(X, Y) ((X) < (Y) ? (X) : (Y)) x = min(a, b); → x = ((a) < (b) ? (a) : (b)); y = min(1, 2); → y = ((1) < (2) ? (1) : (2)); z = min(a + 28, *p); → z = ((a + 28) < (*p) ? (a + 28) : (*p)); ``` -------------------------------- ### Macro Expansion with Concatenation Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Argument-Prescan.html Demonstrates using an intermediate macro to force expansion of arguments before concatenation. ```c #define AFTERX(x) X_ ## x #define XAFTERX(x) AFTERX(x) #define TABLESIZE 1024 #define BUFSIZE TABLESIZE ``` -------------------------------- ### Stringizing Arguments in Traditional Macros Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Traditional-macros.html Illustrates how traditional preprocessors replace parameters within quotes to achieve stringization. ```c #define str(x) "x" str(/* A comment */some text ) → "some text " ``` -------------------------------- ### Testing for Built-in Function Support Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/_005f_005fhas_005fbuiltin.html Recommended pattern for checking if __has_builtin is supported and conditionally defining a wrapper for a specific built-in function. ```c #if defined __has_builtin # if __has_builtin (__builtin_object_size) # define builtin_object_size(ptr) __builtin_object_size (ptr, 2) # endif #endif #ifndef builtin_object_size # define builtin_object_size(ptr) ((size_t)-1) #endif ``` -------------------------------- ### Macro for command definition Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Concatenation.html Using stringizing and concatenation to simplify command table definitions. ```c #define COMMAND(NAME) { #NAME, NAME ## _command } struct command commands[] = { COMMAND (quit), COMMAND (help), … }; ``` -------------------------------- ### Demonstrate a syntax error with trailing commas Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Variadic-Macros.html Shows how omitting variable arguments can lead to a trailing comma syntax error. ```c eprintf("success!\n", ); → fprintf(stderr, "success!\n", ); ``` -------------------------------- ### Recommended Macro Definition Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Operator-Precedence-Problems.html The final recommended way to define the macro using full parenthesization to avoid all precedence issues. ```c #define ceil_div(x, y) (((x) + (y) - 1) / (y)) ``` -------------------------------- ### Report configuration inconsistency Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Diagnostics.html Use #error to validate that required configuration parameters are set correctly relative to one another. ```cpp #if !defined(FOO) && defined(BAR) #error "BAR requires FOO." #endif ``` -------------------------------- ### Define and invoke a function-like macro Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Function-like-Macros.html Defines a simple function-like macro and shows its expansion. ```c #define lang_init() c_init() lang_init() → c_init() ``` -------------------------------- ### Define command structure Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Concatenation.html Structure definition for command mapping. ```c struct command { char *name; void (*function) (void); }; ``` -------------------------------- ### Nested #if and #else structure Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Elif.html Demonstrates the verbose approach to checking multiple conditions using nested #if and #else directives. ```c #if X == 1 … #else /* X != 1 */ #if X == 2 … #else /* X != 2 */ … #endif /* X != 2 */ #endif /* X != 1 */ ``` -------------------------------- ### Using the #else directive Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Else.html Provides an alternative block of code to be processed when the initial conditional expression evaluates to zero. ```c #if expression text-if-true #else /* Not expression */ text-if-false #endif /* Not expression */ ``` -------------------------------- ### __has_include and __has_include_next Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/_005f_005fhas_005finclude.html Operators used to test if a header file can be included. They evaluate to a non-zero value if the header is available and zero otherwise. ```APIDOC ## __has_include and __has_include_next ### Description These operators are used within '#if' and '#elif' expressions to determine if a header file can be successfully included. They are designed to be used with the same syntax as the '#include' and '#include_next' directives. ### Usage - `__has_include(operand)`: Tests if the specified header can be included. - `__has_include_next(operand)`: Tests if the specified header can be included using the '#include_next' directive. ### Example ```c #if defined __has_include # if __has_include () # include # endif #endif ``` ``` -------------------------------- ### Define a basic variadic macro Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Variadic-Macros.html Uses the ellipsis (...) syntax to accept variable arguments, which are accessed via __VA_ARGS__. ```c #define eprintf(...) fprintf (stderr, __VA_ARGS__) ``` -------------------------------- ### Trace indirect macro expansion Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Self-Referential-Macros.html Shows the expansion steps for indirectly self-referential macros. ```text x → (4 + y) → (4 + (2 * x)) y → (2 * x) → (2 * (4 + y)) ``` -------------------------------- ### Checking file dependencies with #pragma GCC dependency Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Pragmas.html Checks the relative dates of files and issues warnings if the dependency is newer. ```c #pragma GCC dependency "parse.y" #pragma GCC dependency "/usr/include/time.h" rerun fixincludes ``` -------------------------------- ### Linemarker Syntax Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Preprocessor-Output.html The format used by the preprocessor to convey source file name and line number information in the output. ```text # linenum filename flags ``` -------------------------------- ### Portable __has_attribute usage Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/_005f_005fhas_005fattribute.html Recommended pattern for checking attribute support by first verifying the operator's existence. ```c #if defined __has_attribute # if __has_attribute (nonnull) # define ATTR_NONNULL __attribute__ ((nonnull)) # endif #endif ``` -------------------------------- ### Advanced #embed with Parameters Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Binary-Resource-Inclusion.html Usage of standard parameters including prefix and suffix for custom formatting of the embedded data. ```c #embed "foo.dat" limit(1) __prefix__(32, ) suffix(, 0) ``` -------------------------------- ### Define a robust macro using do-while Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Swallowing-the-Semicolon.html The recommended approach for defining statement-like macros by wrapping the body in a do-while(0) loop to safely handle trailing semicolons. ```c #define SKIP_SPACES(p, limit) \ do { char *lim = (limit); \ while (p < lim) { \ if (*p++ != ' ') { \ p--; break; }}} \ while (0) ``` -------------------------------- ### Standard License Notice Template Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/GNU-Free-Documentation-License.html Use this template for documents without Invariant Sections or Cover Texts. ```text Copyright (C) year your name. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled ``GNU Free Documentation License''. ``` -------------------------------- ### NUL byte behavior in preprocessor directives Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Tokenization.html Demonstrates that NUL bytes are treated as whitespace in directives, making the two definitions equivalent. ```c #define X^@1 #define X 1 ``` -------------------------------- ### Define a variadic macro with named and variable arguments Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Variadic-Macros.html Combines a mandatory format string with optional variable arguments. ```c #define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__) ``` -------------------------------- ### Demonstrate variable argument omission Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Variadic-Macros.html Shows the behavior when the variable argument is omitted in C++20 or GNU CPP. ```c eprintf ("success!\n") → fprintf(stderr, "success!\n", ); ``` -------------------------------- ### Syntax of the #if directive Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/If.html The basic structure for conditional compilation using the #if directive. ```c #if expression controlled text #endif /* expression */ ``` -------------------------------- ### Report errors with __FILE__ and __LINE__ Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Standard-Predefined-Macros.html Use these macros to include the current file path and line number in diagnostic messages. ```c fprintf (stderr, "Internal error: " "negative string length " "%d at %s, line %d.", length, __FILE__, __LINE__); ``` -------------------------------- ### Expand a variadic macro Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Variadic-Macros.html Shows the expansion of a variadic macro call into a standard function call. ```c eprintf ("%s:%d: ", input_file, lineno) → fprintf (stderr, "%s:%d: ", input_file, lineno) ``` -------------------------------- ### Handling Unshielded Commas in Macro Arguments Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Argument-Prescan.html Illustrates how unshielded commas in macro arguments can cause incorrect argument counts during expansion. ```c #define foo a,b #define bar(x) lose(x) #define lose(x) (1 + (x)) ``` -------------------------------- ### Test macro existence and value Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Defined.html Checks if BUFSIZE is defined and meets a minimum value requirement. ```c #if defined BUFSIZE && BUFSIZE >= 1024 ``` -------------------------------- ### cpp [options] infile outfile Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Invocation.html The primary command to invoke the C preprocessor. It reads from an input file and writes to an output file, supporting various options for macro manipulation and file processing. ```APIDOC ## cpp [options] infile outfile ### Description Invokes the C preprocessor to process source files. The preprocessor reads the input file, processes directives, and writes the result to the output file. ### Parameters #### Positional Arguments - **infile** (string) - Required - The source file to process. Use '-' for standard input. - **outfile** (string) - Optional - The destination file for output. Use '-' for standard output. If omitted, defaults to standard output. #### Options - **-D name[=definition]** (string) - Optional - Predefine a macro. If definition is omitted, it defaults to 1. - **-U name** (string) - Optional - Cancel any previous definition of the specified macro. - **-include file** (string) - Optional - Process the specified file as if it were included at the start of the source file. - **-imacros file** (string) - Optional - Process the specified file for macro definitions, but discard any output produced by it. - **-undef** (flag) - Optional - Do not predefine system-specific or GCC-specific macros. - **-pthread** (flag) - Optional - Define macros required for the POSIX threads library. ``` -------------------------------- ### Macro Expansion Precedence Error Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Operator-Precedence-Problems.html Demonstrates how a macro expansion can lead to incorrect operator grouping. ```c a = ceil_div (b & c, sizeof (int)); → a = (b & c + sizeof (int) - 1) / sizeof (int); ``` ```c a = (b & (c + sizeof (int) - 1)) / sizeof (int); ``` ```c a = ((b & c) + sizeof (int) - 1)) / sizeof (int); ``` -------------------------------- ### Portable header inclusion check Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/_005f_005fhas_005finclude.html Recommended pattern for checking if a header exists before including it, ensuring compatibility with compilers that do not support the operator. ```cpp #if defined __has_include # if __has_include () # include # endif #endif ``` -------------------------------- ### Block Comment Nesting Behavior Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Initial-processing.html Demonstrates that block comments do not support nesting, causing the first closing delimiter to terminate the comment. ```c /* this is /* one comment */ text outside comment ``` -------------------------------- ### Test for any assertion answer Source: https://gcc.gnu.org/onlinedocs/gcc-16.2.0/cpp/Obsolete-Features.html Syntax to check if any answer exists for a given predicate. ```cpp #if #machine ```