[~]$ cd ~/cs31/weeklylab [weeklylab]$ mkdir week02 [weeklylab]$ cd week02 [week02]$ cp ~newhall/public/cs31/week02/* . [week02]$ ls arrays.c Makefile testprog.c types_scanf.c
Now, let's try it out on one of the files you copied over (we will use the -g and -o outfile options to gcc):
gcc -g -o testprog testprog.c ./testprogWith the code you copied over is a Makefile. In this file are rules for compiling executables from the .c source files that are executed by typing in the make command. make is very convenient way to compile without having to type in a long gcc command every time, you just need to type make:
make # this will compile all files as specified by the all: rule make clean # this removes all files generated by make (they can be rebuilt)
We are going to start by looking at arrays.c, which contains examples of how to use (and abuse) arrays in C.
Then, we are going to look at the types_scanf.c example, and try running it to see what is happening.
Talking about scanf will lead naturally to an introduction to pointers in C. Later in the semester, we'll talk about the basics of how to define them and use them. We'll also talk about the relationship between pointers and arrays.
For now, just remember that scanf needs to know the memory location of where to put the values read in, and we use the & operator on a variable name to pass this location to scanf. For the Lab 2 assignment you will need to do something similar when you call library functions to read in values from a file.
To use the debugger, you usually want to compile your C program with the -g flag to add debugging information to the a.out file (this allows gdb to map machine code to C program code that the programmer understands).
$ gcc -g -o testprog testprog.cActually, the makefile already has this rule for us, so let's just run make. Next, we will run the executable file inside the gdb debugger:
$ gdb ./testprogThe first thing we get is the gdb prompt (our program has not yet started). Typically we will set a breakpoint at main. A breakpoint tells gdb to grab control at a certain point in the execution, in this case right before the first instruction in main is executed:
(gdb) break mainNext, we will enter the run command at the gdb prompt to tell gdb to start running our program:
(gdb) runThe run command will start your program running, and gdb will only gain control again when a breakpoint is hit.
There are a few other main gdb commands we will learn today, the first is next (or just n for next), which tells gdb to execute the next instruction and then grab control again:
(gdb) next # this will execute the instruction x = 10The list command lists the C source code around the point where we are in the execution:
(gdb) listlist with a line number lists the source code around that line:
(gdb) list 30cont tells gdb to let the program continue running. Since we have no more breakpoints it will run until termination.
Now lets add a breakpoint in the function mystery, and rerun the program:
(gdb) break mystery (gdb) runLet's set a breakpoint at line 20, right before the call to mystery, then type cont to continue execution from the breakpoint in main:
(gdb) break 20 (gdb) cont (gdb) listWe can use the print command to print out expressions in the program, so let's cont to the breakpoint in mystery and then print out the values of its paramters.
(gdb) cont (gdb) print a # print out the value of the variable a (gdb) print (a - 4) # print out the value of the expression (a - 4) (gdb) list
The where or bt command prints the call stack:
(gdb) whereLet's step through some of the mystery function's execution, and print out some of its parameters and locals.
When you are all done using gdb, type the command quit.
If you use gdb to help you debug the lab this week, you may need to know how to use it for programs that take command line arguments. For such programs, simply list the arguments after the run command:
(gdb) break main (gdb) run 6 4 hello
We will talk more about C and gdb over the course of the semester.
Off Professor Newhall's "Unix and CS Info Pages and Links" page are some C programming and gdb references that will be useful this semester:
In Lab 2, you will be writing some C code to perform a sorting task.