advertisement

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? 1 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; } 2 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 */ 3 } 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 4 Casts K&R Sect 2.7 char 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 ) 5 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 ) 6 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]); 7 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 8 Increment / Decrement Operators • In fact (experiment), hard to predict sometimes int 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!9 Bit-wise Operators Section 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 10 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 11 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) 12 Get a Group of Bits • Get a bit field of n from position p in value (put in least significant bits with zeros above) 7 6 5 4 3 2 1 0 Position p n Bits unsigned getbits(unsigned x, int p, int n) { return (x >> (p+1-n)) & ~(~0 << n); } 13 Assignment Operators Section 2.10. • i += 3 means i = i + 3; – We can also use other operators int i = 3; i += 3; i <<= 2; i |= 0x02; /* value now? */ ( 6) /* value now? */ (24) /* value now? */ (24 = 0x18. 0x18 | 0x02 = 0x1a) 14 K&R Exercise 2-9 • x &= (x-1) sets the rightmost 1-bit to a 0 • char x = 0xa4 has bits 10100100 x= & x-1 x= & x-1 x = & x-1 x = 10100100 10100011 10100000 10011111 10000000 01111111 00000000 Bits all counted • What about char x = 0? 15 Conditional Expressions Section 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; 16 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’ : ‘ ’); 17