Download presentation
Presentation is loading. Please wait.
Published byTamsin Hubbard Modified over 9 years ago
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 (2 nd 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 name 1.c $ gcc –o name 2 name 1.c compile name 1.c using GNU C compiler and put executable in name 2 $ name 2 run compiled program in name 2 $ name 2 arg 1 … arg N run name 2 with command line args arg 1 … arg N
5
Running C programs $ gcc … -O … generate optimised code $ gcc –c name 1.c … name N.c generate object files name 1.o … name N.o only $ gcc –o name name 1.o … name N.o link object files name 1.o … name N.o and put executable in name
6
Running C programs $ gcc name.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 1.#include... 2.#define... 3.extern... 4.declarations 5.function declarations 6.main(int argc,char ** argv) 7.{... }
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. for I/O 1.#include... 2.#define... 3.extern... 4.declarations 5.function declarations 6.main(int argc, 7. char ** argv) 8.{... }
9
Program layout 2. macro and constant definitions 3. names/types of variables/functions used in this file but declared in linked files 1.#include... 2.#define... 3.extern... 4.declarations 5.function declarations 6.main(int argc, 7. char ** argv) 8.{... }
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 ; 1.#include... 2.#define... 3.extern... 4.declarations 5.function declarations 6.main(int argc, 7. char ** argv) 8.{... }
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 main(int argc,char ** argv) { printf("hello\n"); }... $ gcc -o hello hello.c $ hello hello $
13
Memory organisation stack – allocated automatically – global declarations – local declarations – function parameters heap – allocated by program – c.f. Java new – no garbage collection stack heap top of stack top of heap memory
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 name 1 ; type name 2 ;... type name N ; type name 1,name 2...name N ;
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 int sizeof( type ) returns number of bytes for type
18
Display output 2 printf(“ format ”, exp 1...exp N ) displays the values of expressions exp 1...exp N depending 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 main(int argc,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 typesize typesize.c $ typesize char: 1 int: 4 short: 2 long: 4 float: 4 double: 8
22
Hexadecimal byte sequences may be described in hexadecimal 0X h 1 h 2...h N where h i == 0..9 A..F digitbinarydigitbinary 0000081000 1000191001 20010A1010 30011B1011 40100C1100 50101D1101 60110E1110 70111F1111
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; 67452301 0123 a byte
24
Address operator: & declaration: type name associates name with address of enough memory for type & name address of 1 st byte allocated to variable name lvalue on PC Linux: address4 bytes
25
Memory allocation to declarations e.g. addrs.c #include main(int argc,char ** argv) { int a,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 a: bfe5118c b: bfe51188 c: bfe51184 x: bfe51178 y: bfe51170 z: bfe51168 0 1 2 3 x x 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
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 * 10 11 character: ‘ letter ’ e.g. ‘a’ ‘\n’
29
Operator expressions unary op exp – evaluate exp – apply op to value binary infix exp 1 op exp 2 – evaluate exp 1 – evaluate exp 2 – 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 ( exp 1...exp N ) evaluate actual parameters exp 1 to exp N – pushing values onto stack function will access formal parameters via stack result of function execution is returned
35
Keyboard input int scanf(“ format ”, addr 1...addr N ) inputs from keyboard to memory at specified addresses – depending on format characters typically, addr i is & name i i.e. address associated with variable name i int return value for success or end of input or failure
36
evaluate AX 2 +BX+C #include main(int argc,char ** argv) { int a,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); } Example: polynomial evaluation $ poly a: 3 b: 8 c: 4 x: 6 160
37
evaluate AX 2 +BX+C #include main(int argc,char ** argv) { int a,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); } Example: polynomial evaluation $ 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 ) 1.get address associated with name 2.get value from memory at that address
39
Assignment expression 1 = expression 2 ; evaluate expression 1 to give an address – lvalue – on left of assignment evaluate expression 2 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 expression 2 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 1.evaluate expression 2.if non-zero then i.execute statement ii.repeat from 1. 3.if zero then end iteration break in statement ends enclosing iteration
46
Example: sum and average sumav.c include main(int argc,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( exp 1 ; exp 2 ; exp 3 ) statement exp 1 ; while( exp 2 ) { statement exp 3 ; } 1.execute statement exp 1 2.repeatedly test exp 2 3.each time exp 2 is true 1.execute statement 2.execute statement exp3 all exps and statement are optional
50
Iteration: for for( exp 1 ; exp 2 ; exp 3 ) statement usually: – exp 1 initialises loop control variable – exp 2 checks if termination condition met for control variable – exp 3 changes control variable NB must declare control variable before for
51
Condition: if if( expression ) statement 1 else statement 2 1.evaluate expression 2.if non-zero then execute statement 1 3.if zero then execute statement 2 else statement 2 is optional – if expression is zero then go on to next statement
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.