CS61C Spring 2013 Lab 1
C Intro, Number Representation

Reminder

Homework 0 is due in lab this week, so don't forget to have that ready to turn in when you come into lab.

Goals

Exercises

Setup

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 = ___;
   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: 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
Yoshua

Go BEARS!

Checkoff: Explain the changes you made.

Bonus (but not for credit): 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. Louis Reasoner repeats the above three sub-exercises on his home computer, but he finds that the results don't match with what he found while working on the lab computers. How could this be?
  5. 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?

Checkoff: Give your answers to the above five questions to the TA.