Week 5 - C Preprocessor and Program Structure Flashcards
What 4 things do C compiler directives do?
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
In C, each source file is pre-processed by the C preprocessor. What does this mean?
It rewrites the file with macro expansion
What is macro expansion in C?
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>
What is the difference between #include <> and #include “” in C?
<> 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
What does #define do in C?
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
In C, why do you need to take care when using #define to create macros?
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
What do these C selective compilation flags mean?
#ifdef
#ifndef
#undef
#else
#elif
#endif
ifdef - if a symbol is defined
What do these useful C predefined macros do?
__FILE__
__LINE__
__DATE__
__TIME__
__STDC__
__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
What are the two C macro naming standards?
Macro names are in all capitals
System macros start with __ (two underscores)
In C, how should a properly structured program be split up?
myProgram.h:
#includes
Structure definitions
Function prototypes
myProgram.c
#include <myProgram.h>
Main function
Other function definitions</myProgram.h>
What is the .h header file always named after?
The code file it belongs to (.c)
In C, what is the difference between static and extern storage classes in a header file myHeader.h?
Static is only available in myHeader.c
Extern is available to all programs #include -ing myHeader.h
In terms of C storage classes, what do these words mean?
auto
register
static
extern
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
In C, what is a translation unit?
The .c file of a .h file
In C, why are #include guards needed and what must they be?
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