1 / 62

F28PL1 Programming Languages

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.

shalom
Download Presentation

F28PL1 Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. F28PL1 Programming Languages Lecture 7 Programming in C - 2

  2. Function declaration type name(type1 name1,...,typeNnameN) { declarations statements } • result type optional • default is int • namei== 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(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 ;

  5. 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)); }

  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 <stdio.h> swap(inta,int b) { int t; t = a; a = b; b = t; }

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  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 <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 }

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  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 ints • EOF == system end of file constant

  26. 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

  27. 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

  28. 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 ...

  29. exit exit(int) • end program • return value of int to outer system • in stdliblibrary

  30. 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

  31. 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); } }

  32. Example: file copy main(intargc,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

  35. 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

  36. Example: input binary number • 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 <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; }

  39. 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); }

  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 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

  43. 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 !

  44. 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

  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 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 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 ODD EVEN ODD

More Related