1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
The C Preprocessor Yongjoon Lee April 22, What is it? Things with # –#include Processes the C source files BEFORE handing it to compiler. –`Pre`-process.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
CS 201 Functions Debzani Deb.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
CS 202 Computer Science II Lab Fall 2009 October 1.
C Slides and captured lecture (video and sound) are available at:
Programming C/C++ on Eclipe Trình bày: Ths HungNM C/C++ Training.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
1 CSC103: Introduction to Computer and Programming Lecture No 26.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
Outline Midterm results Static variables Memory model
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Tutorial ICS431 – Introduction to C.
C Hints and Tips The preprocessor and other fun toys.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Engineering Computing I Chapter 4 Functions and Program Structure.
1 Preprocessor –How it works File Inclusion: #include Macro Definition: #define Predefined macros –__LINE__, __FILE__, __DATE__, __TIME__ Conditional Compilation.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Gator Engineering 1 Chapter 2 C Fundamentals (Continued) Copyright © 2008 W. W. Norton & Company. All rights reserved.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CS 261 C Basics Page 2 1/14/2016 CS 261, WSU Vancouver Primitive Types Notes: 4 A is a constant; B is a variable.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
Chapter 14: The Preprocessor Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 14 The Preprocessor.
Lecture 18 CIS 208 Wednesday, March 30, Test2 April 15 th : Happy Tax day. Review: Wednesday, the 13 th.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 Lecture12: Preprocessor and Debugger 6/6/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 13 – C: The Rest of the Preprocessor.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Administrative things
INC 161 , CPE 100 Computer Programming
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
Pre-processor Directives
Introduction to Programming
Preprocessor C program → Modified C program → Object Code
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
Programming in C Miscellaneous Topics.
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
Chapter 11: The Preprocessor
CSE 341 Lecture 22 Macros; extending Scheme's syntax
Conditional Compilation
Administrative things
Week 2 - Friday CS222.
Presentation transcript:

1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C

Outline Midterm results Program development & midterm solutions Preprocessor 2

Midterm results 3 72 students Avg: 18.7 Std: 15.3 What happened? –Not enough time? –Problems too difficult?

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 3 rd problem = (5 + 10)/2 = 7.5

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

Midterm Problem 1 6

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

8

9

10

11

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

13

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

15

16

17

18

19

20

21

22

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

Midterm Problem 2 24

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

26

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

28

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

30

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

32

33

34

35

36

Preprocessors C program -> Preprocessor -> Modified C program -> Compile #include #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 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); }

Preprocessor Commands to the compiler Include files, shortcuts, conditional compilation Common Preprocessor Commands #include #define #ifdef / #ifndef # ##

#include: Header Files Includes files: Liberally copy-paste Typically header files Header files: –Declares External functions Variable types External global variables –Typically named *.h

#include: Header Files mylib.h int max(int a, int b); Mylib.c #include "mylib.h" int max(int a, int b) { return (a > b ? a : b); }

#define: Macros Blind substitution inside file #define MALLOC mymalloc #define MAXSIZE 100 p = MALLOC(MAXSIZE); printf("Allocated %d bytes", MAXSIZE); is exactly the same as p = mymalloc(100); printf("Allocated %d bytes", 100);

#define: Parameterized Macros #define PROD(a, b) PROD2(a, b * 10) PROD(5, 6) => PROD2(5, 6 * 10) PROD(5, 6 + 7) => PROD2(5, * 10) BUG!! 42

#define: More usage #undef #define OLDFUNC(a, b) NEWFUNC1(a); NEWFUNC2(b) OLDFUNC(5,6) becomes NEWFUNC1(5); NEWFUNC2(6) for (i = 0; i < 5; i++) OLDFUNC(5, 6); becomes for (i = 0; i < 5; i++) NEWFUNC1(5); NEWFUNC2(6); // can also undefine #undef OLDFUNC 43

# 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

## 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

## 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

#ifdef, #ifndef, #if, #elif, #else #ifdef identifer Lines to be included if identifier is defined as a macro #endif #ifndef identifer Lines to be included if identifier is not defined as a macro #endif #if expr1 Lines to be included if expr1 is nonzero #elif expr2 Lines to be included if expr1 is zero and expr2 is nonzero #else Lines to be included otherwise #endif

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 into printf(“sqrt(%f) = %f\n”, 3.0, sqrt(3.0)); 48