# 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

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/Types/`, 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: Compiling Basics

a. Create your own copy of the Hello World program, storing it in the file `hello.c`.

b. Confirm that you can compile the program use `make`.

c. Confirm that you can check the program using `splint`.

### Exercise 2: Limits

Using `<limits.h>` (documented on page 257 of K&R), find the various interesting limits on integer-like types. A typical line of your program will look something like the following.

```  printf ("CHAR_BIT  %12d\n", CHAR_BIT);
```

### Exercise 3: Coercion

Determine experimentally, what happens when

• You assign a character to an integer.
• You assign an integer to a character.
• When the integer is in the range 0..127
• When the integer is negative.
• When the integer is greater than 256.
• You assign a long to a short.
• Anything else you think might be interesting.

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 4: 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`.

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

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

### Exercise 5: Exceeding Limits

Predict the results of each of the following and then check your answers experimentally. (Note that we will explore why you get these results when we talk about binary representation.)

Assume that we've declared `i` as an integer variable.

a.

```  i = INT_MIN;
--i;
printf ("i = %d\n", i);
```

b.

```  i = INT_MAX;
++i;
printf ("i = %d\n", i);
```

c.

```  i = -31;
i = abs (i);
printf ("i = %d\n", i);
```

d.

```  i = INT_MIN;
i = abs (i);
printf ("i = %d\n", i);
```

e.

```  i = INT_MAX / 4;
printf ("i = %d\n", i);
```

f.

```  i = INT_MAX / 4;
i = i*5;
printf ("i = %d\n", i);
```

## History

Sunday, 5 September 2010 [Samuel A. Rebelsky]

• Created.

Friday, 10 September 2010 [Samuel A. Rebelsky]

• Fixed bug.

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 Sep 10 11:45:15 2010.
The source to the document was last modified on Fri Sep 10 11:45:12 2010.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/types-lab.html`.
A PDF version of this document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/type-lab.pdf`

Samuel A. Rebelsky, rebelsky@grinnell.edu