40-491 Adv. UNIX:pre/11 Advanced UNIX Advanced UNIX Objectives of these slides: Objectives of these slides: – look at the features of the C look at the features of the C preprocessor preprocessor 240-491 Special Topics in Comp. Eng. 1 Semester 2, 2000-2001 11. The Preprocessor
33
Embed
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.
Adv. UNIX:pre/ 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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
240-491 Adv. UNIX:pre/11 1
Advanced UNIXAdvanced UNIX
Objectives of these slides:Objectives of these slides:– look at the features of the C preprocessorlook at the features of the C preprocessor
240-491 Special Topics in Comp. Eng. 1Semester 2, 2000-2001
11. The Preprocessor
240-491 Adv. UNIX:pre/11 2
OverviewOverview
1.1. Preprocessor Features Preprocessor Features2.2. Including Files Including Files3.3. Symbolic Constants Symbolic Constants4.4. Macros Macros5.5. Conditional Compilation Conditional Compilation
240-491 Adv. UNIX:pre/11 3
1. Preprocessor Features1. Preprocessor Features
The preprocessor is part of the compilerThe preprocessor is part of the compiler– it modifies the C program it modifies the C program beforebefore the program is the program is
compiledcompiled
The modifications involve text The modifications involve text substitutionsubstitution– text in the program is replaced by other texttext in the program is replaced by other text
continued
240-491 Adv. UNIX:pre/11 4
Substitution is actually based on replacing C Substitution is actually based on replacing C tokenstokens by other text. by other text.
Examples of tokens in a line of C codeExamples of tokens in a line of C code– the code:the code:
x = inc(foo);x = inc(foo);– the tokens:the tokens:x = inc ( foo ) ;x = inc ( foo ) ;
240-491 Adv. UNIX:pre/11 5
1.1. Main Kinds of Substitutions1.1. Main Kinds of Substitutions
1.2. Seeing the Substitution1.2. Seeing the Substitution It is possible to execute only the preprocessor part of It is possible to execute only the preprocessor part of gccgcc: :
$ gcc $ gcc -E-E examp.c examp.c
This call to This call to gccgcc will output the modified version of will output the modified version of examp.cexamp.c
– can be used for checking that the right substitutions are can be used for checking that the right substitutions are occurringoccurring
240-491 Adv. UNIX:pre/11 7
2. Including Files 2. Including Files (#include)(#include)
Some examples:Some examples:#include <stdio.h> /* from /usr/include */#include <stdio.h> /* from /usr/include */#include “mydefs.h” /* from current dir */#include “mydefs.h” /* from current dir */
#include <sys/file.h> /* from /usr/include/sys */#include <sys/file.h> /* from /usr/include/sys */
Each #include line in the program will bereplaced by the contents of the named header file.
240-491 Adv. UNIX:pre/11 8
2.1. What’s in a header file?2.1. What’s in a header file? Headers are C code (so Headers are C code (so have a lookhave a look).). Typical code:Typical code:
– symbolic constant definitions (symbolic constant definitions (#define#define lines) lines)– typedeftypedef declarations declarations– externextern declarations (see later)declarations (see later)– function prototypesfunction prototypes
You will write your own header files when coding large, You will write your own header files when coding large, multiple file programs multiple file programs – see latersee later
240-491 Adv. UNIX:pre/11 9
3. Symbolic Constants3. Symbolic Constants (#define) (#define) Some examples:Some examples:
4.1. Rules you 4.1. Rules you MustMust Follow Follow
1. Don’t place ‘;’s at the end of a macro body.1. Don’t place ‘;’s at the end of a macro body. 2. Bracket every parameter in the macro body.2. Bracket every parameter in the macro body. 3. Bracket the entire macro body.3. Bracket the entire macro body. 4. Avoid side-effects in macro calls.4. Avoid side-effects in macro calls. 5. Add braces around multiple statements in a 5. Add braces around multiple statements in a
macro body; have a ‘;’ after every statement.macro body; have a ‘;’ after every statement.
When in doubt: add some brackets
240-491 Adv. UNIX:pre/11 12
4.2. Macros are 4.2. Macros are notnot Functions Functions
A macro ‘call’ is textually A macro ‘call’ is textually replacedreplaced by the by the macro body inside the preprocessor.macro body inside the preprocessor.
There is There is no overheadno overhead of a function call at run- of a function call at run-time.time.
Macro replacement causes the Macro replacement causes the sizesize of the of the program to increase.program to increase.
LotsLots of tricky errors are possible. of tricky errors are possible.
240-491 Adv. UNIX:pre/11 13
4.3. Multi-line Macros4.3. Multi-line Macros
Big macros use ‘\’ to run over multiple lines:Big macros use ‘\’ to run over multiple lines:
/* macros for control flow *//* macros for control flow */#define forever#define forever for(;;)for(;;)#define repeat#define repeat do {do {#define until(x)#define until(x) } while (!(x))} while (!(x))
/* macro instead of function, with type info *//* macro instead of function, with type info */#define swap(x, y, TYPE)#define swap(x, y, TYPE){ TYPE temp; \{ TYPE temp; \
temp = x; \ temp = x; \ x = y; \ x = y; \ y = temp; } y = temp; }
/* hide pointers without using functions *//* hide pointers without using functions */#define LEFT(ptr)#define LEFT(ptr) (ptr->left)(ptr->left)#define RIGHT(ptr)#define RIGHT(ptr)(ptr->right)(ptr->right)#define DATA(ptr)#define DATA(ptr) (ptr->data)(ptr->data)
240-491 Adv. UNIX:pre/11 23
4.6. Undefining4.6. Undefining
Cancel a definition:Cancel a definition:#undef name#undef name
– applies from this line in the text downwardsapplies from this line in the text downwards
Can redefine later:Can redefine later:#define name 10#define name 10
Specify which blocks of code to Specify which blocks of code to compilecompile– some parts of the program can be some parts of the program can be ignoredignored
by the compilerby the compiler
Used for debugging and portability.Used for debugging and portability.
240-491 Adv. UNIX:pre/11 25
5.1. If Tests5.1. If Tests
General format:General format:#if#if expression expression
...... /* code to be included if /* code to be included if expression is true */ expression is true */
#endif#endif
Expressions are C-like, but can only refer to Expressions are C-like, but can only refer to symbolic constants and/or macros.symbolic constants and/or macros.
240-491 Adv. UNIX:pre/11 26
ExamplesExamples#define MAX 9#define MAX 9 : :#if#if MAX < 10 MAX < 10
........ /* code to include *//* code to include */#endif#endif
#define X #define X 55#define Y#define Y 77#define DEBUG 2#define DEBUG 2 : :#if#if X < Y && DEBUG == 1 X < Y && DEBUG == 1
... ... /* code to include */ /* code to include */ #endif#endif
240-491 Adv. UNIX:pre/11 27
Leaving Out CodeLeaving Out Code
Example:Example:##if if 00
...... /* lines of code to /* lines of code to be left out */ be left out */
#endif#endif
Useful for commenting out lines that Useful for commenting out lines that contain lots of commentscontain lots of comments– cannot nest comments in Ccannot nest comments in C
240-491 Adv. UNIX:pre/11 28
5.2. Multi-way Branches5.2. Multi-way Branches
Format:Format:#if expr1#if expr1
......#elif#elif expr2 expr2
......#elif expr3#elif expr3
...... : :#else#else
......#endif#endif
240-491 Adv. UNIX:pre/11 29
5.3. Definition Test5.3. Definition Test
#ifdef#ifdef name name Test if Test if namename defined defined#ifndef#ifndef name name Test if Test if namename notnot defined defined
Used for debugging:Used for debugging:#define DEBUG#define DEBUG : :#ifdef DEBUG#ifdef DEBUG printf(“Some debug message\n”): printf(“Some debug message\n”):#endif#endif