Overview (1 A)
Cop y rig h t (c) 2 0 0 9-2 0 1 7 Y oung W. Lim. Perm ission is g ra nted to cop y, d istribute a nd /or m od ify th is d ocum ent und er th e term s of th e G N UFree D ocum enta tion License, Version 1.2 or a ny la ter version p ublish ed by th e Free Softw a re Found a tion;w ith no Inva ria nt Sections, no Front-Cover Texts, a nd no Ba ck-cover Texts. A cop y of th e license is includ ed in th e section entitled "G N U Free D ocum enta tion License". Plea se send corrections (or sug g estions)to y oung w lim @ h otm a il.com. Th is d ocum ent w a s p rod uced by using Op enoffi ce.
Ca lcula ting th e Mea n m = a b c 3 40 + 50 + 60 3 = 150 3 = 50 Integ er num ber fi xed p oint num ber 45 53 63 3 = 161 3 = 53.6666666... Rea lnum ber fl oa ting p oint num ber Overview 3
Ca lcula ting a m ea n in C int a, b, c; int m ea n; a = 4 0 ; b = 5 0 ; c = 6 0 ; m ea n = (a + b + c)/ 3 ; int a, b, c; fl oa t m ea n; a = 4 5 ; b = 5 3 ; c = 6 3 ; m ea n = (a + b + c)/ 3.0 ; * Va ria ble * Ty p e * Assig nm ent * Op era tor Overview 4
Va ria bles int Ty p e int a, b, c; int m ea n; a = 4 0 ; b = 5 0 ; c = 6 0 ; m ea n = (a + b + c)/ 3 ; a b c m ea n a b c m ea n 4 0 5 0 6 0 5 0 Overview 5
Va ria bles fl oa t Ty p e int a, b, c; fl oa t m ea n; a = 4 5 ; b = 5 3 ; c = 6 3 ; m ea n = (a + b + c)/ 3.0 ; a b c m ea n a b c m ea n 4 5 5 3 6 3 5 3.6...6 7 fl oa t ty p e Overview 6
C a nd Assem bly Cod e View va ria ble na m e a va ria ble va lue 4 0 a d d ress d a ta b c m ea n 5 0 6 0 5 0 c source cod e Com p iler 4 5 0 0 4 5 1 0 4 5 2 0 4 5 3 0 a ssem bly cod e 4 0 5 0 6 0 5 0 Overview 7
Mem ory :(Ad d ress, D a ta ) a a d d ress a d d ress d a ta a va lue b a d d ress 4 5 0 0 4 0 b va lue c a d d ress m ea n a d d ress 4 5 1 0 5 0 c va lue m ea n va lue 4 5 2 0 6 0 4 5 3 0 5 0 com p iler d eterm ines th e a d d resses Overview 8
Ad d resses of va ria bles :com p iler d eterm ined a d d ress a d d ress of a d d ress of a d d ress of a d d ress of Overview 9
Exa m p le a d d ress a nd d a ta of a va ria ble a d d ress 4 5 0 0 4 0 & a a 4 5 0 0 4 0 :a d d ress ( ) :d a ta ( ) Abstra ct Rep resenta tion & a a Overview 1 0
Mem ory a nd th e & op era tor a d d ress a 4 0 a d d ress va lue b 5 0 4 5 0 0 4 0 c m ea n 6 0 5 0 4 5 1 0 4 5 2 0 5 0 6 0 4 5 3 0 5 0 Overview 1 1
A va ria ble ca n h old a n a d d ress a = 4 0 ; Va lue a ssig nm ent to a int a ; 4 5 0 0 = 4 0 & a a 4 5 0 0 4 0 :a d d ress ( ) :d a ta ( ) p = & a ; Ad d ress a ssig nm ent to p int * p ; G enera labstra ct Rep resenta tion = & a Overview 1 2
D a ta a t a n a d d ress a d d ress d a ta a d d ress of d a ta a t th e a d d ress of a d d ress of d a ta a t th e a d d ress of a d d ress of d a ta a t th e a d d ress of a d d ress of d a ta a t th e a d d ress of Overview 1 3
Th e & a nd * op era tors [int a ;] T h e a d d r e s s o f a v a r i a b l e : A d d r e s s o f o p e r a t o r & t h e v a l u e a t a n a d d r e s s : D e r e f e r e n c i n g o p e r a t o r * * & a a & a a & Overview 1 4
Th e & a nd * op era tors [int * p ;] T h e a d d r e s s o f a v a r i a b l e : A d d r e s s o f o p e r a t o r & t h e v a l u e a t a n a d d r e s s : D e r e f e r e n c i n g o p e r a t o r * * & p p & p p & & (p )= & p * (& p )= p * p * p p * p & & (* p )= p * (p )= * p va lue of p ( ) Overview 1 5
Th e & a nd * ca ncelea ch oth er & (* p )= p * (& p )= p [2 ] * & p p & p p * (& p )= p [1 ] & [1 ] * p * p & (* p )= p p * p [2 ] & Overview 1 6
Pointer Ty p e D ecla ra tion int a ; a h old s integ er & a a int * p ; p h old s * p h old s integ er & p va lue of p ( ) p p * p Overview 1 7
Ad d ress Assig nm ent w ith a Pointer Va ria ble int * p ; a d d ress va lue p = & a ; : a d d r of p : va lue of p :& a (a d d r of a ) 4 5 0 0 4 5 1 0 4 5 2 0 4 0 5 0 6 0 4 5 3 0 5 0 4 5 4 0 Overview 1 8
Va lue Assig nm ent w ith a Pointer Va ria ble int * p ; a d d ress va lue p = & a ; * p = 5 5 ; : a d d r of p : va lue of p :& a (a d d r of a ) : p p oints to ( va lue of p ) : va lue 5 5 4 5 0 0 4 5 1 0 4 5 2 0 4 5 3 0 5 0 6 0 5 0 4 5 4 0 4 5 0 0 Overview 1 9
p rintf():th e built-in function Exp ected Outp ut p rintf( Th e m ea n of th ree num bers \n ); p rintf( a = % d \n, a ); p rintf( b = % d \n, b); p rintf( c = % d \n, c); p rintf( m ea n (% d, % d, % d )= > % d \n, a, b, c, m ea n); Overview 2 0
sca nf():a noth er built-in function Inp ut Exa m p le a d d ress va lue 4 5 0 0 4 0 4 5 1 0 5 0 4 5 2 0 6 0 p rintf( Enter th ree num bers! \n ); p rintf( a = ); ( % d, ); p rintf( b = ); ( % d, ); p rintf( c = ); ( % d, ); Overview 2 1
Th e Ma in Function (1 ) { (void ) int a, b, c; int m ea n; a = 4 0 ; b = 5 0 ; c = 6 0 ; m ea n = (a + b + c)/ 3 ; p rintf( Th e m ea n of th ree num bers \n ); p rintf( a = % d \n, a ); p rintf( b = % d \n, b); p rintf( c = % d \n, c); p rintf( m ea n (% d, % d, % d )= > % d \n, a, b, c, m ea n); } Overview 2 2
Th e Ma in Function (2 ) { (void ) int a, b, c; int m ea n; p rintf( Enter th ree num bers! \n ); p rintf( a = ); ( % d, ); p rintf( b = ); ( % d, ); p rintf( c = ); ( % d, ); m ea n = (a + b + c)/ 3 ; p rintf( Th e m ea n of th ree num bers \n ); p rintf( a = % d \n b = % d \n c = % d \n, a, b, c); p rintf( m ea n (% d, % d, % d )= > % d \n, a, b, c, m ea n ); } Overview 2 3
Th e Ma in Function (3 ) { (void ) int a, b, c; int m ea n; g et_num bers(? ); com p ute_m ea n(? ); p rint_num bers(? ); } Overview 2 4
Function com p ute_m ea n() com p ute_m ea n (int, int, int ); m a in (void ) { int m ea n; * Ca llby Va lue m ea n = com p ute_m ea n(4 0, 5 0, 6 0 ); } { int va lues a re cop ied com p ute_m ea n (int, int, int ) int a vg ; a vg = ( + + )/ 3 ; return( a vg ); * Loca lva ria ble } * Return Va lue Overview 2 5
Function g et_num bers() g et_num bers (int *, int *, int * ); m a in (void ) { int a, b, c; * Ca llby Reference g et_num bers(& a, & b, & c); } a d d resses a re cop ied { } g et_num bers (int *, int *, int * ) p rintf( Enter th ree num bers! \n ); p rintf( a = ); ( % d, ); p rintf( b = ); ( % d, ); p rintf( c = ); ( % d, ); * N o Return Va lue Overview 2 6
Ca llby Va lue & Ca llby Reference * Ca llby Va lue m ea n = com p ute_m ea n(4 0, 5 0, 6 0 ); 4 0 5 0 6 0 com p ute_m ea n (int, int, int ) & x x= 4 0 & y y = 5 0 & z z = 6 0 * Ca llby Reference g et_num bers(& a, & b, & c); & a 4 0 & b 5 0 & c 6 0 g et_num bers (int *, int *, int * ) & x x= & a & y y = & b & z z = & c Overview 2 7
Ch a ng e th e ca ller s va ria bles * Ca llby Reference a d d ress va lue & a 4 0 & b 5 0 & c 6 0 Ca ller s Va ria bles & x x= & a & y y = & b & z z = & c N ow, va lues of a, b, c a re ch a ng ed in th e g et_num bers()function * x= 1 0 0 ; * y = 2 0 0 ; * z = 3 0 0 ; a = 1 0 0 ; b= 2 0 0 ; c= 3 0 0 ; Th e ca llee ca n ch a ng e th e va lues of th e ca ller s va ria bles & a 1 0 0 & b 2 0 0 & c 3 0 0 Overview 2 8
Function p rint_num bers() { } p rint_num bers (int, int, int, int ) p rintf( Th e m ea n of th ree num bers \n ); p rintf( a = % d \n b = % d \n c = % d \n, x, y, z ); p rintf( m ea n (% d, % d, % d )= > % d \n, x, y, z, a vg ); * N o Return Va lue * Ca llby Va lue m a in (void ) { int a, b, c; int m ea n; } p rint_num bers(a, b, c, m ea n); Overview 2 9
Th e Ma in Function (4 ) g et_num bers (int *, int *, int * ); com p ute_m ea n (int, int, int ); p rint_num bers (int, int, int, int ); Prototy p es m a in (void ) { int a, b, c; int m ea n; g et_num bers(& a, & b, & c); m ea n = com p ute_m ea n(a, b, c); } p rint_num bers(a, b, c, m ea n); Overview 3 0
Th e Ma in Pa ra m eters (1 ) #includ e < std io.h > int m a in(int a rg c, ch a r * a rg v[]) { } int i; p rintf("a rg c= % d \n", a rg c); for (i= 0 ;i< a rg c;+ + i){ p rintf("a rg v[% d ] = % s \n", i, a rg v[i]); } Overview 3 1
Th e Ma in Pa ra m eters (2 ) y oung @ usy s ~ $ a rg c= 1 a rg v[0 ] =./a.out y oung @ usy s ~ $ a rg c= 4 a rg v[0 ] =./a.out a rg v[1 ] = one a rg v[2 ] = tw o a rg v[3 ] = th ree y oung @ usy s ~ $ a rg c= 5 a rg v[0 ] =./a.out a rg v[1 ] = one a rg v[2 ] = tw o a rg v[3 ] = th ree a rg v[4 ] = four Overview 3 2
Th e Ma in Pa ra m eters (3 ) y oung @ usy s ~ $ a rg c= 3 a rg v[0 ] =./a.out a rg v[1 ] = one a rg v[2 ] = tw o a rg v[0 ] a rg v[1 ] a rg v[2 ] a rg v[3 ] \0 \0 \0 Overview 3 3
[1 ] Essentia lc, N ickpa rla nte [2 ] Effi cient C Prog ra m m ing, Ma rka. W eiss [3 ] C A Reference Ma nua l, Sa m uelp. H a rbison & G uy L. Steele Jr. [4 ] C La ng ua g e Exp ress, I. K. Ch un