lecture23-mar22 - Announcements - Lecture 23 Read Ch. 15...

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

View Full Document Right Arrow Icon
1 Announcements - Lecture 23 • Read Ch. 15 and 17 • Assignment 5 – Out today, Q&A – designs due on Friday in recitation • Exam 2 – scheduled March 31 st – delay to Apr. 5 th • Topics for today – finish Ch. 14 – Finish macro definitions (#define) – simple and parameterized – Finish conditional preprocessing (#if . . . ) – File inclusion directives (#include) – Header file usage ( .h files) – Dynamic storage allocation and deallocation (time permitting) Coming attractions – Linked lists on Wed. – Friday’s lectures might be cancelled ???? How the Preprocessor Works • The preprocessor’s role in the compilation process: Deletes all comments and unnecessary white space Handles all # directives macros file includes conditional compilation misc. others Parameterized Macro Reminder #define AVG(x,y) (((x)+(y))/2) int main () { int a = 3, b = 5, c = 8; a = 1/AVG(b + 2, c); /* what is the equivalent statement that is generated by the preprocessor ? */ . . . a = 1/(((b + 2)+(c))/2); Each parameter and each expression should be enclosed in ( ) Predefined Macros C has several predefined macros, each of which represents an integer constant or string literal. The __DATE__ and __TIME__ macros identify when a program was compiled. e.g: printf("Wacky Windows (c) 2010 Wacky Software, Inc.\n"); printf("Compiled on %s at %s\n", __DATE__, __TIME__); Output produced by these statements: Wacky Windows (c) 2010 Wacky Software, Inc. Compiled on Dec 23 2010 at 22:18:48 This information can be helpful for distinguishing among different versions of the same program. Another predefined macro is named __STDC__ . – This macro exists and has the value 1 if the compiler conforms to the C standard (either C89 or C99). There is also a general-purpose error-detecting macro named assert ( more on this later). Predefined Macros We can use the __LINE__ and __FILE__ macros to help locate errors. E.g., a macro that can help pinpoint the location of a division by zero: #define CHECK_ZERO(divisor) \ if (divisor == 0) \ printf("*** Attempt to divide by zero on line %d " \ "of file %s ***\n", __LINE__, __FILE__) The CHECK_ZERO macro would be invoked just prior to a division operation: CHECK_ZERO(j); k = i / j; If j happens to be zero, a message of the following form will be printed: *** Attempt to divide by zero on line 9 of file foo.c *** Conditional Preprocessing The #if directive tests a constant expression to determine whether or not a particular section of text should be included in a program or not The #endif directive marks the end of the section: #if constant-expression /* include section if not 0 */ #endif The operator defined can be used in an #if directive: #if defined (identifier) /* include code if identifier is currently defined */ #endif The #ifdef directive is shorthand combining #if with defined: #ifdef identifier #endif
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 Example #if-#else-#endif
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

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

Page1 / 5

lecture23-mar22 - Announcements - Lecture 23 Read Ch. 15...

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

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