CSC 115.005/006 Sonoma State University Spring 2022
Scribbler 2
CSC 115.005/006:
Programming I
Scribbler 2
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College


Course Home References Course Details: Syllabus, Schedule, Deadlines, Topic organization MyroC Documentation Project Scope/
Acknowledgments

Notes:

Laboratory Exercise: Types and Variables

This lab explores types, variables, and printing in C.


Work Started in Class

  1. Include the following code segment in a C program, compile the program, and observe the results:

      int a = 5;
      int b = 6;
      int c = a/b;
      int d = b/a;
      int e = (a + b) / 10;
      int f = (a + b) % 10;
      double x = (a + b) / 10;
      double y = (a + b) / 10.0;
      printf ("a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, x=%lf, y=%lf\n",
              a, b, c, d, e, f, x, y);
    
    1. Review what is printed and explain each result.
    2. What happens if one changes the computation for variables f and y to (a + b) % 10.0; ? Explain why the compiler produces this result.

Observation: You may recall there is a float type, that was not involved in Step 1. the double type is often preferred to the float type, because a double is stored more accurately than a float. Also, constants, such as 3.14 are almost always stored as double types in C. An exception arises when memory is scarce, and one wants to use as little space as possible within a computer's memory. With the size of main memory in today's computer, however, size often is not a consideration, and the double type typically is used rather than float type.

Some reminders regarding computations with int and double types

C provides four common arithmetic operations for numbers of type int and double, with an extra capability for the int data type.

Basic rules:


Operator Precedence

  1. Copy the following code in a program. What do you think the result will be? Test this out by printing the value.

        	int v;
    	v = 5 + 4 * 3 + 1
    
  2. Now copy this code in the same program. Think about your expected result and then see what the program prints.

    	int w;
    	w = (5 + 4) * (3 + 1)
    	
    
  3. Insert the following code segment into a C program, compile the program and run it.

    int ans1 = 1 + 2 * 3 + 4;
    int ans2 = 1 - 2 -3 - 4 + 5;
    int ans3 = 2 * 3 - 4 * 6 + 5 * 5;
    printf ("ans1 = %d; ans2 = %d; ans3 = %d\n", ans1, ans2, ans3);
    
    1. Review what is printed and explain each result.
    2. Add parentheses to each computation for ans1, ans2 and ans3, to clarify which operations are done in what order. Then run the program again to check that the computation with your parentheses match the original output.


Assignment and Casting

  1. Include the following code segment in a C program, compile the program, and observe the results:

      int k = 5.0;
      int m = 7.7;
      double n = 5;
      int p = k/m;
      double q = k/m;
      printf ("k = %d, m=%d, n=%lf, p=%d, q=%lf\n", k, m, n, p, q);
      double r = (double) (k/m);
      double s = (double) k / m;
      double t = k / (double) m;
      double u = (double) k / (double) m;
      printf ("r=%lf, s=%lf, t=%lf, u=%lf\n", r, s, t, u);
      k *= 3;
      m /= 5;
      n /= 2.1;
      p += 2.3;
      q += 2.3;
      printf ("k = %d, m=%d, n=%lf, p=%d, q=%lf\n", k, m, n, p, q);
    
    1. Review what is printed.
    2. For each result printed, explain whether the division is done with int values or double values and which, if any, numbers are converted to double values when.

Observation: Sometimes there can be a danger in casting. Types have sizes. If you try to put a number that is bigger than the size of the type you are casting it to, you may get undesired results.


Incrementing and Decrementing Numbers

  1. Examine the output of the following code, in which variables are incremented as the only part of an expression.

       int a = 1;
       int b = 7;
       printf ("a = %d, b = %d\n", a, b);
       a++;
       ++b;
       printf ("a = %d, b = %d\n", a, b);
    

    In this code the ++ operation is the only part of the statement. Note what results are obtained.

  2. Now consider the following example that combines the increment operators with other activities (e.g., assignment)

      int a,b,c;
      a = 0;
      b = a++;
      c = ++a;
      printf(" a = %d b = %d c = %d\n", a,b,c);
      a = 0;
      b = ++a;
      c = a++;
      printf(" a = %d b = %d c = %d\n", a,b,c);	
    

    Examine the output and explain the result. For example, explain any difference observed between the two increment operators in this context.

  3. The next code segment combines the increment operators within a print and within arithmetic expressions. Examine the output to determine whether or not there is a difference between the two increment operators in this context.

      int r, s;
      r = 5;
      s = 7;
      printf(" r = %d s = %d\n", ++r, s++);
      printf(" r = %d s = %d\n", r, s);	
      int t = r++ + s++;
      printf(" r = %d s = %d, t = %d\n", r, s, t);	
      int u = ++r + ++s;
      printf(" r = %d s = %d, u = %d\n", r, s, u);	
    
    

  1. In addition to the increment operators, ++a and a++, C provides pre- and post-decrement, --a and a--, that subtract one from the variable.

    Repeat Step 7, replacing ++ by -- throughout. Again, examine the output and explain what is printed.

Observation: When combined with other operations, the increment operations ++a (pre-increment) and a++ (post-increment) cause different values to be used in processing. This observation yields the following programming conclusion and suggestion:


Homework

Printing

  1. Copy the printf-formatting.c to your account, and then compile and run it.

    1. For each printf statement, explain why the output appears as printed.
    2. For integers, change the width 11 (e.g., %11hd to a width 15. Explain what happens and why. (Then return the width specifications to 11.)
    3. Change the value of in to 54321, and change the width field for printing the integer in to %3. Explain the output produced. (Then return both in and the width field to their original values.)
    4. For the integer printing, change %11.11d to %11.0d and then to %11.6d. In each case, explain what happens. (Then change this part of the program back.)
    5. Change the double specification in the last printf statement to %11lf, %11.0lf, and %11.21lf. In each case, describe what happens and explain why.
  2. The first program for this course quarts.c computed the number of liters from a number of quarts with the formula:

        liters = quarts / 1.056710 ;      /* arithmetic, assignment */
    

    Write a program that computes and prints the number of liters for quarts being 2, 4, 6, 8, 10, and 12. The results should be presented in a table, in which the width of the quarts column is 7 and the width of the liters column is 12 characters. For each liter value, print the number of liters to 5 decimal places.

    For example, the start of the table might be:

    1234567123456789012
      Quarts     Liters
           2    1.89267
           4     ...
    


created 21 July 2011 by David Cowden
last revised 21 July 2011 by David Cowden
minor editing 24 August 2011 by Henry M. Walker
additional editing 3 September 2011 by Henry M. Walker
further revision 26 September 2011 by Henry M. Walker
revision (representation, conversion) 20 July 2012 by Henry M. Walker
revision (incrementing, limits.h) 1 February 2013 by Henry M. Walker
revision (revised chars discussion, omitted unsigned types) 10 September 2013 by Henry M. Walker
reorganized 21 January 2014 by Henry M. Walker
readings added 19 September 2014 by Henry M. Walker
readings readings, material on char type removed, and material reformatted 14 July 2016 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu .