# 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 `~/csc161/labs/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, Revisited

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: 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);
```

### Exercise 4: Enumerated Types

As you may have learned from section 2.3, C supports enumerated types, types in which you can name the values. To declare an enumerated type, you write,

```enum typename { VAL0, VAL1, ... }
```

As I've tried to note, custom in C is to make the values all uppercase.

To declare a variable of that type, you write

```enum typename variable;
```

For example, here is a complete (but pointless) program that defines a type for five popular Greek letters.

```#include <stdio.h>

enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON };

int
main ()
{
enum greek g;
g = ALPHA;
return 0;
} // main
```

a. Extend this program to print out the five values. Use a pattern of `"%d"` to print one of the values.

b. Determine what happens if you try to use `"%s"` to print the values.

### Exercise 5: Fun with Characters

For each of the following expressions, predict the output and then experimentally check your answer. (You will need to embed the code in a program.)

Note that you can check the ASCII table with `man ascii`.

• `printf ("abc\n");`
• `printf ("ab\bc\n");`
• `printf ("ab\fc\n");`
• `printf ("ab\nc\n");`
• `printf ("ab\rc\n");`
• `printf ("ab\tc\n");`
• `printf ("ab\vc\n");`
• `printf ("ab\\c\n");`
• `printf ("ab\?c\n");`
• `printf ("ab\'c\n");`
• `printf ("ab\"c\n");`
• `printf ("ab\050c\n");`
• `printf ("ab\x5e\n");`

## For Those with Extra Time

### Extra 1: ASCII

Write a program that prints out a simple ASCII table of the decimal number of each character from 32 to 127.

## History

Sunday, 5 September 2010 [Samuel A. Rebelsky]

• Created.

Friday, 10 September 2010 [Samuel A. Rebelsky]

Tuesday, 1 February 2011 [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 Tue Feb 1 20:38:01 2011.
The source to the document was last modified on Tue Feb 1 20:37:37 2011.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/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