Presentation is loading. Please wait.

Presentation is loading. Please wait.

THE PREPROCESSOR. Preprocessing is (apparently) done before actual compilation begins. The preprocessor doesnt know (very much) C. Major kinds of preprocessor.

Similar presentations


Presentation on theme: "THE PREPROCESSOR. Preprocessing is (apparently) done before actual compilation begins. The preprocessor doesnt know (very much) C. Major kinds of preprocessor."— Presentation transcript:

1 THE PREPROCESSOR

2 Preprocessing is (apparently) done before actual compilation begins. The preprocessor doesnt know (very much) C. Major kinds of preprocessor directives: Macro definition Conditional compilation File inclusion

3 THE PREPROCESSOR Rules for using preprocessor directives: Must begin with a #. May contain extra spaces and tabs.

4 Simple Macros Form of a simple macro: #define identifier replacement-list The replacement list can be any sequence of C tokens, including identifiers, keywords, numbers, character constants, string literals, operators, and punctuation.

5 Simple Macros Examples of simple macros: #define N 100 #define PI #define CR '\r' #define WARNING "Warning: nonstandard feature" #define BEGIN { #define END } #define BOOL int #define N 100; int a[N]; /* becomes int a[100;]; */

6 Parameterized Macros Form of a parameterized macro: #define identifier(x1, x2, …, xn) replacement-list There must be no space between the identifier and the left parenthesis. Parameterized macros often serve as simple functions. Examples of parameterized macros: #define MAX(x,y) ((x)>(y)?(x):(y)) #define EVEN(n) ((n) % 2 == 0) #define ODD(n) ((n) % 2 != 0)

7 Parameterized Macros Advantages of using a parameterized macro instead of a function: The compiled code will execute more rapidly. Macros are generic. Disadvantages of using a parameterized macro instead of a function: The compiled code will often be larger. Arguments arent type-checked. Its not possible to have a pointer to a macro. A macro may evaluate its arguments more than once

8 General Properties of Macros One macro may be defined in terms of another: #define PI #define TWO_PI (2*PI) When the preprocessor encounters the symbol TWO_PI later in the program, it replaces it by (2*PI). The preprocessor then rescans the replacement list to see if it contains invocations of other macros (PI in this case). The preprocessor will rescan the replacement list as many times as necessary to eliminate all macro names.


Download ppt "THE PREPROCESSOR. Preprocessing is (apparently) done before actual compilation begins. The preprocessor doesnt know (very much) C. Major kinds of preprocessor."

Similar presentations


Ads by Google