{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right Arrow Icon
2 Example #if-#else-#endif selection construct Check if constant identifier is defined ( #ifdef ) and include code1 if so, and other code2 if not #define VERBOSE 1 . . .
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern