CS Behind the Curtain (CS195 2003S)

Laboratory: An Introduction to Basic Types in C

Summary: This lab reviews some of the basic types in C.

Citation: Many problems in this lab are closely based on problems from chapter 2 of

Kernighan, Brian W. and Ritchie, Dennis M. (1988). The C Programming Language, Second Edition: ANSI C. Englewood Cliffs, NJ: Prentice-Hall. ISBN: 0-13-1110362-8.

Contents:

Exercises

Exercise 0: Preparation

Create a new directory for this laboratory. Initially, this directory should be empty.

Exercise 1: Ranges of Integer Values

Determine the minimum and maximum valid values for each of the following types. Note that you will need to include limits.h and refer to appropriate constants (such as UCHAR_MAX, and SHRT_MIN).

Exercise 2: Promotion and Demotion

Write a program that shows the effects of promotion, demotion, and other conversions between the various integral types.

Exercise 3: Very Long Integers

Determine (using whatever technique you find best) whether our compiler supports the signed long long int type introduced in C99. If our compiler supports them, determine their range.

I would recommend that as part of your exploration, you type info gcc and man gcc at the command line.

Exercise 4: Ranges of Reals

By looking at /usr/lib/gcc-lib/i386-redhat-linux/2.96/include/float.h, determine the valid ranges of floats, doubles, and long doubles. You might also want to take notes on other interesting aspects of these values.

Exercise 5: Sizes of Types

By reflecting on the previous exercises, by writing a program that uses sizeof, or both, determine the sizes of the various numeric types (in bits or 8-bit bytes).

Exercise 6: A Simple Enumerated Type

a. Create a program that declares an enumerated type, day, that consists of the days of the week.

b. Add a main procedure that declares a variable, today, of type day and assigns the value MONDAY to it. (I am assuming that you have made MONDAY one of the values in the enumerated type.) Your main should also print out the value of today.

c. What do you expect to happen when you compile your program?

d. Confirm your answer experimentally. Correct any errors revealed in the compilation process.

e. Extend your program to assign the value 8 to today.

f. What do you expect to happen when you splint your program?

g. Verify your results experimentally.

h. What do you expect to happen when you compile your program?

i. Verify your results experimentally.

j. Reflect on what you've learned from this exercise.

Exercise 7: Multiple Enumerated Types

Write a program or programs to answer each of the following questions:

a. What happens if you try to reuse the name of a value within a single enumerated type?

b. What happens if you try to reuse the name of a value across enumerated types?

c. What does the compiler report if you try to assign a value from one enumerated type to a variable of another enumerated type?

d. What does splint report if you try to assign a value from one enumerated type to a variable of another enumerated type?

e. What happens if you try to give two values the same name in a single enumerated type?

Exercise 8: Increment

a. What do you expect the value of a to be after the following series of commands?

b = 5;
a = ++b - ++b;

b. What do you expect the value of a to be after the following series of commands?

b = 5;
a = b++ - b++;

c. Verify your answers experimentally using both our compiler and splint. Be prepared to discuss your expected and final answers.

Notes

Sorry, no notes yet.

 

History

Monday, 10 February 2003 [Samuel A. Rebelsky]

 

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 Fri May 2 14:19:52 2003.
The source to the document was last modified on Mon Feb 10 16:07:46 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS195/2003S/Labs/intro-types.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu