Presentation is loading. Please wait.

Presentation is loading. Please wait.

240-491 Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor 240-491 Special Topics in Comp. Eng.

Similar presentations


Presentation on theme: "240-491 Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor 240-491 Special Topics in Comp. Eng."— Presentation transcript:

1 240-491 Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor 240-491 Special Topics in Comp. Eng. 1 Semester 2, 2000-2001 11. The Preprocessor

2 240-491 Adv. UNIX:pre/112 Overview 1. Preprocessor Features 2. Including Files 3. Symbolic Constants 4. Macros 5. Conditional Compilation

3 240-491 Adv. UNIX:pre/113 1. Preprocessor Features v The preprocessor is part of the compiler –it modifies the C program before the program is compiled v The modifications involve text substitution –text in the program is replaced by other text continued

4 240-491 Adv. UNIX:pre/114 v Substitution is actually based on replacing C tokens by other text. v Examples of tokens in a line of C code –the code: x = inc(foo); –the tokens: x = inc ( foo ) ;

5 240-491 Adv. UNIX:pre/115 1.1. Main Kinds of Substitutions v File Inclusion v Symbolic Constants v Macros v Conditional Compilation

6 240-491 Adv. UNIX:pre/116 1.2. Seeing the Substitution  It is possible to execute only the preprocessor part of gcc : $ gcc -E examp.c  This call to gcc will output the modified version of examp.c –can be used for checking that the right substitutions are occurring

7 240-491 Adv. UNIX:pre/117 2. Including Files (#include) v Some examples: #include /* from /usr/include */ #include “mydefs.h” /* from current dir */ #include /* from /usr/include/sys */ Each #include line in the program will be replaced by the contents of the named header file.

8 240-491 Adv. UNIX:pre/118 2.1. What’s in a header file? v Headers are C code (so have a look). v Typical code: –symbolic constant definitions ( #define lines) –typedef declarations –extern declarations (see later) –function prototypes v You will write your own header files when coding large, multiple file programs –see later

9 240-491 Adv. UNIX:pre/119 3. Symbolic Constants (#define) v Some examples: #define TRUE 1 #define FALSE 0 #define SIZE 10 #define MAX (SIZE*2 + 1)  Common mistake – adding a ‘ ; ’ # define SIZE 10; while (x < SIZE)... becomes while (x < 10;)... The preprocessor replaces SIZE by 10; a constant defined using another constant

10 240-491 Adv. UNIX:pre/1110 4. Macros v Some examples: #define sum(x, y) ((x) + (y)) #define cube(x) ((x) * (x) * (x)) #define print(str) { printf(“%s\n”, str); } v Use:Translation: c = sum(a, b);c = ((a) + (b)); y = cube(a);y = ((a) * (a) * (a)); print(argv[1]);{ printf(“%s\n”, argv[1]); } the macro body

11 240-491 Adv. UNIX:pre/1111 4.1. Rules you Must Follow v 1. Don’t place ‘;’s at the end of a macro body. v 2. Bracket every parameter in the macro body. v 3. Bracket the entire macro body. v 4. Avoid side-effects in macro calls. v 5. Add braces around multiple statements in a macro body; have a ‘;’ after every statement. When in doubt: add some brackets

12 240-491 Adv. UNIX:pre/1112 4.2. Macros are not Functions v A macro ‘call’ is textually replaced by the macro body inside the preprocessor. v There is no overhead of a function call at run-time. v Macro replacement causes the size of the program to increase. v Lots of tricky errors are possible.

13 240-491 Adv. UNIX:pre/1113 4.3. Multi-line Macros v Big macros use ‘\’ to run over multiple lines: #define swap_int(s1, s2) { int temp; \ temp = s1; \ s1 = s2; \ s2 = temp; }

14 240-491 Adv. UNIX:pre/1114 4.4. Some Common Errors 1. Including space after the macro name. 2. Missing parameter brackets. 3. Missing macro body brackets. 4. Including side effects in macros.

15 240-491 Adv. UNIX:pre/1115 4.4.1. Space after macro name v Example: #define abs (x) ((x)> 0) ? (x) : (-(x))) /* WRONG */ v Use: x = abs(2) ; v Expansion: x = (x) ((x)> 0) ? (x) : (-(x)))(2); replaced by space after abs

16 240-491 Adv. UNIX:pre/1116 4.4.2. Missing parameter brackets v Example: #define cube(x) x * x * x /* WRONG */ v Use: y = cube(z + 1); v Expansion: y = z + 1 * z + 1 * z + 1; y = z + 1 * z + 1 * z + 1; same as: y = z + (1 * z) + (1 * z) + 1; no brackets around the x's continued

17 240-491 Adv. UNIX:pre/1117 v Correct Version: #define cube(x) ((x) * (x) * (x)) v Use: y = cube(z + 1); v Expansion: y = ((z + 1) * (z + 1) * (z + 1));

18 240-491 Adv. UNIX:pre/1118 4.4.3. Missing macro body brackets v Example: #define inc(x)(x) + 1/* WRONG */ v Use: p = 3 * inc(q); v Expansion: p = 3 * (q) + 1; p = 3 * (q) + 1; same as: p = (3 *(q)) + 1; no brackets around the entire macro body continued

19 240-491 Adv. UNIX:pre/1119 v Correct version: #define inc(x)((x) + 1) v Use: p = 3 * inc(q); v Expansion: p = 3 * ((q) + 1);

20 240-491 Adv. UNIX:pre/1120 4.4.4. Side effects v Example: #define cube(x)((x) * (x) * (x)) v Use: int a = 2; c = cube(a++); v Expansion: int a = 2; c = ((a++) * (a++) * (a++)) a's value is side-effected three times

21 240-491 Adv. UNIX:pre/1121 4.5. More Examples v /* macros instead of functions */ #define max(x, y)((x) > (y) ? (x) : (y)) #define sign(x)((x) 0 ? 1 : 0)) v /* macros for control flow */ #define foreverfor(;;) #define repeatdo { #define until(x)} while (!(x)) v /* nicer names */ #define TRUE1 #define FALSE0 continued to be used as a pair

22 240-491 Adv. UNIX:pre/1122 v /* graphics macro */ #define line(x1,y1,x2,y2) { moveto(x1, y1) ; \ lineto(x2, y2); } v /* macro instead of function, with type info */ #define swap(x, y, TYPE){ TYPE temp; \ temp = x; \ x = y; \ y = temp; } v /* hide pointers without using functions */ #define LEFT(ptr)(ptr->left) #define RIGHT(ptr)(ptr->right) #define DATA(ptr)(ptr->data)

23 240-491 Adv. UNIX:pre/1123 4.6. Undefining v Cancel a definition: #undef name –applies from this line in the text downwards v Can redefine later: #define name 10

24 240-491 Adv. UNIX:pre/1124 5. Conditional Compilation v Specify which blocks of code to compile –some parts of the program can be ignored by the compiler v Used for debugging and portability.

25 240-491 Adv. UNIX:pre/1125 5.1. If Tests v General format: #if expression.../* code to be included if expression is true */ #endif v Expressions are C-like, but can only refer to symbolic constants and/or macros.

26 240-491 Adv. UNIX:pre/1126 Examples #define MAX 9 : #if MAX < 10..../* code to include */ #endif #define X 5 #define Y7 #define DEBUG 2 : #if X < Y && DEBUG == 1... /* code to include */ #endif

27 240-491 Adv. UNIX:pre/1127 Leaving Out Code v Example: # if 0.../* lines of code to be left out */ #endif v Useful for commenting out lines that contain lots of comments –cannot nest comments in C

28 240-491 Adv. UNIX:pre/1128 5.2. Multi-way Branches v Format: #if expr1... #elif expr2... #elif expr3... : #else... #endif

29 240-491 Adv. UNIX:pre/1129 5.3. Definition Test  #ifdef name Test if name defined #ifndef name Test if name not defined v Used for debugging: #define DEBUG : #ifdef DEBUG printf(“Some debug message\n”): #endif continued

30 240-491 Adv. UNIX:pre/1130 v Or: #define DEBUG 1 : #if DEBUG == 1 printf(“Some debug message\n”); #endif  In this version, we must supply a value for DEBUG. continued

31 240-491 Adv. UNIX:pre/1131 v Prevent multiple includes: #ifndef INCLUDED_TYPES #define INCLUDE_TYPES #include “types.h” #endif v Implement version differences: #ifdef UNIX char *version = “UNIX version”; #else char *version = “DOS version”; #endif only include types.h if INCLUDE_TYPES isn't already defined

32 240-491 Adv. UNIX:pre/1132 5.4. Creating Symbolic Constants  Can use #define or v Create (and initialise) on the command line: $ gcc -DDEBUG -o examp examp.c /* define DEBUG */ $ gcc -DMAX=2 -o foo foo.c /* define MAX with value 2 */

33 240-491 Adv. UNIX:pre/1133 5.5. Error Reporting (#error) v Example: #ifndef UNIX #error UNIX version not chosen. #endif v Or: #if !defined(UNIX) #error UNIX version not chosen #endif


Download ppt "240-491 Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor 240-491 Special Topics in Comp. Eng."

Similar presentations


Ads by Google