$ ssh -Y your_user_name@cs.swarthmore.eduYou can open other windows on the CS machine by running xterm & or by running terminal or an X11 shell on the ITS machine and then ssh into the CS system as above.
See Connecting to the CS Department from Mac OS X for more details, and remotely connecting to CS. for more general information about remotely connecting to CS.
All work you do and software you run from the ssh session will be on a CS machine from within your CS account.
Once logged into our system, start by creating a cs31/weeklylab/week01 subdirectory and copy over some files from my public/cs31/week01/ directory into your subdirectory:
cd mkdir cs31 cd cs31 mkdir weeklylab cd weeklylab mkdir week01 cd week01 pwd cp ~newhall/public/cs31/week01/* . ls
We will use the gnu compiler gcc to translate C to an executable form:
gcc testprog.c
./a.outWe will almost always use make and a Makefile to build executables.
/* a multi-line comment is anything between slash-star and star-slash (it is like """ in python) */ // a single line comment starts with two slashes (it is like # in python) // this is C-syntax for importing library code (like import in Python) // stdio.h is the standard input/output library for C, it is included // in most C programs #include <stdio.h> // all programs have a main function, the body of the function (its statements) // are indented between { and } // every statement in C ends with a semi-colon int main() { int x; // in C all variables must be declared before they are used // when you declare a variable you first give the type // (in this case an int) followed by the variable name (x) x = 10; // this is an assignment statement in C (x gets the value 10) printf("hello world\n"); // printf is C's output function, it is a lot printf("x=%d\n", x); // like python's print statement except that // in C printf is a function so you need to pass // it the string argument to print }Formatted output in C is similar to that in Python, try this simple example to see what is printed:
int x; x = 65; printf("%5d: 0x%6x 0t%t %c\n", x, x, x, x);Here is some more information about formatted output:
### Specifying the numberic representation: %d: print out value in decimal (base 10) %u: print out value in unsigned decimal (base 10) %x: print out value in hexidecimal (base 16) %o: print out value in octal (base 8) ### Specifying the type: %d: int %ld: long int %u: unsigned %lu: long unsigned %p: an address value %f: float or double %lf: double %e: float or double in scientific notation %g: float or double in either %e or %f format %lg: float or double in either %e or %f format %c: char (ex. 'x') %s: string (ex. "hello there") ### Specifying field width: %5d: print out the value in decimal in a field of with 5 %-5d: print out the value in decimal in a field of with 5, left justified %6.4f: print out a float in a field with of 6 with a precision of 4
// foo: a simple, poorly named, summation function // x, y: two int parameters (the values to add) // returns: an int value (the sum of its 2 parameter values) int foo(int x, int y) { int z; // a local variable declaration z = x + y; // an assignment statment return z; // return the value of the expression z } // a function that does not return a value has a return type void void blah( ) { printf("this function is called just for its side effects\n"); } int main() { int p; // a local variable declaration p = foo(7, 12); // a call to function foo printf("%d\n", p); blah(); // a call to a void function (no return value) return 0; }
To use the debugger, we usually want to compile the 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 testprog.cNext, we will run the a.out file inside the gdb debugger:
$ gdb ./a.outThe first thing we get is the gdb prompt (our program has not yet started). Typically we will set a break point 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) runThere are three other main gdb commands we will learn today, the first is next, which tells gdb to execute the next instruction and then grab control again:
(gdb) next # this will execute the instruction x = 10The second is list, which will list the code around where we are in the execution
(gdb) listThe final one is print, which lets us examine the state of program variables (and more generally any C expression we pass to the print command):
(gdb) print x # print out the value of the variable x 10 (gdb) print (x - 4) # print out the value of the expression (x - 4)
try entering next a few more times, you can re-run from the beginning by entering the run command again, and then step through statement execution using next, and printing out runtime state using print.
When you are all done, type the command quit to exit gdb and get back the Unix prompt.
We will talk more about C and gdb over the course of the semester, and
more about C types and gdb print this week in class.
Off my "Unix and CS Info Pages and Links" page are some C programming and
gdb references that will be useful this semester:
Try to identify some of the following: