Download presentation
Presentation is loading. Please wait.
Published byShon Rich Modified over 9 years ago
1
F28PL1 Programming Languages Lecture 7 Programming in C - 2
2
Function declaration type name ( type 1 name 1,..., type N name N ) { declarations statements } result type optional – default is int name i == formal parameters {... } == body declarations optional
3
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;
4
Function call name ( exp 1,..., exp N ) – exp i == actual parameters 1.evaluate exp i from left to right 2.push exp i 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 ;
5
Example: polynomial poly2.c #include 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)); }
6
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
7
Example: swap #include swap(int a,int b) { int t; t = a; a = b; b = t; }
8
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
9
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
10
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
11
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
12
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
13
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 explicitly NULL empty pointer
14
Changing parameters to change actual parameter – pass address ( & ) of actual to formal – change indirect ( * ) on formal so formal must be pointer type
15
Example: swap2 swap2.c #include 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 }
16
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
17
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
18
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
19
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)
20
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)
21
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)
22
Characters ‘ character ’ English character set ‘\n’ – newline ‘\t’ – tab one byte
23
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
24
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
25
Files fclose(FILE *) close file system may not do this for you when program stops for I/O, C handles characters as int s EOF == system end of file constant
26
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
27
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
28
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] == 1 st argument – argv[2] == 2 nd argument...
29
exit exit(int) end program return value of int to outer system in stdlib library
30
Example: file copy $ copy path 1 path 2 1.check correct number of arguments 2.open file 1 – check file 1 exists 3.open file 2 – check file 2 exists 4.copy characters from file 1 to file 2 until EOF 5.close files
31
Example: file copy #include copy(FILE * fin,FILE * fout) { char ch; ch = getc(fin); while(ch!=EOF) { putc(ch,fout); ch = getc(fin); }
32
Example: file copy main(int argc,char ** argv) { FILE * fin, * fout; if(argc!=3) { printf("copy: wrong number of arguments\n"); exit(0); }
33
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); }
34
Character values characters have integer values ASCII (American Standard Code for Information Interchange) representation is international standard characters in usual sequences have sequential values charinthexcharinthexcharinthex ‘0’4830‘A’6541‘a’9761 ‘1’4931‘B’6642‘b’9862... ‘9’5739‘Z’905A‘z’1227A
35
Character values very useful for character processing e.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
36
Example: input binary number inputvalue 110110 10112*0+1 1 – 1 0112*1+1 3 – 11 112*3+0 6 – 110 12*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
37
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
38
Example: input binary number binary.c #include 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; }
39
Example: input binary number main(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); }
40
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); }
41
Example: input binary number bdata.txt 0 1 10 11 100 101... 1111 $ binary bdata.txt 0 1 2 3 4 5... 15
42
Condition: switch switch( expression ) { case constant 1 : statements 1 case constant 2 : statements 2... default: statements N } 1.evaluate expression to a value 2.for first constant i with same value, execute statements i 3.if no constants match expression, evaluate default statements N
43
Condition: switch switch( expression ) { case constant 1 : statements 1 case constant 2 : statements 2... default: statements N } only matches char & int/short/long constants break; end switch NB no break at end of statements i execute statements i+1 !
44
Example: finite state automata basis of UML state diagrams rules for state i : ( state i, symbol j ) -> ( new-state ij, output ij ) in state i with input symbol j, output output ij and change to state ij
45
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
46
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 $ parity 10110110;
47
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,-) $ parity 10110110; ODD
48
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,-) $ parity 10110110; ODD
49
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,-) $ parity 10110110; ODD EVEN
50
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,-) $ parity 10110110; ODD EVEN ODD
51
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,-) $ parity 10110110; ODD EVEN ODD
52
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,-) $ parity 10110110; ODD EVEN ODD EVEN
53
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,-) $ parity 10110110; ODD EVEN ODD EVEN ODD
54
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,-) $ parity 10110110; ODD EVEN ODD EVEN ODD
55
Example: general FSA int state; – current state state i int symbol; – current symbol symbol j char * output; - output symbol = getc(...);
56
Example: general FSA while(1) { switch (state) {... case state i : switch(symbol) {... case symbol j : state = new-state ij ; output = output ij ; break;... ( state i, symbol j ) -> ( new-state ij, output ij )
57
Example: general FSA default: printf(“state %d: bad symb %c\n”, state,symbol); exit(0); } break;... } printf(“%s\n”,output); symbol = getc(...); }
58
Example: FSA for parity check #include #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);
59
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)
60
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,-)
61
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,-)
62
Keyboard/display char I/O for keyboard input int getchar() getc(stdin) for screen output int putchar(int) putc(int,stdout)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.