chapter 2: overview of c

32
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 2: Overview of C Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman

Upload: bathsheba-leonard

Post on 01-Jan-2016

26 views

Category:

Documents


3 download

DESCRIPTION

Chapter 2: Overview of C. Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman. Figure 2.1 C Language Elements in Miles-to-Kilometers Conversion Program. Preprocessor and Library. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 2: Overview of C

© 2010 Pearson Addison-Wesley. All rights reserved.

Addison Wesley is an imprint of

Chapter 2:Overview of C

Problem Solving & Program Design in C

Sixth Edition

By Jeri R. Hanly &

Elliot B. Koffman

Page 2: Chapter 2: Overview of C

1-2

1-2

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.1 C Language Elements in Miles-to-Kilometers Conversion Program

Page 3: Chapter 2: Overview of C

1-3

1-3

© 2010 Pearson Addison-Wesley. All rights reserved.

Preprocessor and Library

• Preprocessor: A system program that modifies a C program before the compilation.

• Preprocessor Directive: A C program line that provides an instruction to the preprocessor

• Library: A collection of useful functions and symbols that may be accessed by a program.

#include <stdio.h>

Page 4: Chapter 2: Overview of C

1-4

1-4

© 2010 Pearson Addison-Wesley. All rights reserved.

Constant Macro

A name that is replaced by a particular constant value before the program is sent to the compiler.

#define KMS_PER_MILE 1.609

Each time the compiler sees KMS_PER_MILE, it will replace it by 1.609.

For example: kms = KMS_PER_MILE * miles;would read: kms = 1.609 * miles;

Page 5: Chapter 2: Overview of C

1-5

1-5

© 2010 Pearson Addison-Wesley. All rights reserved.

Commenting

• Those are for the users to remember or understand easier. They are ignored by the preprocessor and the compiler. They provide supplementary information.

/* This is a comment */

// This is a comment, as well.

Page 6: Chapter 2: Overview of C

1-6

1-6

© 2010 Pearson Addison-Wesley. All rights reserved.

Identifiers

• Reserved WordsWords with special meaning in C; e.g. double.

They cannot be used for any other purpose.• Identifiers

– StandardStill special words but they can be changed by the programmer; e.g.

printf– User-DefinedWords to name the memory cell; e.g. KMS_PER_MILERemark: Names are case sensitive. i.e. area and Area would be

two different identifiers.

Page 7: Chapter 2: Overview of C

1-7

1-7

© 2010 Pearson Addison-Wesley. All rights reserved.

Restrictions

• Identifiers must consist of only letters, digits and underscores.

• Cannot begin with a digit.• Reserved words cannot be used.• Some compilers take the first 31 characters; so

the rest is not important.e.g. my book, 16door, double, ali’sbook….are invalid.Recommendation: Use capital letters in constant

macros and give meaningful names.

Page 8: Chapter 2: Overview of C

1-8

1-8

© 2010 Pearson Addison-Wesley. All rights reserved.

Variables and Data Types

• Variable: A name associated with a memory cell whose value can change.

• Data Type: A set of values and operations that can be performed on those values.

int for an integer number (-32767 to 32767)double for a real numberchar for an individual character valueExample of a variable declaration:double kms;

Page 9: Chapter 2: Overview of C

1-9

1-9

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.2 Memory(a) Before and (b) After Execution of a Program

Page 10: Chapter 2: Overview of C

1-10

1-10

© 2010 Pearson Addison-Wesley. All rights reserved.

Assignment Statements

= is read as “gets”, “becomes”, “assigns”, “takes the value of”.

variable = expression;

x = y + z;

x = x + 1;Previous value is overwritten by the new value

Page 11: Chapter 2: Overview of C

1-11

1-11

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.3 Effect of kms = KMS_PER_MILE * miles;

Page 12: Chapter 2: Overview of C

1-12

1-12

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.4 Effect of sum = sum + item;

Page 13: Chapter 2: Overview of C

1-13

1-13

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.5 Effect of scanf("%lf", &miles);

Page 14: Chapter 2: Overview of C

1-14

1-14

© 2010 Pearson Addison-Wesley. All rights reserved.

Input/Output Operations and Functions

• Data can be stored in the memory in two ways:– By assignment– By copying data from an input device into a variable.

For example: scanf function

• An instruction that displays information stored in the memory is an output operation; e.g. to output data to the screen printf function is used.

Page 15: Chapter 2: Overview of C

1-15

1-15

© 2010 Pearson Addison-Wesley. All rights reserved.

Placeholder

• A symbol beginning with % in a format string that indicates where to display the output value.

Placeholder Variable Type Function Use

%c char printf/scanf

%d int printf/scanf

%f double printf

%lf double scanf

Page 16: Chapter 2: Overview of C

1-16

1-16

© 2010 Pearson Addison-Wesley. All rights reserved.

printf()

printf(“Your point is %f out of 100 \n”, grd);Printf: Function namegrd: Print list i.e. variables or expressions

whose values are displayed.Whole in () is the function argument.\n: Terminate the line.%f: placeholder that indicated where to

display the output value.

Page 17: Chapter 2: Overview of C

1-17

1-17

© 2010 Pearson Addison-Wesley. All rights reserved.

scanf()

scanf(format string, input list);scanf(“%lf”, &miles) means we will have 30.5

given to “miles” if number entered is 30.5.e.g. scanf(“%c%c%c”, &letter_1, &letter_2,

&letter_3);Will take first letter copied to letter_1, second

to letter_2 and third one to letter_3 in the entered order.

Page 18: Chapter 2: Overview of C

1-18

1-18

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.6 Scanning Data Line Bob

Page 19: Chapter 2: Overview of C

1-19

1-19

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.7 General Form of a C Program

Page 20: Chapter 2: Overview of C

1-20

1-20

© 2010 Pearson Addison-Wesley. All rights reserved.

Function Body

• Two parts:– Declarations

Tell the compiler what memory cells are needed in the function

– Executable Statements

First translated into machine language then executed

Punctuation: , ; { }

Page 21: Chapter 2: Overview of C

1-21

1-21

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.8 Evaluation Tree for area = PI * radius * radius;

Page 22: Chapter 2: Overview of C

1-22

1-22

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.9 Step-by-Step Expression Evaluation

Page 23: Chapter 2: Overview of C

1-23

1-23

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.10 Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1);

Page 24: Chapter 2: Overview of C

1-24

1-24

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.11 Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y

Page 25: Chapter 2: Overview of C

1-25

1-25

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.12 Supermarket Coin Value Program

Page 26: Chapter 2: Overview of C

1-26

1-26

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.12 Supermarket Coin Value Program (cont’d)

Page 27: Chapter 2: Overview of C

1-27

1-27

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.13 Batch Version of Miles-to-Kilometers Conversion Program

Page 28: Chapter 2: Overview of C

1-28

1-28

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.14 Miles-to-Kilometers Conversion Program with Named Files

Page 29: Chapter 2: Overview of C

1-29

1-29

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.15 Compiler Listing of a Program with Syntax Errors

Page 30: Chapter 2: Overview of C

1-30

1-30

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.16 A Program with a Run-Time Error

Page 31: Chapter 2: Overview of C

1-31

1-31

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.17 Revised Start of main Function for Supermarket Coin Value Program

Page 32: Chapter 2: Overview of C

1-32

1-32

© 2010 Pearson Addison-Wesley. All rights reserved.

Figure 2.18 A Program That Produces Incorrect Results Due to & Omission