computer programming chapter ( 3 )

Post on 24-May-2015

497 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

C Programming CS 002 Computer Programming

TRANSCRIPT

LOGO

Prepaid By: Eng. Ibrahim Elewah

Higher Technological Institute 10th of Ramadan City6th of October Branch

Electrical and Computer Engineering Department

Main Reference

HTI Student Book and “C For Dummies”

by Dan Gookin 2nd Edition

Lecture Notes in

1

Course Contents

Introduction

C Programming Higher Technological Institute2

1

Program Development2

The Essential of C Programs3

Manipulating Data with Operators 4

Reading from and Writing to Standard I/O5

Decision6

Iteration7

Arrays8

C Functions9

Course Contents

C Programming Higher Technological Institute3

The Essential of C Programs3

Constants and variables

Expressions

Arithmetic operators

Statements

Statement blocks

Data Types and Names in C

Naming a Variable

Expressions

o An expression is a combination of constants,variables, and operators that are used todenote computations

1. Taking the value contained in the drawer(variable) A

2. Multiply this value by 2

3. Subtract 1 from result obtained from 2

4. The value contained in drawer A is omitted, thenputting the result obtained from 3 into drawer A.

C Programming Higher Technological Institute4

𝑨 = 𝟐 ∗ 𝑨 − 𝟏

Arithmetic Operations

C Programming Higher Technological Institute5

Addition

DivisionMultiplication

Subtraction

Reminder

The Reminder operator % is used to obtainthe reminder of the first operand divided bythe second operand

Arithmetic Operations

C Programming Higher Technological Institute6

Addition

DivisionMultiplication

Subtraction

Reminder

𝟔% 𝟒 = 𝟐 𝟏𝟎𝟎𝟏%𝟐 = 𝟏 𝟒𝟖%𝟓 =

Example

Arithmetic Operations

C Programming Higher Technological Institute7

Addition

DivisionMultiplication

Subtraction

Reminder

𝟔% 𝟒 = 𝟐 𝟏𝟎𝟎𝟏%𝟐 = 𝟏 𝟒𝟖%𝟓 = 𝟑

Example

Constants and Variables

o As its name implies, a constant is a value that neverchanges.

o A variable, on the other hand, can be used to presentdifferent values.

o For instance, consider the following:

𝒊 = 𝟏 ;o Where the symbol i is a constant because it always has

the same value (1) and the symbol i is assigned theconstant 1.

o In other words, i contains the value of 1 after thestatement is executed.

C Programming Higher Technological Institute8

Data Types and Names

oThe C language reserves some keywordswords that have special meanings to thelanguage.

oThose reserved words should not be usedas variables, constants, or function namesin your program.

oAll C keywords must be written inlowercase letters, for instance INT will notbe treated as a keyword, it must be writtenas int.C Programming Higher Technological Institute9

The computer list of C keywords

auto break case char

const continue default do

double else enum extern

float for goto if

int long register return

short signed sizeof static

struct switch typedef union

unsigned void volatile while

C Programming Higher Technological Institute10

Naming a Variable

o Characters A through Z and a through z

o Digit characters 0 through 9, which can be used in any position except the first of a variable name.

o The underscore character _

Examplesstop_sign loop3 and_pause

C Programming Higher Technological Institute11

Valid Variable Name Can Use

Naming a Variable

o A variable name can’t contain any C arithmetic signs.

o A variable name can’t contain any dots.

o A variable name can’t contain any apostrophes.

o A variable name can’t contain any other special symbols such as *, @, #, and so on.

Examples4flags sum-result method*4

return what_size? ahmed.ali

C Programming Higher Technological Institute12

Invalid Variable Name Can NOT be Used

Data Types

C Programming Higher Technological Institute13

C Data Type

char

a, B, $, #

int

5, 17, 128

float

2.5 , 0.3

double

23433.3455

Declaration Statement

C Programming Higher Technological Institute14

Typechar

int

float

double

Namec1

N1

F1

d1

Value‘&’

100

32/10

5e3

char c1 = ‘&’ ;

int n1 = 100 ;

Declaration Statement

C Programming Higher Technological Institute15

Typechar

int

float

double

Namec1

N1

F1

d1

Value‘&’

100

32/10

5e3

char c1 ;

int n1 ;

c1 = ‘&’ ;

n1 = 100 ;

Declaration Statement

C Programming Higher Technological Institute16

Typechar

int

float

double

Namec1

n1

f1

d1

Value‘&’

100

32/10

5e3

float f1= 32/100 ;

double d1=5e3 ;

Declaration Statement

C Programming Higher Technological Institute17

Typechar

int

float

double

Namec1

n1

f1

d1

Value‘&’

100

32/10

5e3

float f1 ;

double d1 ;

f1 = 32/100 ;

d1 = 5e3 ;

Some special characters in C

\b BackspaceMoves the cursor to the left one character

\f Form feedGoes to the top of a new page

\n New lineCarriage return and line feeds

\r ReturnReturns to the beginning of the current line

\t TabAdvances to the next tab stop

C Programming Higher Technological Institute18

Examples

/* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

return 0;

}

C Programming Higher Technological Institute19

Examples /* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

printf ( “ The character c1 is : %c \n ”, c1);

printf ( “ The character c1 is : %c ” , c1);

printf ( “ while the character c2 is : %c \n”, c2);

return 0;

}

C Programming Higher Technological Institute20

Examples /* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

printf ( “ The character c1 is : %c \n ”, c1);

printf ( “ The character c1 is : %c ” , c1);

printf ( “ while the character c2 is : %c \n”, c2);

return 0;

}

C Programming Higher Technological Institute21

printf ( “ The character c1 is : %c \n ”, c1);

Examples /* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

printf ( “ The character c1 is : %c \n ”, c1);

printf ( “ The character c1 is : %c ” , c1);

printf ( “ while the character c2 is : %c \n”, c2);

return 0;

}

C Programming Higher Technological Institute22

printf ( “ The character c1 is : %c \n ”, c1);

Examples /* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

printf ( “ The character c1 is : %c \n ”, c1);

printf ( “ The character c1 is : %c ” , c1);

printf ( “ while the character c2 is : %c \n”, c2);

return 0;

}

C Programming Higher Technological Institute23

printf ( “ The character c1 is : %c \n ”, c1);

Examples /* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

printf ( “ The character c1 is : %c \n ”, c1);

printf ( “ The character c1 is : %c ” , c1);

printf ( “ while the character c2 is : %c \n”, c2);

return 0;

}

C Programming Higher Technological Institute24

printf ( “ The character c1 is : %c \n ”, c1);

New Line

Specifier

for

Character Data Type

Examples /* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

printf ( “ The character c1 is : %c \n ”, c1);

printf ( “ The character c1 is : %c ” , c1);

printf ( “ while the character c2 is : %c \n”, c2);

return 0;

}

C Programming Higher Technological Institute25

You Have To Know about Data Types

C Programming Higher Technological Institute26

Type

char

int

float

double

Name

c1

n1

f1

d1

Value

‘&’

100

32/10

5e3

Specifier

%c

%d

%f

%e,%E

Examples /* The arithmetic operations on integers */

# include<stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 15 */

int m = 3;

int n=2;

printf ( "The summation of %d and %d is : %d.\n", m, n, m+n);

printf ( "The difference between %d and %d is : %d.\n", m, n, m-n);

printf ( "The multiplication of %d by %d is : %d.\n", m, n, m*n);

printf ( "The division of %d by %d is : %d.\n", m, n, m/n);

printf ( "The remainder of division of %d by %d is : %d.\n", m, n, m%n);

return 0 ;

}

C Programming Higher Technological Institute27

Operators Precedence

Operator Sign Name

( ) Brackets

/ Division

* Multiplication

+ Addition

- Subtraction

C Programming Higher Technological Institute28

Logic Operators

C Programming Higher Technological Institute29

Operator Sign Name

| | OR

&& AND

!= NOT

Examples /* Example3 : Integer vs. floating point divisions */

# include <stdio.h> /* the header file for the printf ()

function */

main ( )

{

int n1,n2,n3;

float m1,m2,m3;

n1 = 32/10; m1= 32/10;

n2 = 32.0/10; m2= 32.0/10;

n3 = 32/10.0; m3 = 32/10.0;

return 0;

}

C Programming Higher Technological Institute30

Examples /* Example3 : Integer vs. floating point divisions */

# include <stdio.h> /* the header file for the printf () function */

main ( )

{

int n1,n2,n3; float m1,m2,m3;

n1 = 32/10; m1= 32/10;

n2 = 32.0/10; m2= 32.0/10;

n3 = 32/10.0; m3 = 32/10.0;

printf ( “ The integer division of 32/10 is : %d \n”, n1);

printf ( “The floating point division of 32/10 is : %f \n”, m1);

printf ( “The integer division of 32.0/10 is : %d \n”, n2);

printf ( “The floating point division of 32.0/10 is : %f \n”, m2);

printf ( “The integer division of 32/10.0 is : %d \n”, n3);

printf ( “The floating point division of 32/10.0 is : %f \n”, m3);

return 0;

}C Programming Higher Technological Institute31

Double Data Type

o Here are two examples:

[mantissa] e [exponent]

[mantissa] E [exponent]

Example

5000 5e3.

-300 -3e2

0.0025 2.5e-3.

C Programming Higher Technological Institute32

Specifier %e or %E

With printf ( )

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute33

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute34

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute35

X = E * D – B / C + A ;

Precedence Example

X = E * D – B / C + A ;

C Programming Higher Technological Institute36

X = E * D – B / C + A ;

Precedence Example

C Programming Higher Technological Institute37

X = E * D – B / C + A ;

Precedence Example

C Programming Higher Technological Institute38

X = E * D – B / C + A ;

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Precedence Example

C Programming Higher Technological Institute39

X = E * D – B / C + A ;

2 * 10 – 6 / 3 + 20 A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Precedence Example

C Programming Higher Technological Institute40

X = E * D – B / C + A ;

2 * 10 – 6 / 3 + 20

20 – 2 + 20

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Precedence Example

C Programming Higher Technological Institute41

X = E * D – B / C + A ;

2 * 10 – 6 / 3 + 20

20 – 2 + 20

38

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute42

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute43

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute44

Y = ( E * D ) – ( B / C ) + A ;

Precedence Example

.

.

.

C Programming Higher Technological Institute45

Y = ( E * D ) – ( B / C ) + A ;

Precedence Example

.

.

.

38

C Programming Higher Technological Institute46

Y = ( E * D ) – ( B / C ) + A ;

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute47

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute48

Z = A * ( D – B ) / ( C + E ) ;

Precedence Example

C Programming Higher Technological Institute49

Z = A * ( D – B ) / ( C + E ) ;

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Precedence Example

C Programming Higher Technological Institute50

Z = A * ( D – B ) / ( C + E ) ;

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Precedence Example

C Programming Higher Technological Institute51

Z = A * ( D – B ) / ( C + E ) ;

20 * 4 / 5A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Precedence Example

C Programming Higher Technological Institute52

Z = A * ( D – B ) / ( C + E ) ;

20 * 4 / 5

16

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute53

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute54

Home Work ..!!Exercises 3 Page 44

C Programming Higher Technological Institute55

LOGO

Eng. Ibrahim Elewah

Higher Technological Institute 10th of Ramadan City6th of October Branch

Electrical and Computer Engineering Department

56

Main Reference

HTI Student Book and “C For Dummies”

by Dan Gookin 2nd Edition

top related