18
CSCI 130 Preprocessor Directives and Macros Chapter 21

CSCI 130

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: CSCI 130

CSCI 130

Preprocessor Directives and Macros

Chapter 21

Page 2: CSCI 130

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

Page 3: CSCI 130

#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

Page 4: CSCI 130

Advantages of #include

• Structure

• Portability

• Conventions:– similar functions grouped in one file– file given descriptive name

Page 5: CSCI 130

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

Page 6: CSCI 130

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”

Page 7: CSCI 130

#define

• Used for substitution macros– substituting values for variables

• Used for function macros– defining a function ‘on the fly’

Page 8: CSCI 130

#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

Page 9: CSCI 130

#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

Page 10: CSCI 130

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

Page 11: CSCI 130

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

Page 12: CSCI 130

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

Page 13: CSCI 130

#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

Page 14: CSCI 130

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

Page 15: CSCI 130

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

Page 16: CSCI 130

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

Page 17: CSCI 130

#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

Page 18: CSCI 130

#undef

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