131 Week 9 - Preprocessor Flashcards
Role of the preprocessor
Identify all lines with a # (e.g., #include or #define) and resolve them to be in C
e.g., pre-processor finds and copies the headers to the program for an #include.
C processor
Often referred to as CPP.
Intended to be used only with C, C++, and Objective-C source code.
Called a macro processor, because it allows you to define macros - brief abbreviations for longer constructs.
Initial processing
- Input file is read into memory and broken into lines.
- Continued lines (lines ending in ) are merged into 1 long line - backslash is removed and lines joined to become one line.
- All comments are replaced with single spaces
Tokenization
input C file split in pre-processing tokens: identifiers (sequence of letters numbers and underscores), numbers (int and floats) and string literals (string/character constants and header file names)
What does the pre-processing language consist of
directives to be executed and macros to be expanded.
Directives and macros e.g.,
in #define BUFFER_SIZE 1024
#define is the directive and BUFFER_SIZE 1024 is the macro
What are the capabilities of the pre-processing language
Macro expansion, conditional compilation, diagnostics, Inclusion of header files and controlling the compiler.
Macro expansion: object-like macros
Replaces an identifier with a code fragment.
Macros are defined using #define directive.
#define must be followed by a macro name (conventionally written un uppercase) and a macro body (the intended expansion of the macro).
Macro expansion: function-like macros
Macros that look like a function call.
Defined similar to object-like macros but the name must end with () which includes arguments if function has them.
Macro expansion: stringification/stringising
= stringification/stringise/number-sign operator.
Using # before a macro parameter converts the macro parameter into a string literal.
Macro expansion: undefining macros
Used to undefine an existing macro.
Undefine a macro using #undef macroName
Macros can be redefined after being undefined using #define as usual.
Have to undefine a macro before it can be redefined.
Conditional compilation
Use #if to test a condition and execute code if the condition is true.
Use #elif to test a new condition if the main condition was false.
Use #else as a base case to execute code if all previous conditions are false.
Conditional compilation: testing if macros are defined
Can use defined(macroName) to return if a macro is defined or not, can be used in a condition.
#ifdef macroName will execute code if the macro is defined.
#ifNdef macroName will execute code if the macro is not defined.
Diagnostics: predefined macros
Pre-processor has predefined macros which can be used in pre-processor code.
e.g., __FILE__ expands to name of the source file being compiled and __LINE__ expands to current input line number of the source file that is being compiled.
Inclusion of header files
Include using #include directive then the header file name
header file name is surrounded by <> if it is a system file or “” if not.
During compilation, the pre-processor will replace all #include statements with the headers for the associated functions and create a .i file which can be passed to the compiler.