CSC161 2011S Imperative Problem Solving

Laboratory: Types in C

Summary: We explore basics of types in C.

Prerequisites: Familiarity with basic Linux commands. Ability to use an editor. Ability to compile C files.

Preparation

a. Log in to your MathLAN workstation. (Of course, you've probably already done that if you're reading this laboratory.)

b. Open a terminal window into which you can type commands.

c. Create a directory for this lab. (I'd suggest something like ~/Courses/CSC161/MoreTypes/, but it's up to you.)

d. In that directory, create a file called Makefile that contains the following line.

CFLAGS=-Wall

Exercises

Exercise 1: Coercion

Determine experimentally, what happens when

For example, to explore the assignment of a long to a short, you might write

  long l;
  short s;
  ...
  l = SHRT_MAX + 5;
  s = l;

  printf ("Long: %ld\n", l);
  printf ("Short: %d\n", s);

Exercise 2: Enumerated Types

Consider the following code:

enum values { ALPHA, BETA, GAMMA };

int
main ()
{
  int i;
  enum values v;

  i = BETA;
  v = 23;
}

a. What do you expect the compiler to do if you attempt to compile this code? (Assume that you're using -Wall.

b. Check your answer experimentally.

c. What do you expect splint to report for this code?

d. Check your answer experimentlaly.

e. What value do you expect i to have when you print it out?

f. Check your answer experimentally.

Exercise 3: Incrementing

You've learned that the ++ operation increments its parameter. You've also learned that you can place it before or after a variable. What's the difference? Let's check.

a. Predict the output of the following chunk of code.

  int x = 5;
  printf ("x = %d\n", x);
  printf ("x++ = %d\n", x++);
  printf ("x = %d\n", x);
  printf ("++x = %d\n", ++x);
  printf ("x = %d\n", x);

b. Check your answer experimentally.

c. Predict the output of the following chunk of code.

  int y = 6;
  printf ("%d %d %d %d\n", y++, y++, y++, y++);

d. Check your answer experimentally.

e. Discuss the meaning of your output.

 

History

Tuesday, 1 February 2011 [Samuel A. Rebelsky]

Monday, 7 February 2011 [Samuel A. Rebelsky]

  • Added an increment example.

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Feb 7 10:48:21 2011.
The source to the document was last modified on Mon Feb 7 10:46:14 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/more-types-lab.html.
A PDF version of this document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/type-lab.pdf

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright © 2010 Samuel A. Rebelsky. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.