Previous: 3.7 Summary
Up: 3 Designing Programs Top Down
Next: 3.9 Problems
Previous Page: 3.7 Summary
Next Page: 3.9 Problems

3.8 Exercises

  1. What will the following code do?
    #define SQ(x) x * x;
         printf("%d\n", SQ(3));
  2. What will the following code do?
    #define SQ(x) x * x;
         printf("%d\n", SQ(2+3));
  3. What will be the output of the following code:
    #define DEBUG 0
         #define TWICEZ  z + z
    

    main() { int z = 5;

    #ifdef DEBUG printf("%d\n", TWICEZ * 2); #endif }

  4. Check the following program for errors, if any, and use a manual trace to verify the program averages two numbers.
    #include <stdio.h>
         main()
         {    float x, y, average;
    

    printf("Type two numbers: "); scanf("%f %f", &x, &y); calc_avg(x, y); printf("Average of %f and %f is %f\n", x, y, average); }

    calc_avg(float a, float b) { return a + b / 2; }

  5. Check the following program for errors, if any, and manually trace its execution.
    main()
         {    float x, y, average;
    

    printf("Type numbers\n"); scanf("%f", &x); while (x != EOF) { printf("Number read = %f\n", x); scanf("%f", &x); } }

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