Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.

Similar presentations


Presentation on theme: "Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators."— Presentation transcript:

1 Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators operands precedence types.

2 Department of Electronic & Electrical Engineering Reading data... Simple IO... scanf #include { int x; scanf("%d",&x); } "%d" is a control string %d is a slot for an integer & is the address of operator &x is the memory location to place the integer (were x is stored). scanf("%d",x); common mistake

3 Department of Electronic & Electrical Engineering Format string (scanf)

4 Department of Electronic & Electrical Engineering scanf ---- WARNING --- scanf can be confusing to use especially if you do not enter your data correctly. scanf reads your input until it has filled all the slots (%s). If you have extra input characters that are not read by a scanf they will be read by the next scanf !

5 Department of Electronic & Electrical Engineering Example using scanf float x,y; char c; scanf("%f %c %f",&x,&c,&y); printf("%f %c %f",x,c,y); There is a slot for each parameter The number and type of parameters must match the slots

6 Department of Electronic & Electrical Engineering DEMO: Dr Leonard writes a program to read in some numbers and print a result. Illustrating the use of scanf and printf.

7 Department of Electronic & Electrical Engineering Writing data ( printf ) %f --- float e.g 34.1 %e --- with exponent 3.41e1 %d --- decimal (for int type) %c --- single character %s --- string (array of char) %p --- address / pointer We can also do: %10.4f --- print least 10 characters (maybe spaces) and print out 4 digits after the decimal point.

8 Department of Electronic & Electrical Engineering Summary of topics covered in this lecture. Blocks { {} {} } Statements and semicolons Variables: Names Declaring Scope. Types Addresses, memory pointers. IO. Reading and writing variables. printf scanf Format Strings (for IO e.g. " %d" ).

9 Department of Electronic & Electrical Engineering Expressions An expression is comprises: variables constants function calls (must return a value) operators (unary or binary) operands

10 Department of Electronic & Electrical Engineering Example: 2.0*x*sin(z*y) constant variable another expression! function 2.0  *x  * ( z*y  sin ) order of evaluation

11 Department of Electronic & Electrical Engineering Arithmetic Operators Arithmetic operators can be used with any of the fundamental types ( int char float ).

12 Department of Electronic & Electrical Engineering Precedence Expressions are usually evaluated from left to right (associativity) If an expression has different operators then precedence rules are applied Brackets can be used to form sub expressions (override precedence) For example arithmetic operators the * / and % operators will be evaluated before + or – e.g. 4+6/2 is equivalent to 4+ (6/2) 4-6+2 (4-6)+2 4/6 - 2 (4/6)-2

13 Department of Electronic & Electrical Engineering Combining different types. Both operands the same type  results is the same. (2/3  0) integer and floating point type t  result will be floating point. e.g. 3.0/2 -> 1.5 When different sizes of the same type are used the result will be the type of the longest operand. These rules are applied to each step as an expression is evaluated e.g. 3/4*4.0 will give the result zero. For the above you could change the order of evaluation e.g. 4.0*3/4 When in doubt use brackets ! e.g. (4.0*3)/4

14 Department of Electronic & Electrical Engineering Examples 4*5.0+6 7/8*34 x*y*func(4,5) 5%2 (78+84)*(34-45) int func(int a,int b) { return a*b-7; }

15 Department of Electronic & Electrical Engineering C and boolean variables (TRUE and FALSE) unlike some other languages C does not have a Boolean type. C uses int any non zero value is interpreted as TRUE. zero is interpreted as FALSE.

16 Department of Electronic & Electrical Engineering Relational (comparator) operators e.g. 5.0 > 4.0  1 for TRUE 6.0==7.0  0 for FALSE int result

17 Department of Electronic & Electrical Engineering Logical Operators (combining boolean values)

18 Department of Electronic & Electrical Engineering Logical examples. int x=5,y=6,z=7; /* we can declare and initialize at the same time ! */ int result; /* C does not have a boolean type. We use int */ result = x>y; /* result should be 0 which means FALSE */ result = x<y; /* result should be 1 which means TRUE */ result = (x > y) && ( ! z < x) ; /* Hmmm this is a bit tricky */ result = (x > y) && ( ! (z < x)) ; /* I think this is what I meant to write */

19 Department of Electronic & Electrical Engineering Operator Precedence Higher precedence at the top of the table

20 Department of Electronic & Electrical Engineering Associativity Consider 8- 4+2 If associativity is left to right this is (8-4)+2  6 right to left 8-(4+2)  2

21 Department of Electronic & Electrical Engineering Bitwise operators int a=5; /* 101 */ int b=3; /* 011 */ int c; c= a & b ; /* bitwise AND c = 001 */ c= a | b; /* bitwise OR c = 111 */ c= a^b; /* XOR c = 110 */ c= ~a; /* complement c = 1....11010 */ c= a << 2 /* left shift by 2 c = 10100 */ c= a >> 2 /* right shift 2 c = 001 */

22 Department of Electronic & Electrical Engineering char x=2; /* 0000010 */ char y=3; /* 0000011 */ /* logically AND x and y both are TRUE so z is 1 */ char z=x && y; /* bitwise AND x and y result is 00000010 = 2 ! */ char w=x & y; printf(" %d %d \n",z,w); Example bit wise operations


Download ppt "Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators."

Similar presentations


Ads by Google