620 likes | 749 Views
F28PL1 Programming Languages. Lecture 7 Programming in C - 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.
E N D
F28PL1 Programming Languages Lecture 7 Programming in C - 2
Function declaration type name(type1 name1,...,typeNnameN) { declarations statements } • result type optional • default is int • namei== formal parameters • { ... } == body • declarations optional
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;
Function call name(exp1,...,expN) • expi == actual parameters • evaluate expifrom left to right • push expi values onto stack • execute body, allocating stack space to any declarations • reclaim stack space for parameters & any declarations • return result if any • NB end statement function call with ;
Example: polynomial poly2.c #include <stdio.h> int poly(inta,intb,intc,int x) { return a*x*x+b*x+c; } main(intargc,char ** argv) { printf("%d\n",poly(2,4,3,5)); }
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
Example: swap #include <stdio.h> swap(inta,int b) { int t; t = a; a = b; b = t; }
Example: swap main(intargc, char ** argv) { intx,y; x = 1; y = 2; swap(x,y); printf("x: %d, y: %d\n",x,y); } x: 1, y: 2
Example: swap swap(inta,int b) { int t; t = a; a = b; b = t; } ... intx,y; x = 1; y = 2; swap(x,y); 2 y 1 x
Example: swap swap(inta,int b) { int t; t = a; a = b; b = t; } ... intx,y; x = 1; y = 2; swap(x,y); t 2 b 1 a 2 y 1 x
Example: swap swap(inta,int b) { int t; t = a; a = b; b = t; } ... intx,y; x = 1; y = 2; swap(x,y); 1 t 1 b 2 a 2 y 1 x
Example: swap swap(inta,int b) { int t; t = a; a = b; b = t; } ... intx,y; x = 1; y = 2; swap(x,y); 2 y 1 x
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
Changing parameters • to change actual parameter • pass address (&) of actual to formal • change indirect (*) on formal • so formal must be pointer type
Example: swap2 swap2.c #include <stdio.h> swap(int * a,int * b)- parameters point toint { 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 }
Example: swap2 main(intargc, char ** argv) { intx,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
Example: swap2 swap(int * a,int * b) { int t; t = *a; *a = *b; *b = t; } ... intx,y; x = 1; y = 2; swap(&x,&y); 2 y 1 x
Example: swap2 swap(int * a,int * b) { int t; t = *a; *a = *b; *b = t; } ... intx,y; x = 1; y = 2; swap(&x,&y); t b a 2 y 1 x
Example: swap2 swap(int * a,int * b) { int t; t = *a; (i) *a = *b; *b = t; } ... intx,y; x = 1; y = 2; swap(&x,&y); 1 t b (i) a 2 y 1 x
Example: swap2 swap(int * a,int * b) { int t; t = *a; *a = *b; (ii) *b = t; } ... intx,y; x = 1; y = 2; swap(&x,&y); 1 t b a 2 y (ii) 2 x
Example: swap2 swap(int * a,int * b) { int t; t = *a; *a = *b; *b = t; } (iii) ... intx,y; x = 1; y = 2; swap(&x,&y); 1 t b (iii) a 1 y 2 x
Characters ‘character’ • English character set ‘\n’ – newline ‘\t’ – tab • one byte
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
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
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
File character I/O intgetc(FILE *) • return next byte from file address • as int • return EOF if at end of file intputc(int,FILE *) • puts int to file address • as byte • return EOF if failure
File character I/O intgetc(FILE *) • return next byte from file address • as int • return EOF if at end of file intputc(int,FILE *) • puts int to file address • as byte • return EOF if failure
Command line main(intargc; 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 ...
exit exit(int) • end program • return value of int to outer system • in stdliblibrary
Example: file copy $ copy path1 path2 • check correct number of arguments • open file1 • check file1exists • open file2 • check file2exists • copy characters from file1 to file2 until EOF • close files
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); } }
Example: file copy main(intargc,char ** argv) { FILE * fin, * fout; if(argc!=3) { printf("copy: wrong number of arguments\n"); exit(0); }
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); }
Character values • characters have integer values • ASCII (American Standard Code for Information Interchange) representation is international standard • characters in usual sequences have sequential values
Character values • very useful for character processing e.g. char chis: • 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
Example: input binary number • initial value is 0 • for each binary digit • multiply value by 2 • add value of binary digit
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
Example: input binary number binary.c #include <stdio.h> #include <stdlib.h> intgetBinary(FILE * fin,int * ch) { intval; val = 0; while (*ch=='0' || *ch=='1') { val = 2*val+(*ch)-'0'; *ch = getc(fin); } return val; }
Example: input binary number main(intargc,char ** argv) { FILE * fin; intch; intval; 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); }
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); }
Example: input binary number bdata.txt 0 1 10 11 100 101 ... 1111 $ binary bdata.txt 0 1 2 3 4 5 ... 15
Condition: switch switch(expression) { case constant1:statements1 case constant2:statements2 ... default:statementsN } • evaluate expression to a value • for first constanti with same value, execute statementsi • if no constants match expression, evaluate defaultstatementsN
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 !
Example: finite state automata • basis of UML state diagrams • rules for statei: (statei,symbolj) -> (new-stateij,outputij) • in stateiwith input symbolj, output outputij and change to stateij
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
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;
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
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 ODD
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 ODD EVEN
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 ODD EVEN ODD