220 likes | 317 Vues
Homework. Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?. Type Conversion. /* atoi: convert character string of digits to int (base 10) */ int atoi(char s[ ]) /* name based on “ a scii to i nteger” */ { int i, n; n = 0;
E N D
Homework • Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. • Questions?
Type Conversion /* atoi: convert character string of digits to int (base 10) */ int atoi(char s[ ]) /* name based on “ascii tointeger” */ { int i, n; n = 0; for (i=0; s[i] >= '0' && s[i] <= '9'; ++i) /* "is a digit" */ n = 10*n + (s[i] - '0'); /* s[i]-'0' is char add to 10*n is int */ return n; }
Type Conversion /* itoa: convert int n to characters in base 10 in array s */ void itoa (int n, char s[ ]) { int i, sign; if ((sign = n) < 0) /* record sign */ n = -n; /* make n positive */ i = 0;
Type Conversion /* generate digits in reverse order */ do { /* new loop type */ s[i++] = n % 10 + '0'; /* generate next digit */ /* what conversion takes place here? */ } while(( n /= 10) > 0); /* delete digit from end of int */ if (sign < 0) s[i++] = '-'; s[i] = '\0'; reverse (s); /* reverse digit string */ }
Convert Upper Case to Lower int lower(int c) /* this is function tolower(int c) in C library. To use it, you need to #include <ctype.h> (see K&R App. B2) */ { if (c >= 'A' && c <= 'Z') return c – ‘A’ + ‘a’; /* c - 'A' is the offset number... */ /* c -'A'+'a' is the corresponding lower case letter*/ else return c; }
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 */ main ( ) . . . func ( ) /* 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 */ }
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
Type Conversions -Casts K&R Sect 2.7 char c1, c2 = 15; int j = 2379; /* 2379 = 0x 0000094b */ 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 )
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 )
Type Conversion Rules • Precise conversion rules in Section 6 of Appendix A • If either operand is long double, convert the other to long double • Otherwise if either operand is double, convert the other to double • Otherwise if either operand is float, convert the other to float • Otherwise, convert char and short to int • Then if either operand is long, convert the other to long
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]); 3 3 4
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
Increment / Decrement Operators • Hard to predict sometimes, you need to experiment n = 3; n = (n++)*(n++); • Do we get 3*3+1+1 = 11? It might be different on different machines, so don't do this! • With "gcc" we get result 9! • Another example: printf ( "%d %d\n", ++n, n); ??? Unclear what is printed out. Which expression is evaluated first?
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
Difference between the bit wise operator & and logical operator && • Example … int a =0xf, b= 0xab, c, d; c = a & b; d = a && b; c = 0xb d = 0x1 because it is true True True
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
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 complement)
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)); } 7 6 5 4 3 2 1 0 Position p n Bits
Assignment Operators Section 2.10. • expr1 op = expr2 means expr1 = (expr1) op (expr2) • 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 = 0x1, 0x18 | 0x02 = 0x1a)
K&R Exercise 2-9 • Find how many 1’s in a binary number? • Use the expression to set the rightmost 1-bit to a 0 x &= (x-1) • Repeat until x is all zeros char x = 0xa4 has bits 10100100 (three 1-bits) x = 10100100 x = 10100000 & x-1 10100011 & x-1 10011111 10100000 10000000 x = 10000000 & x-1 01111111 00000000 Bits all counted • What about char x = 0?
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;
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’ : ‘ ’);