C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture19_PreprocessorMacros

C_DOCUME~1_MAXWID~1 - Announcements Lecture 19 Topic for today Preprocessor Macros Next Class Read Chapter 20 Low Level Programming ee312 Spring

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 0 ee312 Spring 2008 Lecture 19 Announcements Lecture 19 • Topic for today –Preprocessor –Macros • Next Class –Read Chapter 20 –Low Level Programming 1 ee312 Spring 2008 Lecture 19 C compilation C source code C Preprocessor C Compiler C object code
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 2 ee312 Spring 2008 Lecture 19 What can you use .cpp for! http://www.cs.tut.fi/~jkorpela/html/cpre.html 3 ee312 Spring 2008 Lecture 19 Preprocessor Directives • Preprocessing involves making changes to the text of the source program (to save typing usually). • Preprocessing is done before actual compilation begins. • The preprocessor doesn’t know (very much) C. • Major kinds of preprocessor directives: – Macro definition – Conditional compilation – File inclusion • Rules for using preprocessor directives: – Must begin with a #. – May contain extra spaces and tabs. End at the first new-line character, unless continued using \. – Can appear anywhere in a program. – Comments may appear on the same line.
Background image of page 2
3 4 ee312 Spring 2008 Lecture 19 Preprocessor Directives Table 5 ee312 Spring 2008 Lecture 19 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. It is a literal insertion of the replacement-list • Uses of simple macros: – Defining “symbolic constants” – Making minor changes to the syntax of C – Renaming types – As conditions to be tested later by the preprocessor
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 6 ee312 Spring 2008 Lecture 19 Simple Macro Examples #define N 100 #define PI 3.14159 #define CR '\r' #define WARNING "Warning: nonstandard feature" #define BEGIN { #define END } #define BOOL int #define DEBUG • Warning: Don’t put any extraneous symbols in a macro definition; these will become part of the replacement list: #define N = 100 int a[N]; /* becomes int a[= 100]; */ #define N 100; int a[N]; /* becomes int a[100;]; */ 7 ee312 Spring 2008 Lecture 19 Preprocessor constants Define a symbolic constant like so #define PI 3.141526 Better version #define PI ( 3.141526 ) Use the symbolic constant circle_length = 2 * PI * radius;
Background image of page 4
5 8 ee312 Spring 2008 Lecture 19 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) #define getchar() getc(stdin) 9 ee312 Spring 2008 Lecture 19 Parameterized Macros (2) • Advantages of using a parameterized macro instead of a function: – The compiled code will execute more rapidly. – Macros are “generic.”
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/15/2008 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas at Austin.

Page1 / 14

C_DOCUME~1_MAXWID~1 - Announcements Lecture 19 Topic for today Preprocessor Macros Next Class Read Chapter 20 Low Level Programming ee312 Spring

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online