62
F28PL1 Programming Languages Lecture 7 Programming in C - 2

F28PL1 Programming Languages Lecture 7 Programming in C - 2

Embed Size (px)

Citation preview

Page 1: F28PL1 Programming Languages Lecture 7 Programming in C - 2

F28PL1 Programming Languages

Lecture 7 Programming in C - 2

Page 2: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Function declaration

type name(type1 name1,...,typeN nameN)

{ declarations statements}• result type optional – default is int

• namei == formal parameters• { ... } == body• declarations optional

Page 3: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Function declaration

• formal parameters optional • expression function– last statement to be executed should be:return expression;

• statement function– no result type– may end call with:

return;

Page 4: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Function call

name(exp1,...,expN)

– expi == actual parameters

1. evaluate expi from left to right

2. push expi values onto stack

3. execute body, allocating stack space to any declarations

4. reclaim stack space for parameters & any declarations

5. return result if any• NB end statement function call with ;

Page 5: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: polynomial

poly2.c#include <stdio.h>

int poly(int a,int b,int c,int x){ return a*x*x+b*x+c; }

main(int argc,char ** argv){ printf("%d\n",poly(2,4,3,5));}

Page 6: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Changing parameters

• parameters passed by value• value of actual parameter copied to space for

formal parameter• final value is not copied back from formal to

actual• so changing the formal does not change the

actual

Page 7: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap

#include <stdio.h>

swap(int a,int b){ int t; t = a; a = b; b = t;}

Page 8: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap

main(int argc, char ** argv){ int x,y; x = 1; y = 2; swap(x,y); printf("x: %d, y: %d\n",x,y);}

x: 1, y: 2

Page 9: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap

swap(int a,int b){ int t; t = a; a = b; b = t; }...int x,y;x = 1;y = 2;swap(x,y); x

y

1

2

Page 10: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap

swap(int a,int b){ int t; t = a; a = b; b = t; }...int x,y;x = 1;y = 2;swap(x,y); x

y

1

2

a

b

1

2

t

Page 11: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap

swap(int a,int b){ int t; t = a; a = b; b = t; }...int x,y;x = 1;y = 2;swap(x,y); x

y

1

2

a

b

1

2

t

1

Page 12: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap

swap(int a,int b){ int t; t = a; a = b; b = t; }...int x,y;x = 1;y = 2;swap(x,y); x

y

1

2

Page 13: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Pointers

type * name;• variable name holds address for byte

sequence for type• allocates stack space for address but does not

create instance of type• must allocate space explicitlyNULL• empty pointer

Page 14: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Changing parameters

• to change actual parameter– pass address (&) of actual to formal– change indirect (*) on formal

• so formal must be pointer type

Page 15: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap2

swap2.c#include <stdio.h>

swap(int * a,int * b)- parameters point to int{ int t; t = *a; - t is value a points to *a = *b; - value a points to is value b points to *b = t; - value b points to is t’s value}

Page 16: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap2

main(int argc, char ** argv){ int x,y; x = 1; y = 2; swap(&x,&y); - pass pointers to a and b printf("x: %d, y: %d\n",x,y);}

x: 2, y: 1

Page 17: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap2

swap(int * a,int * b){ int t; t = *a; *a = *b; *b = t; }...int x,y;x = 1;y = 2;swap(&x,&y); x

y

1

2

Page 18: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap2

swap(int * a,int * b){ int t; t = *a; *a = *b; *b = t; }...int x,y;x = 1;y = 2;swap(&x,&y); x

y

1

2

a

b

t

Page 19: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap2

swap(int * a,int * b){ int t; t = *a; (i) *a = *b; *b = t; }...int x,y;x = 1;y = 2;swap(&x,&y); x

y

a

b

1

1

t

2

(i)

Page 20: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap2

swap(int * a,int * b){ int t; t = *a; *a = *b; (ii) *b = t; }...int x,y;x = 1;y = 2;swap(&x,&y); x

y

a

b

1

2

t

2(ii)

Page 21: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: swap2

swap(int * a,int * b){ int t; t = *a; *a = *b; *b = t; } (iii)...int x,y;x = 1;y = 2;swap(&x,&y); x

y

a

b

1

2

t

1

(iii)

Page 22: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Characters

‘character’• English character set‘\n’ – newline‘\t’ – tab• one byte

Page 23: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Files

FILE• type for system representation of file• all files are treated as text– i.e. character sequences

FILE * name;• name is the address of a FILE value

Page 24: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Files

FILE * fopen(path,mode)• open file with path string• return – FILE address– NULL if no file

• mode – “r” – read– “w” – write – create new empty file – delete old version!– “a” – append i.e. write at end of existing file

Page 25: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Files

fclose(FILE *)• close file• system may not do this for you when program stops

• for I/O, C handles characters as ints • EOF == system end of file constant

Page 26: F28PL1 Programming Languages Lecture 7 Programming in C - 2

File character I/O

int getc(FILE *)• return next byte from file address– as int

• return EOF if at end of file

int putc(int,FILE *)• puts int to file address– as byte

• return EOF if failure

Page 27: F28PL1 Programming Languages Lecture 7 Programming in C - 2

File character I/O

int getc(FILE *)• return next byte from file address– as int

• return EOF if at end of file

int putc(int,FILE *)• puts int to file address– as byte

• return EOF if failure

Page 28: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Command line

main(int argc; char ** argv) • argc == number of command line arguments– including call to executable

• argv == pointer to pointer to char– i.e. pointer to sequence of char

• i.e. array of strings

– argv[0] == name of executable– argv[1] == 1st argument– argv[2] == 2nd argument ...

Page 29: F28PL1 Programming Languages Lecture 7 Programming in C - 2

exit

exit(int)• end program• return value of int to outer system• in stdlib library

Page 30: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: file copy

$ copy path1 path2

1. check correct number of arguments2. open file1

– check file1 exists

3. open file2

– check file2 exists

4. copy characters from file1 to file2 until EOF

5. close files

Page 31: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: file copy#include <stdio.h>#include <stdlib.h> copy(FILE * fin,FILE * fout){ char ch; ch = getc(fin); while(ch!=EOF) { putc(ch,fout); ch = getc(fin); }}

Page 32: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: file copy main(int argc,char ** argv){ FILE * fin, * fout; if(argc!=3) { printf("copy: wrong number of arguments\n"); exit(0); }

Page 33: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: file copy fin = fopen(argv[1],"r"); if(fin==NULL) { printf("copy: can't open %s\n",argv[1]); exit(0); } fout = fopen(argv[2],"w"); if(fout==NULL) { printf("copy: can't open %s\n",argv[2]); exit(0); } copy(fin,fout); fclose(fin); fclose(fout);}

Page 34: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Character values

• characters have integer values• ASCII (American Standard Code for Information

Interchange) representation is international standard

• characters in usual sequences have sequential values

char int hex char int hex char int hex

‘0’ 48 30 ‘A’ 65 41 ‘a’ 97 61

‘1’ 49 31 ‘B’ 66 42 ‘b’ 98 62

... ... ...

‘9’ 57 39 ‘Z’ 90 5A ‘z’ 122 7A

Page 35: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Character values

• very useful for character processinge.g. char ch is:• lower case if: ‘a’ <= ch && ch <= ‘z’• upper case if: ‘A’ <= ch && ch <= ‘Z’• digit if: ‘0’ <= ch && ch <= ‘9’• if ch is a digit then it represents value: ch-’0’• e.g. ‘7’-’0’ 55-48 7

Page 36: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: input binary number

input value

11011 0

1011 2*0+1 1 – 1011 2*1+1 3 – 1111 2*3+0 6 – 1101 2*6+1 13 – 1101

2*13+1 27 – 11011

•initial value is 0•for each binary digit• multiply value by 2• add value of binary digit

Page 37: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: input binary number

• function to convert text from file to value• parameters for:– file– next character

• input each character from file into a non-local variable– pass address of character variable– access character indirect on address

Page 38: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: input binary number

binary.c

#include <stdio.h>#include <stdlib.h>

int getBinary(FILE * fin,int * ch){ int val; val = 0; while (*ch=='0' || *ch=='1') { val = 2*val+(*ch)-'0'; *ch = getc(fin); } return val;}

Page 39: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: input binary numbermain(int argc,char ** argv){ FILE * fin; int ch; int val;

if(argc!=2) { printf("getBinary: wrong number of arguments\n"); exit(0); } if((fin=fopen(argv[1],"r"))==NULL) { printf("getBinary: can't open %s\n",argv[1]); exit(0); }

Page 40: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: input binary number ch = getc(fin); while(1) { while(ch!='0' && ch !='1' && ch!=EOF) – skip to binary digit ch=getc(fin); if(ch==EOF) break; val = getBinary(fin,&ch); printf("%d\n",val); } fclose(fin);}

Page 41: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: input binary number

bdata.txt011011100101...1111

$ binary bdata.txt012345...15

Page 42: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Condition: switch

switch(expression){ case constant1: statements1

case constant2: statements2

... default: statementsN

}1. evaluate expression to a value2. for first constanti with same value, execute statementsi

3. if no constants match expression, evaluate default statementsN

Page 43: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Condition: switch

switch(expression){ case constant1: statements1

case constant2: statements2

... default: statementsN

}• only matches char & int/short/long constants• break; end switch• NB no break at end of statementsi execute

statementsi+1 !

Page 44: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: finite state automata

• basis of UML state diagrams• rules for statei:

(statei,symbolj) -> (new-stateij,outputij)

in statei with input symbolj, output outputij and change to stateij

Page 45: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: parity check

• technique for checking data errors• sender:– adds extra 1 or 0 to byte to make number of 1 bits

odd (odd parity) or even (even parity)– transmits extended byte

• receiver:– checks parity unchanged– built into low level harware

Page 46: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: parity check

• end input with a ‘;’(start,0) -> (even,EVEN)(start,1) -> (odd,ODD)(even,0) -> (even,EVEN)(even,1) -> (odd,ODD)(even,;) -> (stop,-)(odd,0) -> (odd,ODD)(odd,1) -> (even,EVEN)(odd,;) -> (stop,-)• start with EVEN

$ parity10110110;

Page 47: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: parity check

• end input with a ‘;’(start,0) -> (even,EVEN)(start,1) -> (odd,ODD)(even,0) -> (even,EVEN)(even,1) -> (odd,ODD)(even,;) -> (stop,-)(odd,0) -> (odd,ODD)(odd,1) -> (even,EVEN)(odd,;) -> (stop,-)

$ parity10110110;ODD

Page 48: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: parity check

• end input with a ‘;’(start,0) -> (even,EVEN)(start,1) -> (odd,ODD)(even,0) -> (even,EVEN)(even,1) -> (odd,ODD)(even,;) -> (stop,-)(odd,0) -> (odd,ODD)(odd,1) -> (even,EVEN)(odd,;) -> (stop,-)

$ parity10110110;ODDODD

Page 49: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: parity check

• end input with a ‘;’(start,0) -> (even,EVEN)(start,1) -> (odd,ODD)(even,0) -> (even,EVEN)(even,1) -> (odd,ODD)(even,;) -> (stop,-)(odd,0) -> (odd,ODD)(odd,1) -> (even,EVEN)(odd,;) -> (stop,-)

$ parity10110110;ODDODDEVEN

Page 50: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: parity check

• end input with a ‘;’(start,0) -> (even,EVEN)(start,1) -> (odd,ODD)(even,0) -> (even,EVEN)(even,1) -> (odd,ODD)(even,;) -> (stop,-)(odd,0) -> (odd,ODD)(odd,1) -> (even,EVEN)(odd,;) -> (stop,-)

$ parity10110110;ODDODDEVENODD

Page 51: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: parity check

• end input with a ‘;’(start,0) -> (even,EVEN)(start,1) -> (odd,ODD)(even,0) -> (even,EVEN)(even,1) -> (odd,ODD)(even,;) -> (stop,-)(odd,0) -> (odd,ODD)(odd,1) -> (even,EVEN)(odd,;) -> (stop,-)

$ parity10110110;ODDODDEVENODDODD

Page 52: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: parity check

• end input with a ‘;’(start,0) -> (even,EVEN)(start,1) -> (odd,ODD)(even,0) -> (even,EVEN)(even,1) -> (odd,ODD)(even,;) -> (stop,-)(odd,0) -> (odd,ODD)(odd,1) -> (even,EVEN)(odd,;) -> (stop,-)

$ parity10110110;ODDODDEVENODDODDEVEN

Page 53: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: parity check

• end input with a ‘;’(start,0) -> (even,EVEN)(start,1) -> (odd,ODD)(even,0) -> (even,EVEN)(even,1) -> (odd,ODD)(even,;) -> (stop,-)(odd,0) -> (odd,ODD)(odd,1) -> (even,EVEN)(odd,;) -> (stop,-)

$ parity10110110;ODDODDEVENODDODDEVENODD

Page 54: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: parity check

• end input with a ‘;’(start,0) -> (even,EVEN)(start,1) -> (odd,ODD)(even,0) -> (even,EVEN)(even,1) -> (odd,ODD)(even,;) -> (stop,-)(odd,0) -> (odd,ODD)(odd,1) -> (even,EVEN)(odd,;) -> (stop,-)

$ parity10110110;ODDODDEVENODDODDEVENODDODD

Page 55: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: general FSA

int state; – current state statei

int symbol; – current symbol symbolj

char * output; - output

symbol = getc(...);

Page 56: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: general FSA

while(1){ switch (state) { ... case statei:

switch(symbol) { ... case symbolj: state = new-stateij;

output = outputij; break;

...

(statei,symbolj) -> (new-stateij,outputij)

Page 57: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: general FSA

default: printf(“state %d: bad symb %c\n”, state,symbol); exit(0); } break; ... } printf(“%s\n”,output); symbol = getc(...);}

Page 58: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: FSA for parity check#include <stdio.h>#include <stdlib.h>

#define START 0#define ODD 1#define EVEN 2

int symbol;int state;char * output;

main(int argc,char ** argv){ state = START; symbol = getc(stdin);

Page 59: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: FSA for parity check while(1) { switch(state) { case START: switch(symbol) { case '0': state = EVEN; output = "EVEN"; break; case '1': state = ODD; output = "ODD"; break; default:printf("state %d: bad symbol: %c\n", state,symbol); exit(0); } break;

(start,0) -> (even,EVEN)(start,1) -> (odd,ODD)

Page 60: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: FSA for parity check case EVEN: switch(symbol) { case '0': state = EVEN; output = "EVEN"; break; case '1': state = ODD; output = "ODD"; break; case ';': exit(0); default: printf("state %d: bad symbol: %c\n", state,symbol); exit(0); } break;

(even,0) -> (even,EVEN)(even,1) -> (odd,ODD)(even,;) -> (stop,-)

Page 61: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Example: parity check case ODD: switch(symbol) { case '0': state = ODD; output = "ODD"; break; case '1': state = EVEN; output = "EVEN"; break; case ';': exit(0); default: printf("state %d: bad symbol: %c\n", state,symbol); exit(0); } break; } printf("%s\n",output); symbol = getc(stdin); }}

(odd,0) -> (odd,ODD)(odd,1) -> (even,EVEN)(odd,;) -> (stop,-)

Page 62: F28PL1 Programming Languages Lecture 7 Programming in C - 2

Keyboard/display char I/O

• for keyboard inputint getchar() getc(stdin)

• for screen output int putchar(int) putc(int,stdout)