Previous: 5.6 Summary
Up: 5 Numeric Data Types and Expression Evaluation
Next: 5.8 Problems
Previous Page: 5.6 Summary
Next Page: 5.8 Problems

5.7 Exercises

  1. If x is 100 and z is 200, what is the output of the following:
    if (z = x)
              printf("z = %d, x = %d\n", z, x);

  2. With the following declarations:
    int a = 10, b = 15, c = 25;
         float x = 15;
         double y = 30;
         long int m = 25L;
    What are the values and types of the following expressions:
    a + b / c * x;
         a + b / x * c;
         a + b / y * c;
         a + b / m * c;
    

    x + a / b; x + (int) a / b;

  3. Evaluate the expressions following the declarations:
    int x, y, z;
         float u, v, w;
    

    x = 10; y= 20; z = 30;

    x = z / y + y; x = x / y / z; x = x % y % z

  4. Evaluate the expressions:
    int x, y, z;
         float u, v, w;
         x = 10; y= 20; z = 30;
         u = 5.0; v = 10.0; w = 30.0;
    

    x = w / y + y; u = z / y + y; u = w / y + y; u = x / y / w + u / v;

  5. What is the output of the following program?
    #define PRHAPS
    #define TWICEZ z + z
    

    main() { int w, x, y, z; float a, b, c; w = 16; x = 5; y = 15; z = 8; a = 1.0; b = 2.0; c = 4.0;

    #ifdef PRHAPS x = 15; y = 5; #endif

    printf("(a). %d %d\n", x, y); printf("(b). %d\n", TWICEZ * 2); printf("(c). %f %f\n", w / z * a + c, z / w * b + c); printf("(d). %d\n", z % y % x); }

  6. What will be the output in the following cases:
    1. #define SWAP(x, y)  int temp; temp = x; x = y; y = temp
      main()
      {    int x1 = 10, x2 = 20;
      

      SWAP(x1, x2); printf("x1 = %d, x2 = %d\n", x1, x2); }

    2. #define SWAP(x, y)  {int temp; temp = x; x = y; y = temp;}
      main()
      {    int x1 = 10, x2 = 20;
      

      SWAP(x1, x2); printf("x1 = %d, x2 = %d\n", x1, x2); }

    3. #define SWAP(x, y)  int temp; temp = x; x = y; y = temp
      main()
      {    int x1 = 10, x2 = 20;
      

      printf("Swapping Values\n"); SWAP(x1, x2); printf("x1 = %d, x2 = %d\n", x1, x2); }

  7. Write a while and a do...while loop to read and echo long integers until end of file. Allow for the possibility that the first input is an end of file.

  8. Write a for loop to print out squares of integers in the sequence 5, 10, 15, 20, 25, etc. until 100.

  9. Given the following declarations:
    int x = 100, y;
    What are the values of x and y after each of the following expressions is evaluated (the expressions are evaluated in sequence)?
    y = x++;
         y = ++x;
         y = --x;
         y = x--;

  10. What are the values of the following expressions considered sequentially:
    x = 100; y = 200;
         y = y++ - ++x;
         y = ++y - x++;
         y = ++y * 2;
         y = 2 * x++;

  11. Evaluate the following:
    x = 100; y = 200;
         y += 2 * x++;
         y -= 2 * --x;
         y += x;

  12. Evaluate the following:
    x = 100; y = 200; z = 25;
         z = y > x ? x : y;
         z = (z >= x && z >= y) ? z - x * y : z + x * y;

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:40:40 HST 1994