CS61C Summer 2011

Lab 1: C Intro, Number Representation





You need to have an EECS instructional account (cs61c-*) in order to do your lab work. More 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.



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

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

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 = ___;
   return 0;

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

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

Checkoff: Explain the change you made to output0.c.

Exercise 2: C control flow

Look at the code contained in eccentric.c. We see four different examples of basic C control flow. First compile and run the program to see what it does. Play around with the initialization values of the control variables i, j, k, and m and see how that changes the program output. 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


Checkoff: Explain the changes you made.

Bonus: If you are allowed to replace appearances of the control variables (i,j,k,m) with each other in the code, what is the minimum number of variables you could use to produce the same output above?

Exercise 3: The Biggest Integer

In class we discussed number representation. In particular, we discussed unsigned integers and two's complement, the most commonly used 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. Based 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 perform two's complement negation of the most negative value. Can you explain why this happens?

Checkout: Give your answers to the above four questions to the TA.