Chapter 5

1: Assume all variables are of type int. Find the value of each of the following variables:
  1. x = (2 + 3) * 6;

  2. x = (12 + 6)/2*3;

  3. y = x = (2 + 3)/4;

  4. y = 3 + 2*(x = 7/2);

A1:
  1. 30

  2. 27 (not 3). (12 + 6)/(2*3) would give 3.

  3. x = 1, y = 1 (integer division)

  4. x = 3 (integer division) and y = 9

2: Assume all variables are of type int. Find the value of each of the following variables:
  1. x = (int) 3.8 + 3.3;

  2. x = (2 + 3) * 10.5;

  3. x = 3 / 5 * 22.0;

  4. x = 22.0 * 3 / 5;

A2:
  1. 6 (reduces to 3 + 3.3)

  2. 52

  3. 0 (reduces to 0 * 22.0)

  4. 13 (reduces to 66.0 / 5 or 13.2, and then assigned to int)

3: You suspect that there are some errors in the next program. Can you find them?
 int main(void) { int i = 1, float n; printf("Watch out! Here come a bunch of fractions!\n"); while (i < 30) n ...

Get C Primer Plus, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.