The C pre-processor Flashcards

(42 cards)

1
Q

What is the C preprocessor?

A

A tool that rewrites source files with macro expansion before compilation.

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

What is the general flow of C compilation with the preprocessor?

A

Source (.c) → Preprocessor (cpp) → Rewritten source → Compiler → Object code (.o)

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

What does the #include <stdio.h> directive do?</stdio.h>

A

It finds the file called stdio.h and copies its contents into the current position in the program, replacing the directive line.

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

What’s the difference between #include <stdio.h> and #include "myFile.h"?</stdio.h>

A

<stdio.h> searches standard system paths, while "myFile.h" first searches in the same directory as the program, then standard paths.
</stdio.h>

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

What does the #define directive do?

A

Defines a macro that replaces any instances of the macro name with its definition.

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

In what scenario are #define macro replacements not applied?

A

In quoted strings. For example, printf(“It is an ELEPHANT”) will not replace “ELEPHANT” with its defined value.

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

What potential issue can occur with macros like #define ADD(x,y) x+y?

A

Unexpected results due to operator precedence. For example, ADD(12,n) becomes 12+n, and ADD(12,n)5 becomes 12+n5.

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

How can you fix the potential issues with macro expansion?

A

By using brackets around the entire expression and each parameter: #define MAX(a,b) ((a)>(b)?(a):(b))

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

What issue can still occur with macros even with proper bracketing?

A

Side effects can be duplicated. For example, with MAX(i++,j++), parameters might be evaluated multiple times.

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

What is the common naming convention for macros in C?

A

All capitals (e.g., #define TRUE 1).

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

How can you create a Boolean datatype in ANSI C?

A

Using macros and typedef:
#define TRUE 1
#define FALSE 0
typedef char BOOLEAN;

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

What does the # (stringification) operator do in a macro?

A

Converts a macro argument to a string literal.

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

What is the output of REPORT_float(pi) where #define REPORT_float(FOO) fprintf(stderr, “%s is %f\n”, #FOO, FOO)?

A

pi is 3.141593

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

What directives are used for selective compilation?

A

ifdef, #ifndef, #undef, #else, #elif, #endif

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

What is the purpose of #ifdef and #ifndef?

A

ifdef checks if a symbol is defined, and #ifndef checks if a symbol is not defined, for conditional compilation.

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

What does __FILE__ represent in C code?

A

The current file name

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

What does __LINE__ represent in C code?

A

The current line number in the file

18
Q

What does __DATE__ represent in C code?

A

The date when the object code was generated from the source

19
Q

What does __TIME__ represent in C code?

A

The time when the object code was generated from the source.

20
Q

What does __STDC__ represent in C code?

A

It equals 1 if the implementation of C conforms to the ANSI/ISO standard; any other value means not.

21
Q

Why do system macros typically start with two underscores?

A

So they don’t interfere with user-defined macros and can be easily recognized.

22
Q

What are the benefits of structuring C programs into separate modules?

A

Makes code more reliable and easier to read, enables code reuse, provides functionality for other programs, and allows sharing code across platforms.

23
Q

What typically goes in a header (.h) file?

A

includes, structure definitions, typedefs, and function prototypes.

24
Q

What typically goes in a code (.c) file?

A

includes (including its header file), function definitions, and potentially a main function.

25
What naming convention should be followed for header files?
The header (.h) file is ALWAYS named after the code (.c) file it belongs to.
26
What does the static storage class mean for functions?
The function is only visible within its "translation unit" (i.e., its .c file).
27
What does the extern storage class mean for functions?
The function is visible globally, across all files.
28
What does the auto storage class mean for variables?
Storage is allocated when the block is entered and automatically freed when the block is exited (default).
29
What does the register storage class mean for variables?
Suggests the variable be stored in CPU registers for quick access.
30
What does the static storage class mean for local variables?
The variable continues to exist even after the block in which it is defined terminates, and its value is retained between calls.
31
What is the problem with multiple includes of the same header file?
It can lead to redefinition errors for structures, types, and other definitions.
32
How do #include guards prevent multiple inclusion issues?
By using preprocessor conditionals to check if a unique symbol is defined: #ifndef __HEADER_H #define __HEADER_H // Content of header file #endif
33
What is the difference between static libraries (.a) and dynamically linked shared object libraries (.so)?
Static libraries are linked with and become part of the application at link time, while dynamic libraries are linked at runtime.
34
How do you compile a C file without linking it?
gcc -c filename.c -o filename.o
35
How do you link multiple object files into an executable?
gcc file1.o file2.o -o programName
36
How do you link to a library like the math library?
Using the -l flag: gcc myMaths.o -lm -o myMaths
37
What tool helps manage dependencies and compilation of C projects?
The make utility with a Makefile.
38
What are the three main components of a Makefile rule?
Targets, dependencies, and commands.
39
What happens when you run make on a Makefile?
It checks dependencies, and recompiles/relinks only what's necessary based on modification times.
40
What does the following Makefile line mean: myProgram: myProgram.o stack.o?
The target myProgram depends on myProgram.o and stack.o.
41
How can you specify standard compilation flags in a Makefile?
Using variables like CC = gcc and CFLAGS = -ansi.
42
What is the purpose of a clean target in a Makefile?
To remove generated files (like object files and executables) for a clean build.