Week 5 - C Preprocessor and Program Structure Flashcards

1
Q

What 4 things do C compiler directives do?

A

Giving your programs access to useful library functions and values

Controlling what code gets compiled

Defining macros to make your code more readable

Defining macros for values that might change some time in the future

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

In C, each source file is pre-processed by the C preprocessor. What does this mean?

A

It rewrites the file with macro expansion

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

What is macro expansion in C?

A

Finding the header file and copying its contents into the current #include position in the program (effectively replacing the line), e.g. #include <stdio.h> is replaced by the contents of the stdio.h file.</stdio.h>

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

What is the difference between #include <> and #include “” in C?

A

<> searches for the enclosed file using the path in which the pre-processor expects to find such files

”” searches for the enclosed file firstly in the same directory as the program, and then using the path in which the pre-processor expects to find such files. Search path can be specified with -l compiler flag

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

What does #define do in C?

A

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

e.g. #define TRUE 1

Then wherever TRUE is used, it is replaced with 1

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

In C, why do you need to take care when using #define to create macros?

A

Definitions are substituted before the compiler compiles the source code, so an error introduced is hard to trace

The direct substitution can lead to unexpected results

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

What do these C selective compilation flags mean?
#ifdef
#ifndef
#undef
#else
#elif
#endif

A

ifdef - if a symbol is defined

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

What do these useful C predefined macros do?
__FILE__
__LINE__
__DATE__
__TIME__
__STDC__

A

__FILE__ - the current file name

__LINE__ - the current line number in this file

__DATE__ - the date this object code was generated from the source

__TIME__ - the time this object code was generated from the source

__STDC__ - 1 if this implementation of C conforms to the ANSI/ISO standard; every other value means not

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

What are the two C macro naming standards?

A

Macro names are in all capitals

System macros start with __ (two underscores)

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

In C, how should a properly structured program be split up?

A

myProgram.h:
#includes
Structure definitions
Function prototypes

myProgram.c
#include <myProgram.h>
Main function
Other function definitions</myProgram.h>

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

What is the .h header file always named after?

A

The code file it belongs to (.c)

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

In C, what is the difference between static and extern storage classes in a header file myHeader.h?

A

Static is only available in myHeader.c

Extern is available to all programs #include -ing myHeader.h

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

In terms of C storage classes, what do these words mean?
auto
register
static
extern

A

auto (default):
- declared at the start of the block
- storage allocated when a block entered and automatically freed when block exited

register:
- variable is stored in CPU registers (rather than memory) for quick access

static:
- variable continues to exist even after the block in which it is defined terminated
- value is retained between repeated calls to the same function
- scope is limited to the block in which it is defined
- function is only visible within its “translation unit” (ie its .c file)

extern:
- scope is global
- function is visible globally

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

In C, what is a translation unit?

A

The .c file of a .h file

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

In C, why are #include guards needed and what must they be?

A

To avoid trying to include the same code multiple times in one executable, each header file needs an #include guard

It must be unique (standard is double underscore, followed by file name in capitals, with dots replaced by underscores, e.g. __STACK_H

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

In C, what is the difference between static libraries (.a) and dynamically linked shared object libraries (.so)?

A

Static libraries are linked with, and become part of, the application at link time

Dynamically linked shared object libraries are linked to at runtime

17
Q

In C, what to libraries do?

A

Group together multiple object files (*.o) into a single unit