![Page 1: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/1.jpg)
1
Lecture08: Program Development & Preprocessor11/12/2012
Slides modified from Yin Lou, Cornell CS2022: Introduction to C
![Page 2: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/2.jpg)
Outline
• Midterm results• Program development & midterm solutions
• Preprocessor
2
![Page 3: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/3.jpg)
Midterm results
3
• 72 students• Avg: 18.7• Std: 15.3
• What happened?– Not enough time?– Problems too difficult?
![Page 4: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/4.jpg)
Take-home assignment #7
4
• For those who came to take the midterm exam (i.e.,
submitted something on the judge system on the exam day), you have a chance to earn 1/2 credit back on the lost points for problems 3-5 (not problems 1-2 because I will talk
about the solutions today).• Say midterm score is 5 on the 3rd problem.
– Get 10 points on the 3rd problem in assignment #7– Your midterm score for the 3rd problem = (5 + 10)/2 = 7.5
![Page 5: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/5.jpg)
Program development
• A Common problem– Know all C commands but don’t know how to use them to write a
program.• Top-down approach – always work!
– Start by writing down the main steps (in words/comments, not code) of a program.
• Consider each step to be a function– For each difficult main step, break it down into smaller steps (again in
words/comments, not code).
– Apply this “divide-and-conquer” approach until the steps are simple enough to code.
– Write code
5
![Page 6: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/6.jpg)
Midterm Problem 1
6
![Page 7: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/7.jpg)
Program development
• A Common problem– Know all C commands but don’t know how to use them to write a
program.• Top-down approach – always work!
– Start by writing down the main steps (in words/comments, not code) of a program.
• Consider each step to be a function– For each difficult main step, break it down into smaller steps (again in
words/comments, not code).
– Apply this “divide-and-conquer” approach until the steps are simple enough to code.
– Write code
7
![Page 8: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/8.jpg)
8
![Page 9: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/9.jpg)
9
![Page 10: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/10.jpg)
10
![Page 11: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/11.jpg)
11
![Page 12: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/12.jpg)
Program development
• A Common problem– Know all C commands but don’t know how to use them to write a
program.• Top-down approach – always work!
– Start by writing down the main steps (in words/comments, not code) of a program.
• Consider each step to be a function– For each difficult main step, break it down into smaller steps (again in
words/comments, not code).
– Apply this “divide-and-conquer” approach until the steps are simple enough to code.
– Write code
12
![Page 13: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/13.jpg)
13
![Page 14: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/14.jpg)
Program development
• A Common problem– Know all C commands but don’t know how to use them to write a
program.• Top-down approach – always work!
– Start by writing down the main steps (in words/comments, not code) of a program.
• Consider each step to be a function– For each difficult main step, break it down into smaller steps (again in
words/comments, not code).
– Apply this “divide-and-conquer” approach until the steps are simple enough to code.
– Write code!
14
![Page 15: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/15.jpg)
15
![Page 16: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/16.jpg)
16
![Page 17: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/17.jpg)
17
![Page 18: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/18.jpg)
18
![Page 19: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/19.jpg)
19
![Page 20: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/20.jpg)
20
![Page 21: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/21.jpg)
21
![Page 22: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/22.jpg)
22
![Page 23: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/23.jpg)
Systematic way of writing programs
• Like writing an essay.• Start with an outline that describes what you are going to
write.• Okay to write the comments in Chinese.
23
![Page 24: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/24.jpg)
Midterm Problem 2
24
![Page 25: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/25.jpg)
Program development
• A Common problem– Know all C commands but don’t know how to use them to write a
program.• Top-down approach – always work!
– Start by writing down the main steps (in words/comments, not code) of a program.
• Consider each step to be a function– For each difficult main step, break it down into smaller steps (again in
words/comments, not code).
– Apply this “divide-and-conquer” approach until the steps are simple enough to code.
– Write code
25
![Page 26: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/26.jpg)
26
![Page 27: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/27.jpg)
Program development
• A Common problem– Know all C commands but don’t know how to use them to write a
program.• Top-down approach – always work!
– Start by writing down the main steps (in words/comments, not code) of a program.
• Consider each step to be a function– For each difficult main step, break it down into smaller steps (again in
words/comments, not code).
– Apply this “divide-and-conquer” approach until the steps are simple enough to code.
– Write code
27
![Page 28: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/28.jpg)
28
![Page 29: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/29.jpg)
Program development
• A Common problem– Know all C commands but don’t know how to use them to write a
program.• Top-down approach – always work!
– Start by writing down the main steps (in words/comments, not code) of a program.
• Consider each step to be a function– For each difficult main step, break it down into smaller steps (again in
words/comments, not code).
– Apply this “divide-and-conquer” approach until the steps are simple enough to code.
– Write code
29
![Page 30: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/30.jpg)
30
![Page 31: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/31.jpg)
Program development
• A Common problem– Know all C commands but don’t know how to use them to write a
program.• Top-down approach – always work!
– Start by writing down the main steps (in words/comments, not code) of a program.
• Consider each step to be a function– For each difficult main step, break it down into smaller steps (again in
words/comments, not code).
– Apply this “divide-and-conquer” approach until the steps are simple enough to code.
– Write code!
31
![Page 32: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/32.jpg)
32
![Page 33: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/33.jpg)
33
![Page 34: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/34.jpg)
34
![Page 35: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/35.jpg)
35
![Page 36: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/36.jpg)
36
![Page 37: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/37.jpg)
PreprocessorsC program -> Preprocessor -> Modified C program -> Compile#include <stdio.h>#define FREEZING_PT 32.0#define SCALE_FACTOR (5.0 / 9.0)int main(){
float fahrenheit, celsius;printf(“Enter Fahrenheit temperature: “);scanf(“%f”, &fahrenheit);celsius = (fahrenheit – FREEZING_PT) * SCALE_FACTOR;printf(“Celsius equivalent is: %.1f\n”, celsius);
}
37
#include <stdio.h>int main(){
float fahrenheit, celsius;printf(“Enter Fahrenheit temperature: “);scanf(“%f”, &fahrenheit);celsius = (fahrenheit – 32.0) * (5.0 / 9.0);printf(“Celsius equivalent is: %.1f\n”, celsius);
}
![Page 38: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/38.jpg)
Preprocessor
• Commands to the compiler• Include files, shortcuts, conditional compilation• Common Preprocessor Commands
#include#define#ifdef / #ifndef# ##
![Page 39: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/39.jpg)
#include: Header Files
• Includes files: Liberally copy-paste• Typically header files• Header files:
– Declares• External functions• Variable types• External global variables
– Typically named *.h
![Page 40: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/40.jpg)
#include: Header Files
• mylib.hint max(int a, int b);
• Mylib.c
#include "mylib.h"int max(int a, int b){
return (a > b ? a : b);}
![Page 41: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/41.jpg)
#define: Macros
• Blind substitution inside file
#define MALLOC mymalloc#define MAXSIZE 100p = MALLOC(MAXSIZE);printf("Allocated %d bytes", MAXSIZE);
• is exactly the same as
p = mymalloc(100);printf("Allocated %d bytes", 100);
![Page 42: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/42.jpg)
#define: Parameterized Macros
#define PROD(a, b) PROD2(a, b * 10)
PROD(5, 6) => PROD2(5, 6 * 10)PROD(5, 6 + 7) => PROD2(5, 6 + 7 * 10) BUG!!
42
![Page 43: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/43.jpg)
#define: More usage#undef
#define OLDFUNC(a, b) NEWFUNC1(a); NEWFUNC2(b)OLDFUNC(5,6) becomesNEWFUNC1(5); NEWFUNC2(6)
for (i = 0; i < 5; i++) OLDFUNC(5, 6); becomesfor (i = 0; i < 5; i++) NEWFUNC1(5); NEWFUNC2(6);
// can also undefine#undef OLDFUNC
43
![Page 44: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/44.jpg)
# operator
#define PRINT_INT(n) printf(#n “ = %d\n”, n);
Preprocessor creates a string literal from PRINT_INT’s argument.
PRINT_INT(i/j)->printf(“i/j” “ = %d\n”, n);-> printf(“i/j = %d\n”, n);
44
![Page 45: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/45.jpg)
## operator
#define MK_ID(n) i##n
Preprocessor combines i and n together into one token.
int MK_ID(1);int MK_ID(2);->int i1;int i2;
45
![Page 46: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/46.jpg)
## operator
#define MK_ID(n) i##n
Preprocessor combines i and n together into one token.
int MK_ID(1);int MK_ID(2);->int i1;int i2;
46
![Page 47: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/47.jpg)
#ifdef, #ifndef, #if, #elif, #else
#ifdef identiferLines to be included if identifier is defined as a macro#endif
#ifndef identiferLines to be included if identifier is not defined as a macro#endif
#if expr1Lines to be included if expr1 is nonzero#elif expr2Lines to be included if expr1 is zero and expr2 is nonzero#elseLines to be included otherwise#endif
![Page 48: 1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C](https://reader035.vdocuments.net/reader035/viewer/2022062317/5a4d1afe7f8b9ab059985ac3/html5/thumbnails/48.jpg)
In-class Exercise 8.1
Write a macro DISP(f,x) that expands into a call of printf that displays the value of the function f when called with argument x. For example,
DISP(sqrt, 3.0)
should expand intoprintf(“sqrt(%f) = %f\n”, 3.0, sqrt(3.0));
48