Presentation is loading. Please wait.

Presentation is loading. Please wait.

More about the C Preprocessor CS-2303, C-Term 20101 More about the C Preprocessor CS-2303 System Programming Concepts (Slides include materials from The.

Similar presentations


Presentation on theme: "More about the C Preprocessor CS-2303, C-Term 20101 More about the C Preprocessor CS-2303 System Programming Concepts (Slides include materials from The."— Presentation transcript:

1 More about the C Preprocessor CS-2303, C-Term 20101 More about the C Preprocessor CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

2 More about the C Preprocessor CS-2303, C-Term 20102 The C Preprocessor A separate compilation step before the compiler tries to “understand” your program Simple text substitution –“On the fly” See K&R, §4.11 & §A12 D&D, Chapter 13

3 More about the C Preprocessor CS-2303, C-Term 20103 Familiar Preprocessor Directives #include "filename" #include –Inserts the entire contents of filename in place of the #include line. –Remember, all.c and.h files must end with newline character! #define NAME replacement-text –Substitutes replacement-text wherever NAME appears in source file. Difference: which directories to search for filename

4 More about the C Preprocessor CS-2303, C-Term 20104 Macro Substitution with Arguments #define NAME(A, B) repl_text with A & B Defines a macro in which –text of argument A is inserted into repl_text wherever parameter A occurs –text of argument B is inserted into repl_text wherever parameter B occurs Example:– #define max(A, B) ((A) > (B) ? (A) : (B)) –Replace max(x+1, y-2) with ((x+1) > (y-2) ? (x+1) : (y-2))

5 More about the C Preprocessor CS-2303, C-Term 20105 Hazards with Macros Side effects:– –max(x++, y--) expands to ((x++) > (y--) ? (x++) : (y--)) –I.e., x is incremented twice if greater, y is decremented twice if lesser Unintended consequences:– #define SQUARE(x) x * x –Then SQUARE(n + 1) expands to n + 1 * n + 1

6 More about the C Preprocessor CS-2303, C-Term 20106 Hazards with Macros (continued) Always surround parameters with () in replacement text To be sure that evaluation order of expression is what you really mean Never use expression with side-effects as argument to a pre-processor macro I.e., x++, a += b, etc.

7 More about the C Preprocessor CS-2303, C-Term 20107 Conditional Inclusion #if !defined(HDR) #define HDR /*contents of hdr.h are here */ #ENDIF Result:– –The file is only included once –Even if it is mentioned multiple times

8 More about the C Preprocessor CS-2303, C-Term 20108 Conditional Inclusion Example Suppose that your program says #include... It turns out that also includes Must avoid multiple copies of in your program Zillions of compile errors!

9 More about the C Preprocessor CS-2303, C-Term 20109 Conditional Inclusion Example (continued) Therefore, looks like #ifndef _STDIO_H #define _STDIO_H /* body of stdio.h */ #endif All system include files have the same form On any non-trivial project, your include files should take the same form. #ifndef NAME is same as #if !defined(NAME)

10 More about the C Preprocessor CS-2303, C-Term 201010 Another Conditional Include Example #if SYSTEM == SYSV #define HDR "sysv.h" #elif SYSTEM == BSD #define HDR "bsd.h" #elif SYSTEM == MSDOS #define HDR "msdos.h" #else #define HDR "default.h" #endif #include HDR K & R page 91 Selectively include the correct header file for the particular platform Value of system can be defined on gcc command line gcc –D SYSTEM=BSD … Widely used inside of OS and device drivers

11 More about the C Preprocessor CS-2303, C-Term 201011 Yet Another Conditional Include Example #if PROCESSOR == x86 /*C-code for Intel x86 */ #elif PROCESSOR == X86_64 /*C-code for Intel/AMD x86_64 */ #elif PROCESSOR == PPC /*C-code for IBM PowerPC */ #else #error PROCESSOR not supported #endif

12 More about the C Preprocessor CS-2303, C-Term 201012 Setting Variables from Compile Command To set a variable, include the following in your makefile :– gcc –D PROCESSOR=X86...

13 More about the C Preprocessor CS-2303, C-Term 201013 C Preprocessor Directives Widely used in large C projects Intellectually “messy” An essential part of supporting projects and applications on multiple platforms

14 More about the C Preprocessor CS-2303, C-Term 201014 Questions?


Download ppt "More about the C Preprocessor CS-2303, C-Term 20101 More about the C Preprocessor CS-2303 System Programming Concepts (Slides include materials from The."

Similar presentations


Ads by Google