131 Week 9 - Preprocessor Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Role of the preprocessor

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

C processor

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Initial processing

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Tokenization

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the pre-processing language consist of

A

directives to be executed and macros to be expanded.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Directives and macros e.g.,

A

in #define BUFFER_SIZE 1024
#define is the directive and BUFFER_SIZE 1024 is the macro

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the capabilities of the pre-processing language

A

Macro expansion, conditional compilation, diagnostics, Inclusion of header files and controlling the compiler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Macro expansion: object-like macros

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Macro expansion: function-like macros

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Macro expansion: stringification/stringising

A

= stringification/stringise/number-sign operator.
Using # before a macro parameter converts the macro parameter into a string literal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Macro expansion: undefining macros

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Conditional compilation

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Conditional compilation: testing if macros are defined

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Diagnostics: predefined macros

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Inclusion of header files

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Control the compiler: double inclusion

A

A header file can sometimes be included twice which results in either an error or increased compilation time.
This can be stopped by having #include directives in a conditional.

17
Q

Control the compiler: computed #include

A

Computed #include’s specifies which header to include externally as a compiler option.
e.g.,
#include SYSTEM_H in the code
then gcc -DSYSTEM_H = ‘ “system_1.h” ‘ -o … to determine which header file to use.