180 likes | 304 Views
CSCI 130. Preprocessor Directives and Macros Chapter 21. Preprocessor. Part of all C compiler packages First component that processes source code Source code changed based on directives all preprocessor directives begin with # Output - modified source code file
E N D
CSCI 130 Preprocessor Directives and Macros Chapter 21
Preprocessor • Part of all C compiler packages • First component that processes source code • Source code changed based on directives • all preprocessor directives begin with # • Output - modified source code file • used in next step of compilation • deleted automatically by system
#include • Imports other files into source code • maybe library functions (stdio.h) • use < > • may be user defined functions • use “ ” • may have more than one function in file
Advantages of #include • Structure • Portability • Conventions: • similar functions grouped in one file • file given descriptive name
Example - math library file • Contents of user created mathfn.h file: • int areaOfSquare(int length) { • return length * length; • } • int areaOfRectangle(int width, int height) { • return width * height; • }
main.c • main.c is sometimes called the driver • ‘drives’ the flow of logic • often does not contain any function definitions • contains function prototypes - includes functions with #include preprocessor directive • #include “mathfn.h”
#define • Used for substitution macros • substituting values for variables • Used for function macros • defining a function ‘on the fly’
#define - substitution macro • Creates substitution macro • #define PI 3.14 • area = radius * radius * PI • circumference = 2 * radius * PI • Changes in source code after precompiling • area = radius * radius * 3.14 • circumference = 2 * radius * 3.14 • Space after constant indicates substitution macro
#define - function macro • Shorthand for a more complicated operation • Arguments not type sensitive • Ex: • #define HALFOF(value) ((value)/2) • printf(“%f”, HALFOF(x + y)); • Changes in source code after precompiling • printf(“%f”, ((x+y)/2)); • No space after function name indicates function macro
Other function macro examples #define AVG3(a, b, c) (((a) + (b) + (c)) / 5) #define SMALLER(x, y) ((x) < (y) ? (x) : (y)) #define SUM (x, y, z) ((x) + (y) + (z))
Common Errors-function macros • Spaces after function macro name • #define SUM (x, y, z) ((x) + (y) + (z)) • Forgetting parenthesis • #define AREA(x, y) x*y • All parameters must be used • #define SUM(x, y, z) ((x) + (y))
Macros vs. Functions • Macros can be used for simple functions • Size of program • Functions exist as a single copy • Macro expanded in code every time it is called • Execution efficiency • no overhead to use a macro • overhead required for functions
#if and #endif • Preprocessor directives controlling conditional compilation • if statement determines if statements executed • #if statement determines if statements compiled • #elif, #else work as else if, else
Where would #if be used • For purposes of CSCI 130: • When including files • If file included more than once, code is imported for each time • out of memory • Use #if and ‘defined’ keywords to conditionally include statements for compilation
Example #if • #if defined mathfn_h • #else • #define mathfn_h //Note: no periods can be used here • int areaOfSquare(int length) { • return length * length; • } • int areaOfRectangle(int width, int height) { • return width * height; • } • #endif
Not (!) is allowed • #if !defined mathfn_h • #define mathfn_h • int areaOfSquare(int length) { • return length * length; • } • int areaOfRectangle(int width, int height) { • return width * height; • } • #endif
#ifndef directive • #ifndef mathfn_h • #define mathfn_h • int areaOfSquare(int length) { • return length * length; • } • int areaOfRectangle(int width, int height) { • return width * height; • } • #endif
#undef • Opposite effect of #define • #define PI 3.14 • ... • #undef PI