U.C. Berkeley Fall 2010, CS61C

Lab1, Part 1: The C Environment, String Representation



These lab exercises are intended to show you how to run a C program on the EECS instructional computers, and introduce you to the C debugger gdb. You will gain some practical experience using the debugger while exploring the structure of C strings.


  • K&R: 1.1-1.5
  • P&H (4th ed.): 2.5


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.



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

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

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 ( ) {
  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


Explain to your TA the change you made to output0 

Exercise 2: Debugger

For this exercise, you will find the GDB reference card useful. Compile your solution to exercise 1 with the "-g" flag:

$ gcc -g -o output0 output0.c
This causes gcc to store information in the executable program for gdb to make sense of it. There are two ways to start the debugger:
  1. Run gdb <filename> from the command line
  2. In EMACS, type M-x gdb, then type gdb <filename>
Single-step through the whole program by:

  1. setting a breakpoint at main
  2. giving gdb's run command
  3. using gdb's single-step command

Type help from within gdb to find out the commands to do these things, or use the reference card.

More gdb commands

Learning these commands will prove useful for the rest of this lab, and your C programming career in general. Create a text file containing answers to the following questions.

  1. How do you pass command line arguments to a program when using gdb?
  2. How do you set a breakpoint which only occurs when a set of conditions is true (eg when certain variables are a certain value)?
  3. How do you execute the next line of C code in the program after a break?
  4. If the next line is a function call, you'll execute the call in one step. How do you execute the C code, line by line, inside the function call?
  5. How do you continue running the program after breaking?
  6. How can you see the value of a variable (or even an expression) in gdb?
  7. How do you configure gdb so it prints the value of a variable after every step?
  8. How do you print a list of all variables and their values in the current function?
  9. How do you exit out of gdb?


Set the breakpoint at main, and show your TA how you run up to that breakpoint. 
Show your TA your text document containing the additional gdb commands. 

Exercise 3: Debugging a short C program

You will now use your newly acquired gdb knowledge to debug a short C program!

Consider the C program appendTest.c. Compile and run the program, and experiment with it. Try appending a few strings, and notice that it does not always produce the correct result. (Press Ctrl-C to exit).

Now, start gdb on the program, following the instructions in exercise two. Set a breakpoint in the append function, and run the program. When the debugger returns at the breakpoint, step through the instructions in the append function line by line, and examine the values of the variables. Pay attention to s1 and s2 in the append function. Are they correct? Why is this a bug?

Hint: How does C represent strings? Fix this bug.


Explain the inconsistent behavior of the buggy program, and explain your fix.