CSC302 2007S, Class 11: Macros and More Admin: * Starting Friday, each person who arrives late will receive a penalty toward their final participation grade of 1 point per 5 minutes late or portion thereof. * Don't forget Friday's lunch! * Goal this week: Help you think better in C. * Q on HW4? * FOR FRIDAY: * How do I use comp in Quicksort? * How do I declare the comparator type? Overview: * Background: What is the C "perspective"? * Background: Refactoring. * A Domain: Quicksort. * Macros: Basic Concepts. * Enumerated Types. * Macros: Some Hacks. /Why use different languages?/ * Languages encourage us to think diffeently aboutt he programming task * Different languages better meet different tasks * Different langauges do have different efficiencies * Running code * Programmer time * Once you've chosen a language, you should adopt the "worldview" of that language /What is the C umwelt?/ * Expect to have direct control over memory * And to understand how computers use memory * Stacks and heaps, pointers are just integers, data segment vs. code segment, virtual memory (maybe) * Detour: Virtual memory * C programmers think about the underlying machine * C programmers care about efficiency * Low-level string processsing * A string is a 0-terminated array of characters * And arrays and pointers are indistinguishable * Typical quick version of strcpy void strcpy(char *src, char *dest) { while (*dest++ = *src++); } char html_request[128]; strcpy(argv[1], html_request); /General Good Programming Practice/ * Unit Testing * Refactoring * Identify and "factor out" common code * Particularly good idea as alternative to cut-and-paste programming * Sam: Good programmers can predict opportunities for refactoring, and write general code first * XP/AP practice: Refactor only when you see or are about to write duplicated code * Like functional programming, a technique for strong programmers /An Example: Quicksort/ * How do we convert from "sort ints" to "sort floats"? * How do we ease such conversions? * Typical strategy: Use "pointers to void" rather than int * Another strategy: Use typedef * Another strategy: Use #define * Whoops! I need to sort in increasing order, rather than decreasing