1 exam / homework exam 1 in class 10 –open book / open notes hw3 due next class hw4 will be...

17
1 Exam / Homework Exam 1 in Class 10 Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Read K&R through Chapter 4.2. • Questions?

Upload: aubrey-hardy

Post on 13-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

1

Exam / Homework

• Exam 1 in Class 10– Open book / open notes

• HW3 due next class• HW4 will be on-line soon.• Finishing Chapter 2 of K&R. We will go through

Chapter 3 very quickly. Not a lot is new.• Read K&R through Chapter 4.2.• Questions?

Page 2: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

2

Convert Upper Case to Lower

int lower(int c) /* this is function tolower(int c) if #include <ctype.h> (see K&R, App. B2) */

{ if (c >= 'A' && c <= 'Z') return c – ‘A’ + ‘a’; /* c - 'A' is letter number... */ /* ...where ‘A’ = 0, so c -'A' + 'a' is corresponding lower case letter */ else return c;}

Page 3: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

3

Declarations and Initialization Section 2.4.

int x, a[20]; /* external vars, initialized to zero */int w = 37, v[3] = {1, 2, 3}; /* happens once */int main (void){ . . . /* contains frequent calls to func */}void func (void) /* entered 1000 times per second */{

int y, b[20]; /* automatic -- contains junk on entry */int s = 37, t[3] = {1, 2, 3}; /* happens on each entry */

}

Page 4: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

4

C language Characteristics

• C language never does a possibly significant amount of work without this being specified by the programmer

• Initialization is not done unless programmer codes it in a way that causes the initialization to be done

Page 5: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

5

Casts K&R Sect 2.7char c1, c2 = 15;

int j = 2379;

float g = 12.1;

printf ("%d\n", c2 + j); /* what type, what printed ? */

( int, 15 + 2379 = 2394 )

c1 = j; /* what type, value of c1? */

( char, 2379 % 256 = 75 )

printf ("%d\n", c1 + c2); /* what type, value printed? */

( char, 90 )

Page 6: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

6

Casts K&R Sect 2.7

printf ("%d\n", (char) j + c2); /* value? */ ( 90 )

printf ("%9.1f\n", j + g); /* type, value? */ ( float, 2379. + 12.1 = 2391.1 )printf ("%d\n", j + (int) g); /* type, value? */ ( int, 2379 + 12 = 2391 )

Page 7: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

7

Increment / Decrement Operators Section 2.8.

• Both ++n and n++ have effect of n = n + 1; • Both --n and n-- have effect of n = n – 1;• With ++n or --n in an expression

– n is incremented/decremented BEFORE being used

• With n++ or n-- in an expression– value is used THEN n is incremented/decremented

int arr[4] = {1, 2, 3, 4}, n = 2;printf("%d\n", arr[n++]); /* values printed? */printf("%d\n", arr[--n]);printf("%d\n", arr[++n]);

Page 8: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

8

Increment / Decrement Operators• Precedence, pg. 53, specifies the binding order,

not the temporal order. Consider two statements:n = 5;m = n-- + 7;

• In second statement, binding is: m = ((n--) + 7);• But value of n-- is evaluated last• Uses value 5 for n in evaluation of the expression • The value of n is not decremented to 4 until the

ENTIRE EXPRESSION has been evaluated (in this case, the right side of assignment statement)

• Temporal order of execution is not specified by binding order of operators in precedence table

Page 9: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

9

Increment / Decrement Operators

• In fact (experiment), hard to predict sometimesint n = 3;

n = (n++)*(n++); /* bad practice */• Do we get 3*3+1+1 = 11? It might be different on

different machines and compilers, so don't do this!• When we did this with “gcc” last semester, we got 11!• With “gcc” on our systems now, we get 9!  • printf ( "%d %d\n", n, n++); /* unclear what is printed */• Which expression is evaluated first?• Second expression is evaluated first, so we get 10, 9!

Page 10: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

10

Bit-wise OperatorsSection 2.9

• We’ve already covered these, but they can be difficult when using them for first time

• Bit-wise Operators& bit-wise AND

| bit-wise inclusive OR

^ bit-wise exclusive OR

~ one’s complement

<< left shift

>> right shift

Page 11: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

11

Bit-wise Operators

• Masking is the term used for selectively setting some of the bits of a variable to zero& is used to turn off bits where “mask” bit is zero

n = n & 0xff resets all except 8 LSBs to zero

| is used to turn on bits where “mask” bit is one

N = n | 0xff sets all LSBs to one

Page 12: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

12

Bit-wise Operators

• Other tricks with bit-wise operators^ can be used to zero any value

n = n ^ n sets value of n to zero

~ can be used to get the negative of any value

n = ~n + 1 sets value of n to –n (2’s compliment)

Page 13: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

13

Get a Group of Bits• Get a bit field of n from position p in value

(put in least significant bits with zeros above)

unsigned getbits(unsigned x, int p, int n){

return (x >> (p+1-n)) & ~(~0 << n);}

Position p

01234567

n Bits

Page 14: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

14

Assignment OperatorsSection 2.10.

• i += 3 means i = i + 3;– We can also use other operators

int i = 3;

i += 3; /* value now? */ ( 6)

i <<= 2; /* value now? */ (24)

i |= 0x02; /* value now? */ (24 = 0x18. 0x18 | 0x02 = 0x1a)

Page 15: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

15

K&R Exercise 2-9• x &= (x-1) sets the rightmost 1-bit to a 0• char x = 0xa4 has bits 10100100

x = 10100100& x-1 10100011x = 10100000 & x-1 10011111x = 10000000

& x-1 01111111

x = 00000000 Bits all counted

• What about char x = 0?

Page 16: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

16

Conditional ExpressionsSection 2.11

• Example: To implement z = max(a, b)

if (a > b)

z = a;

else

z = b; • As long as both sides of if statement set only one

variable value, it can be written:

z = (a > b)? a: b;

Page 17: 1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through

17

Conditional Expressions

• Can also nest conditionals:

z = (a > b)? a: ((x < y)? b: 0);

• Can include conditions inside an expression (e.g. to print EOL every 10th value or at end):

for (i = 0; i < n; i++)

printf(“%6d%c”, a[i],

(i%10 == 9 || i == n-1) ? ‘\n’ : ‘ ’);