# Exam / Homework ```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&amp;R. We will go through
Chapter 3 very quickly. Not a lot is new.
• Read K&amp;R through Chapter 4.2.
• Questions?
1
Convert Upper Case to Lower
int lower(int c)
/* this is function tolower(int c)
if #include &lt;ctype.h&gt; (see K&amp;R, App. B2) */
{
if (c &gt;= 'A' &amp;&amp; c &lt;= '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;
/* external vars, initialized to zero */
int w = 37, v = {1, 2, 3}; /* happens once */
int main (void)
{
. . .
/* contains frequent calls to func */
}
void func (void)
/* entered 1000 times per second */
{
int y, b; /* automatic -- contains junk on entry */
int s = 37, t = {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&amp;R Sect 2.7
char c1, c2 = 15;
int j = 2379;
float g = 12.1;
printf (&quot;%d\n&quot;, c2 + j); /* what type, what printed ? */
( int, 15 + 2379 = 2394 )
c1 = j;
/* what type, value of c1? */
( char, 2379 % 256 = 75 )
printf (&quot;%d\n&quot;, c1 + c2); /* what type, value printed? */
( char, 90 )
5
Casts K&amp;R Sect 2.7
printf (&quot;%d\n&quot;, (char) j + c2); /* value? */
( 90 )
printf (&quot;%9.1f\n&quot;, j + g);
/* type, value? */
( float, 2379. + 12.1 = 2391.1 )
printf (&quot;%d\n&quot;, 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 = {1, 2, 3, 4}, n = 2;
printf(&quot;%d\n&quot;, arr[n++]); /* values printed? */
printf(&quot;%d\n&quot;, arr[--n]);
printf(&quot;%d\n&quot;, 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 ( &quot;%d %d\n&quot;, 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
&amp;
|
^
~
&lt;&lt;
&gt;&gt;
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
&amp; is used to turn off bits where “mask” bit is zero
n = n &amp; 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 &gt;&gt; (p+1-n)) &amp; ~(~0 &lt;&lt; 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 &lt;&lt;= 2;
i |= 0x02;
/* value now? */ ( 6)
/* value now? */ (24)
/* value now? */ (24 = 0x18.
0x18 | 0x02 = 0x1a)
14
K&amp;R Exercise 2-9
• x &amp;= (x-1) sets the rightmost 1-bit to a 0
• char x = 0xa4 has bits 10100100
x=
&amp; x-1
x=
&amp; x-1
x =
&amp; 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 &gt; 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 &gt; b)? a: b;
16
Conditional Expressions
• Can also nest conditionals:
z = (a &gt; b)? a: ((x &lt; y)? b: 0);
• Can include conditions inside an expression
(e.g. to print EOL every 10th value or at end):
for (i = 0; i &lt; n; i++)
printf(“%6d%c”, a[i],
(i%10 == 9 || i == n-1) ? ‘\n’ : ‘ ’);
17
```