Presentation is loading. Please wait.

Presentation is loading. Please wait.

Preprocessor Directives and Macros Chapter 21

Similar presentations


Presentation on theme: "Preprocessor Directives and Macros Chapter 21"— Presentation transcript:

1 Preprocessor Directives and Macros Chapter 21
CSCI 130 Preprocessor Directives and Macros Chapter 21

2 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

3 #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

4 Advantages of #include
Structure Portability Conventions: similar functions grouped in one file file given descriptive name

5 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;

6 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”

7 #define Used for substitution macros Used for function macros
substituting values for variables Used for function macros defining a function ‘on the fly’

8 #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

9 #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

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

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

12 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

13 #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

14 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

15 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

16 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

17 #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

18 #undef Opposite effect of #define #define PI 3.14 ... #undef PI


Download ppt "Preprocessor Directives and Macros Chapter 21"

Similar presentations


Ads by Google