CS61C Summer 2012 Lab 1 - C Intro, Number Representation

Goals

Reading

Accounts

You need to have an EECS instructional account (cs61c-*) in order to do your lab work. Most importantly, you will need to log in to this account in order to enroll in the class. Don't forget to change your password (passwd) once you have logged in.

Setup

Copy the contents of ~cs61c/labs/01 to a suitable location in your home directory.

$ mkdir ~/lab01
$ cp -R ~cs61c/labs/01/ ~/lab01

Exercises

Exercise 1: Simple C program

Fill in the blank in the following C program, also in output0.c, so that its output is a line containing 0. Don't change anything else in the program.

#include <stdio.h>
int main(void) {
	int n;
	n = ___;
	printf("%c\n",n);
	return 0;
}

To verify your solution, compile it and run the resulting binary:

$ gcc -o output0 output0.c
$ ./output0
0

Checkoff

Exercise 2: C control flow

Look at the code contained in eccentric.c. In it are four different examples of basic C control flow. Compile and run the program to see what it does. Modifying only the initialization values of variables, make the program produce the following output:

$ gcc -o eccentric eccentric.c
$ ./eccentric
Berkeley eccentrics:
====================
Happy Happy Happy
Yoshua

Go BEARS!

Checkoff

Exercise 3: The Biggest Integer

In class we discussed number representation. In particular, we discussed unsigned integers and two's complement, the almost ubiquitous format for signed integers. Look at biggestInt.c. You may wish to read through the comments but at this point it is not critical that you understand exactly how the program works. Basically, it is a C program that will tell you some useful information about certain C data types. It does this by exploiting the fact that C does not check for overflow and wrap around conditions. Compile and run the program and answer the following questions:

  1. The first piece of information output by the program tells you the value of the most significant bit (MSB) of an unsigned int on your terminal. Based on this information, how many bits does the C data type unsigned int have?
  2. The second piece of information tells you the value of the largest positive signed long on your terminal. Base on this information, how many bits does the C data type long have?
  3. The third piece of information tells you the value of the most negative signed int on your machine. Based on this information, do the unsigned int and signed int have the same number of bits?
  4. Two's complement number representations have one more negative number than they do positive numbers. That is, the most negative number does not have a positive counter-part. The final piece of information printed is the signed value of what you get when you try to negate the most negative value. Can you explain why this happens?

Checkoff