csci 130
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 PresentationTRANSCRIPT
![Page 1: CSCI 130](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/1.jpg)
CSCI 130
Preprocessor Directives and Macros
Chapter 21
![Page 2: CSCI 130](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/3.jpg)
#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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/4.jpg)
Advantages of #include
• Structure
• Portability
• Conventions:– similar functions grouped in one file– file given descriptive name
![Page 5: CSCI 130](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/7.jpg)
#define
• Used for substitution macros– substituting values for variables
• Used for function macros– defining a function ‘on the fly’
![Page 8: CSCI 130](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/8.jpg)
#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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/9.jpg)
#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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/13.jpg)
#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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/17.jpg)
#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](https://reader035.vdocuments.us/reader035/viewer/2022072014/56812d1a550346895d920eeb/html5/thumbnails/18.jpg)
#undef
• Opposite effect of #define #define PI 3.14 ... #undef PI