chapter 2: overview of c -...

40
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 2: Overview of C Problem Solving & Program Design in C

Upload: vuongnhu

Post on 17-Mar-2018

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

© 2010 Pearson Addison-Wesley. All rights reserved.

Addison Wesley

is an imprint of

Chapter 2:

Overview of C

Problem Solving & Program Design in C

Page 2: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-2

© 2010 Pearson Addison-Wesley. All rights reserved. 1-2

Why Learn C?

Compact, fast, and powerful

“High-level” Language

• Standard for program development (wide acceptance)

• It is everywhere! (portable)

• Supports modular programming style

• Useful for all applications

• C is the native language of UNIX

• Easy to interface with system devices/assembly routines

• C is terse

Page 3: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-3

© 2010 Pearson Addison-Wesley. All rights reserved. 1-3

C Language Elements in

Miles-to-Kilometers Conversion Program

Page 4: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-4

© 2010 Pearson Addison-Wesley. All rights reserved.

Preprocessor directives

• Begins with # symbol

• Provides an instruction to the C

preprocessor.

• Preprocessor modifies the text of C

program before compilation.

• In the example there are two

– # include

– # define

1-4

Page 5: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-5

© 2010 Pearson Addison-Wesley. All rights reserved.

library

• A collection of useful functions and symbols that may be

accessed by the program

• Each library has a standard header file ending with .h

symbol.

• The ANSI(American National Standards Institute)-C

standard requires that collection of standard libraries be

provided in every ANSI C implementation.

• Number of operation available may be expanded by

supplyin additional libraries

• An individual programmer may create his own libraries – <stdio.h>

1-5

Page 6: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-6

© 2010 Pearson Addison-Wesley. All rights reserved.

Header file examples

1-6

#include <stdio.h>

#include <string.h>

#include <math.h>

#include “mylib.h” (user defined)

#include <stdio.h> /* printf, scanf definitions */

Some names used in the program are found in the standard header file.

Page 7: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-7

© 2010 Pearson Addison-Wesley. All rights reserved.

#define

1-7

Other directive

#define KMS_PER_MILE 1.609

Replace each occurrence of KMS_PER_MILE by 1.609

Note that only data values that never change should be given names using a #define.

Using such constants makes easier to understand the statements

#define PI 3.141593

#define GRAV_ACC 9.81

Page 8: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-8

© 2010 Pearson Addison-Wesley. All rights reserved.

Comments

1-8

The text in the example starts with /* and end with */ is

called a comment.

Comments provide information making it easier for us to

understand the program.

Comment lines are non-executable statements, i.e. they are

ignored during compilation and execution of the program.

Page 9: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-9

© 2010 Pearson Addison-Wesley. All rights reserved.

Function main()

1-9

int → type of function

main(void) → function takes no value

{ → beginning of the function body

Function body

} → end of the function body

Function body has two main parts

Type declerations

Executable statements

Page 10: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-10

© 2010 Pearson Addison-Wesley. All rights reserved.

Function main()

1-10

#include <stdio.h>

int

main(void)

{

printf(“Hello world \n”);

return (0);

}

Page 11: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-11

© 2010 Pearson Addison-Wesley. All rights reserved.

Reserved keywords

1-11

Keywords

auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while

C’s reserved keywords.

Page 12: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-12

© 2010 Pearson Addison-Wesley. All rights reserved.

User defined identifiers

1-12

in C programs we choose our own identifiers to name memory cells that

will data and program results.

(KMS_PER_MILE)

rules for choosing identifiers

1. consist of only letters digits and underscore

2. cannot begin with a digit

3. a reserved word cannot be used as an identifier

4. do not use too long identifier names

Page 13: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-13

© 2010 Pearson Addison-Wesley. All rights reserved.

User defined identifiers

1-13

valid identifiers:

letter_1, inches, x1, VAR, COUNTER

invalid identifiers:

1letter,my count, two+four, ali’s

(be careful when using turkish letters)

Note also that

C is a case sensitive language

all the following names corresponds to a different

identifier name

hello, Hello, HELLO…

for a good programming style you may prefer to use

longer names with underscore;

x_coord, arithmetic_mean, sum_avg etc..

Page 14: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-14

© 2010 Pearson Addison-Wesley. All rights reserved.

Variable Declarations

1-14

syntax;

int variable_list;

double variable_list;

char variable_list;

examples:

int count,large;

double x,y,z;

char ans;

or

int count,

large;

double x;

double y;

double z;

char ans;

Page 15: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-15

© 2010 Pearson Addison-Wesley. All rights reserved.

Data types

1-15

int stands for integer numbers

double stands for real numbers

char stands for character symbols (letters, digits, symbols etc)

int: represents integer numbers between -32767 and 32767

double: represents real numbers contains integers as well,

larger values. they may be represented in scientific notation like

1.2300e5 or 1.2300E5.

3.1415, 1234.0, 15.0e-04, 12e+5 valid double constants

150, 0.123e, 15e-0.3, 34,99 invalid constants

char: a letter, a digit or a symbol enclosed in a apostrophe

‘A’, ‘1’, ’:’,’”’,’ ’

performing arithmetic operations is possible with character data type

(take care)

Page 16: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-16

© 2010 Pearson Addison-Wesley. All rights reserved.

Executable statements

1-16

they follow the declarations statements

this part has been translated into the machine language

back to the example

Assignment statements

kms=KMS_PER_MILE*miles;

the expression in LHS is firstly calculated and then result is assigned to

RHS variable, therefore it is NOT AN EQUALITY expression.

Page 17: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-17

© 2010 Pearson Addison-Wesley. All rights reserved. 1-17

C Language Elements in

Miles-to-Kilometers Conversion Program

Page 18: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-18

© 2010 Pearson Addison-Wesley. All rights reserved. 1-18

Memory(a) Before and (b) After Execution of

a Program

Page 19: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-19

© 2010 Pearson Addison-Wesley. All rights reserved. 1-19

Effect of kms = KMS_PER_MILE * miles;

Page 20: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-20

© 2010 Pearson Addison-Wesley. All rights reserved. 1-20

Effect of sum = sum + item;

Page 21: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-21

© 2010 Pearson Addison-Wesley. All rights reserved.

Input/Output Operations and

Functions

• data can be stored in memory in two ways

– by assignment

– by using a function like scanf

input /output operations are performed by

special functions supplied by directive

#include <stdio.h>

1-21

Page 22: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-22

© 2010 Pearson Addison-Wesley. All rights reserved.

printf function

syntax: printf(format string,print list)

printf(“ that equals %f kilometers. \n”,kms)

char string

result:

that equals 16.090000 kilometers

1-22

format placeholder print variable list

Page 23: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-23

© 2010 Pearson Addison-Wesley. All rights reserved.

printf function

1-23

format placeholders (specifiers)

Page 24: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-24

© 2010 Pearson Addison-Wesley. All rights reserved.

printf function

1-24

Example:

Assume letter_1, letter_2 , letter_3 are char variables and age is an int type data

printf(“Hi %c%c%c – your age is %d \n”, letter_1, letter_2 ,

letter_3, age)

Hi ABC – your age is 10 will be printed on screen where

letter_1=‘A’

letter_2=‘B’

letter_3=‘C’,

age = 10.

Page 25: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-25

© 2010 Pearson Addison-Wesley. All rights reserved.

scanf function

1-25

syntax: scanf(format string,input list)

Generally user is prompted by a printf function to tell user what to do before a

scanf function

example printf(“Enter the first number”);

scanf(“%lf”,&num1);

& ampersand symbol is the C address of operator. Do not forget to use & in

scanf.

after entering data user can press <enter> to process data

Page 26: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-26

© 2010 Pearson Addison-Wesley. All rights reserved. 1-26

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

Page 27: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-27

© 2010 Pearson Addison-Wesley. All rights reserved. 1-27

Scanning Data Line Bob

scanf(“%c%c%c”,letter_1,letter_2,letter_3);

Page 28: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-28

© 2010 Pearson Addison-Wesley. All rights reserved.

return (0);

• transfer control from program to the operating system

• the value in parenthesis is considered the result of the

main function’s execution.

• indicates the program is executed without error

1-28

Page 29: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-29

© 2010 Pearson Addison-Wesley. All rights reserved. 1-29

General Form of a C Program

Page 30: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-30

© 2010 Pearson Addison-Wesley. All rights reserved. 1-30

General Form of a C Program

• you can use blank spaces between the statements

• more than one statement on a line may be written but it is

better to use one statement per line for readability of the

program

• use comments at the beginning of the program and for a

good programming style

• it is a good practice to add

• name of programmer

• a brief description about aim of the program

Page 31: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-31

© 2010 Pearson Addison-Wesley. All rights reserved. 31

2.5 Arithmetic Expressions

Arithmetic operators:

Rules of operator precedence:

C operation

Arithmetic operator

Algebraic expression

C expression

Addition + f + 7 f + 7

Subtraction - p – c p - c

Multiplication * bm b * m

Division / x / y x / y

Modulus % r mod s r % s

Operator(s) Operation(s) Order of evaluation (precedence)

() Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nested), they are evaluated left to right.

*, /, or % Multiplication,Division, Modulus

Evaluated second. If there are several, they are evaluated left to right.

+ or - Addition Subtraction

Evaluated last. If there are several, they are evaluated left to right.

Page 32: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-32

© 2010 Pearson Addison-Wesley. All rights reserved. 1-32

Evaluation Tree for

area = PI * radius * radius;

Page 33: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-33

© 2010 Pearson Addison-Wesley. All rights reserved. 1-33

Step-by-Step Expression Evaluation

Page 34: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-34

© 2010 Pearson Addison-Wesley. All rights reserved. 34

Equality and Relational Operators Step 1. y = 2 * 5 * 5 + 3 * 5 + 7;

Step 2. y = 10 * 5 + 3 * 5 + 7;

Step 3. y = 50 + 3 * 5 + 7;

Step 4. y = 50 + 15 + 7;

Step 5. y = 65 + 7;

Step 6. y = 72;

2 * 5 is 10

10 * 5 is 50

3 * 5 is 15

50 + 15 is 65

65 + 7 is 72

(Leftm ost mult ip licat ion)

(Leftm ost mult ip licat ion)

(Mult ip licat ion before ad dition)

(Leftm ost ad dit ion)

(Last a dd it ion)

(Last op era t io n—p la ce 72 in y)

Page 35: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-35

© 2010 Pearson Addison-Wesley. All rights reserved. 1-35

Evaluation Tree and Evaluation for v = (p2 -

p1) / (t2 - t1);

Page 36: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-36

© 2010 Pearson Addison-Wesley. All rights reserved. 1-36

Evaluation Tree and Evaluation for

z - (a + b / 2) + w * -y

Page 37: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-37

© 2010 Pearson Addison-Wesley. All rights reserved.

Notes on / and % operators

when two positive integers are divided integral part of the

result is the result.

7.0/2.0=3.5 but 7/2 = 3, ½ = 0, 0 / 4=0, 4/0 = undefined

x % y means the remainder of x /y.

x=(x/y)*y+(x%y)

7=(7/2)*2+(7%2) !!!

3%5=3, 7%5=2 …

mixed type expressions:

var1 operator var2,

result is integer if both var1 and var2 are integers otherwise

result is double.

1-37

Page 38: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-38

© 2010 Pearson Addison-Wesley. All rights reserved.

Notes on / and % operators

example: given m and n are integers and x,y and p are

double

m=3;

n=2;

p=2.0;

x=m/p;

y=m/n;

m=3, n=2, p=2.0, x=1.5, y=1.0

1-38

Page 39: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-39

© 2010 Pearson Addison-Wesley. All rights reserved.

type conversion

• a type of an expression may be converted

to another type by placing the desired type

in parenthesis in front of the expression

example int x,y;

double z;

z=(double) x / (double) y;

1-39

Page 40: Chapter 2: Overview of C - mimoza.marmara.edu.trmimoza.marmara.edu.tr/~byilmaz/C_Lecture_01_2012.pdf · Chapter 2: Overview of C Problem Solving & Program Design in C . ... int →

1-40

© 2010 Pearson Addison-Wesley. All rights reserved.

writing mathematical formulas in C

1-40