Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | bathsheba-leonard |
View: | 25 times |
Download: | 3 times |
© 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
1-2
1-2
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.1 C Language Elements in Miles-to-Kilometers Conversion Program
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>
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;
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.
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.
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.
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;
1-9
1-9
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.2 Memory(a) Before and (b) After Execution of a Program
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
1-11
1-11
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.3 Effect of kms = KMS_PER_MILE * miles;
1-12
1-12
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.4 Effect of sum = sum + item;
1-13
1-13
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.5 Effect of scanf("%lf", &miles);
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.
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
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.
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.
1-18
1-18
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.6 Scanning Data Line Bob
1-19
1-19
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.7 General Form of a C Program
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: , ; { }
1-21
1-21
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.8 Evaluation Tree for area = PI * radius * radius;
1-22
1-22
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.9 Step-by-Step Expression Evaluation
1-23
1-23
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.10 Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1);
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
1-25
1-25
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.12 Supermarket Coin Value Program
1-26
1-26
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.12 Supermarket Coin Value Program (cont’d)
1-27
1-27
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.13 Batch Version of Miles-to-Kilometers Conversion Program
1-28
1-28
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.14 Miles-to-Kilometers Conversion Program with Named Files
1-29
1-29
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.15 Compiler Listing of a Program with Syntax Errors
1-30
1-30
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.16 A Program with a Run-Time Error
1-31
1-31
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.17 Revised Start of main Function for Supermarket Coin Value Program
1-32
1-32
© 2010 Pearson Addison-Wesley. All rights reserved.
Figure 2.18 A Program That Produces Incorrect Results Due to & Omission