The C pre-processor Flashcards
(42 cards)
What is the C preprocessor?
A tool that rewrites source files with macro expansion before compilation.
What is the general flow of C compilation with the preprocessor?
Source (.c) → Preprocessor (cpp) → Rewritten source → Compiler → Object code (.o)
What does the #include <stdio.h> directive do?</stdio.h>
It finds the file called stdio.h and copies its contents into the current position in the program, replacing the directive line.
What’s the difference between #include <stdio.h> and #include "myFile.h"?</stdio.h>
<stdio.h> searches standard system paths, while "myFile.h" first searches in the same directory as the program, then standard paths.
</stdio.h>
What does the #define directive do?
Defines a macro that replaces any instances of the macro name with its definition.
In what scenario are #define macro replacements not applied?
In quoted strings. For example, printf(“It is an ELEPHANT”) will not replace “ELEPHANT” with its defined value.
What potential issue can occur with macros like #define ADD(x,y) x+y?
Unexpected results due to operator precedence. For example, ADD(12,n) becomes 12+n, and ADD(12,n)5 becomes 12+n5.
How can you fix the potential issues with macro expansion?
By using brackets around the entire expression and each parameter: #define MAX(a,b) ((a)>(b)?(a):(b))
What issue can still occur with macros even with proper bracketing?
Side effects can be duplicated. For example, with MAX(i++,j++), parameters might be evaluated multiple times.
What is the common naming convention for macros in C?
All capitals (e.g., #define TRUE 1).
How can you create a Boolean datatype in ANSI C?
Using macros and typedef:
#define TRUE 1
#define FALSE 0
typedef char BOOLEAN;
What does the # (stringification) operator do in a macro?
Converts a macro argument to a string literal.
What is the output of REPORT_float(pi) where #define REPORT_float(FOO) fprintf(stderr, “%s is %f\n”, #FOO, FOO)?
pi is 3.141593
What directives are used for selective compilation?
ifdef, #ifndef, #undef, #else, #elif, #endif
What is the purpose of #ifdef and #ifndef?
ifdef checks if a symbol is defined, and #ifndef checks if a symbol is not defined, for conditional compilation.
What does __FILE__ represent in C code?
The current file name
What does __LINE__ represent in C code?
The current line number in the file
What does __DATE__ represent in C code?
The date when the object code was generated from the source
What does __TIME__ represent in C code?
The time when the object code was generated from the source.
What does __STDC__ represent in C code?
It equals 1 if the implementation of C conforms to the ANSI/ISO standard; any other value means not.
Why do system macros typically start with two underscores?
So they don’t interfere with user-defined macros and can be easily recognized.
What are the benefits of structuring C programs into separate modules?
Makes code more reliable and easier to read, enables code reuse, provides functionality for other programs, and allows sharing code across platforms.
What typically goes in a header (.h) file?
includes, structure definitions, typedefs, and function prototypes.
What typically goes in a code (.c) file?
includes (including its header file), function definitions, and potentially a main function.