1 / 51

F28PL1 Programming Languages

F28PL1 Programming Languages. Lecture 6: Programming in C - 1. Overview. strict, strongly typed, imperative system programming language combines high-level constructs with low level access to type representations and memory

gali
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 6: Programming in C - 1

  2. Overview • strict, strongly typed, imperative system programming language • combines high-level constructs with low level access to type representations and memory • Reference: B. Kernighan & D. Ritchie, The C Programming Language (2nd Ed), Prentice-Hall, 1988

  3. Overview • C looks like Java BUT IS VERY DIFFERENT! • Java has high-level objects • C exposes low-level memory formats & addresses • must manage memory explicitly • very easy to make programming errors • almost invariably address errors

  4. Running C programs • suppose $is Linux prompt • put program text in name1.c $ gcc –o name2name1.c • compile name1.c using GNU C compiler and put executable in name2 $ name2 • run compiled program in name2 $ name2 arg1 … argN • run name2 with command line argsarg1 … argN

  5. Running C programs $ gcc … -O … • generate optimised code $ gcc –c name1.c … nameN.c • generate object files name1.o … nameN.oonly $ gcc –o namename1.o … nameN.o • link object files name1.o … nameN.o and put executable in name

  6. Running C programs $ gccname.c • forgot –o name ? • puts executable in a.out! $ man gcc • Linux manual entry for GNU C compiler • can use cc instead of gcc • proprietary C compiler for host OS/platform

  7. Program layout • #include ... • #define ... • extern ... • declarations • function declarations • main(intargc,char ** argv) • { ... }

  8. Program layout • 1. include files • #include “...” == look in current directory • #include <...> == look in system directories • import libraries via header files: name.h e.g. <stdio.h> for I/O • #include ... • #define ... • extern ... • declarations • function declarations • main(intargc, • char ** argv) • { ... }

  9. Program layout • 2. macro and constant definitions • 3. names/types of variables/functions used in this file but declared in linked files • #include ... • #define ... • extern ... • declarations • function declarations • main(intargc, • char ** argv) • { ... }

  10. Program layout • 4. & 5. declare all variables before all functions • 6. & 7. main function with optional command line argument count and array • declarations and statements terminated with a ; • #include ... • #define ... • extern ... • declarations • function declarations • main(intargc, • char ** argv) • { ... }

  11. Display output 1 printf(“text”) • system function • sends text to the display • \n == newline • \t == tab

  12. Display output 1 • e.g. hello.c #include <stdio.h> main(intargc,char ** argv) { printf("hello\n"); } ... $ gcc -o hello hello.c $ hello hello $

  13. Memory organisation memory • stack • allocated automatically • global declarations • local declarations • function parameters • heap • allocated by program • c.f. Java new • no garbage collection stack top of stack top of heap heap

  14. Declarations type name; • allocates space for new variable of type called name on stack name • letters+ digits+ _starting with a letter • C convention • lower case = variable name • UPPER CASE = symbolic constant

  15. Declarations • can group declarations for same type type name1; type name2; ... type nameN;  type name1,name2...nameN;

  16. Declarations • basic types • char – character • int – integer • short – short integer • long – long integer • float – floating point number • double – double size floating point number

  17. Declarations • amount of space for type depends on • type • platform • compiler • measure type sizes in 8 bit bytes intsizeof(type) • returns number of bytes for type

  18. Display output 2 printf(“format”,exp1...expN) • displays the values of expressions exp1...expNdepending on the format characters %d == decimal integer %f == floating point %x == hexadecimal %s == string

  19. Display output 2 • NB variable number of arguments • must have one format for each argument • any non-format information in string is displayed as text

  20. Sizes of types e.g. typesize.c #include <stdio.h> main(intargc,char ** argv) { printf("char: %d\n",sizeof(char)); printf("int: %d\n",sizeof(int)); printf("short: %d\n",sizeof(short)); printf("long: %d\n",sizeof(long)); printf("float: %d\n",sizeof(float)); printf("double: %d\n", sizeof(double)); }

  21. Sizes of types • on 64 bit PC Linux $ gcc -o typesizetypesize.c $ typesize char: 1 int: 4 short: 2 long: 4 float: 4 double: 8

  22. Hexadecimal • byte sequences may be described in hexadecimal • 0Xh1h2...hN • where hi== 0..9 A..F

  23. Memory allocation to values • 2 digits == 8 bits == 1 byte • NB values stored from most significant to least significant byte e.g. int a; a = 0x01234567; a 67 45 23 01 byte 0 1 2 3

  24. Address operator: & • declaration: type name • associates name with address of enough memory for type &name • address of 1st byte allocated to variable name • lvalue • on PC Linux: address 4 bytes

  25. Memory allocation to declarations • e.g. addrs.c #include <stdio.h> main(intargc,char ** argv) { inta,b,c; double x,y,z; printf("&a: %x, &b: %x, &c: %x\n", &a,&b,&c); printf("&x: %x, &y: %x, &z: %x\n", &x,&y,&z); } ... $ addrs &a: bfe5118c, &b: bfe51188, &c: bfe51184 &x: bfe51178, &y: bfe51170, &z: bfe51168

  26. Memory allocation to declarations • 8c-88 == 4 • 88-84 == 4 • 84-78 == 12 == 8+4? • 64 bit machine • padding to 8 byte boundary • 78-70 == 8 • 70-68 == 8 0 1 2 3 a: bfe5118c b: bfe51188 c: bfe51184 x xxx x: bfe51178 y: bfe51170 z: bfe51168

  27. Expressions • constant value of constant • name value from memory • NB value may differ depending on what type context name is used in • unary/binary expression • function call • NB C function == Java method

  28. Constants • signed integer • e.g. 4231 -2579 • signed decimal • e.g. 886.754 • e.g. -3.9E11 == -3.9 * 1011 • character: ‘letter’ • e.g. ‘a’ ‘\n’

  29. Operator expressions • unary op exp • evaluate exp • apply op to value • binary infix exp1 op exp2 • evaluate exp1 • evaluate exp2 • apply op to values

  30. Arithmetic • unary minus: - • binary infix + == add - == subtract * == multiply / == division % == integer modulo/remainder

  31. Arithmetic • (...) – brackets • precedence (...) before... unary – before... * or / or % before... + or – before ... function call

  32. Arithmetic • expressions evaluated from left to right • BUT compiler may rearrange + and * for efficiency • e.g. A+B*C  LOAD R1,A LOAD R2,B MULT R2,C ADD R1,R2 • e.g. B*C+A  LOAD R1,B MULT R1,C ADD R1,A • NB not ARM assembler

  33. Arithmetic • mixed mode arithmetic permitted • always works at maximum precision needed • for a binary operator: • char & short converted to int • float always converted to double • either operand is double then the other is converted to double • either operand is long then the other is converetd to long

  34. Function call • function called as: name(exp1...expN) • evaluate actual parameters exp1 to expN • pushing values onto stack • function will access formal parameters via stack • result of function execution is returned

  35. Keyboard input intscanf(“format”,addr1...addrN) • inputs from keyboard to memory at specified addresses • depending on format characters • typically, addri is &namei • i.e. address associated with variable namei • intreturn value for success or end of input or failure

  36. Example: polynomial evaluation • evaluate AX2+BX+C #include <stdio.h> main(intargc,char ** argv) { inta,b,c,x; printf("a: "); scanf("%d",&a); printf("b: "); scanf("%d",&b); printf("c: "); scanf("%d",&c); printf("x: "); scanf("%d",&x); printf("%d\n",a*x*x+b*x+c); } $ poly a: 3 b: 8 c: 4 x: 6 160

  37. Example: polynomial evaluation • evaluate AX2+BX+C #include <stdio.h> main(intargc,char ** argv) { inta,b,c,x; printf("a: "); scanf("%d",&a); printf("b: "); scanf("%d",&b); printf("c: "); scanf("%d",&c); printf("x: "); scanf("%d",&x); printf("%d\n",a*x*x+b*x+c); } $ poly a: 3 b: 8 c: 4 x: 6 160 put value at address in memory for variable

  38. Indirection operator: * *expression  • evaluate expression to integer • use integer as address to get value from memory • so name in expression  *(&name) • get address associated with name • get value from memory at that address

  39. Assignment expression1=expression2; • evaluate expression1 to give an address • lvalue – on left of assignment • evaluate expression2 to give a value • rvalue – on right of assignment • put the value in memory at the address

  40. Assignment • assignments are expressions • returns the value of expression2 • value ignored when assignment used as a statement

  41. Logic and logical operators • no boolean values • 0  false • any other valuetrue • unary ! - not • binary && - logical AND || - logical OR

  42. Comparison operators • binary == - equality != - inequality < - less than <= - less than or equal > - greater than >= - greater than or equal

  43. Precedence (...) before && before || before ! before comparison before arithmetic before function call

  44. Block {declarations statements } • declarations are optional • space allocated to declarations • on stack • for life of block

  45. Iteration: while while(expression) statement  • evaluate expression • if non-zero then • execute statement • repeat from 1. • if zero then end iteration • break in statement ends enclosing iteration

  46. Example: sum and average • sumav.c include <stdio.h> main(intargc,char ** argv) { int count; int sum; int n; count = 0; sum = 0;

  47. Example: sum and average printf("next> "); scanf("%d",&n); while(n!=0) { count = count+1; sum = sum+n; printf("next> "); scanf("%d",&n); } printf("count: %d, sum: %d, average: %d\n",count,sum,sum/count); }

  48. Example: sum and average $ sumav next> 1 next> 2 next> 3 next> 4 next> 5 next> 0 count: 5, sum: 15, average: 3

  49. Iteration: for for(exp1;exp2;exp3) statement exp1; while(exp2) { statement exp3; } • execute statement exp1 • repeatedly test exp2 • each time exp2 is true • execute statement • execute statement exp3 • all exps and statement are optional

  50. Iteration: for for(exp1;exp2;exp3) statement • usually: • exp1 initialises loop control variable • exp2 checks if termination condition met for control variable • exp3 changes control variable • NB must declare control variable before for

More Related