The purpose of this problem set is to show you how to use the create, compile, run, and debug C programs on the NC State Linux computers.
Many of the documents for connecting to the NCSU computers assume you'll want a remote X11 connection. That might be a good idea for on-campus (dorm) access, but it would probably be too slow for remote users, particularly, those with DSL connections. Consequently, this document avoid use of remote displays. If you really want to use X windows, you should consult the EOS instructions on installing X-Win32,
You will need a remote login client that support SSH connections. NCSU (and Computer Science at UNCA) recommends you use PuTTY. NC State provide a PuTTY download site. You can also use the PuTTY developer's download site for the more recent versions.
PuTTY has an easy-to-use interface for remote connections. However, you must be sure to specify XXXXXXX. I also suggest you choose a large font size than the default.
You can make a PuTTY connection directly to remote-linux.eos.unca.edu and compile and run C programs. However, it is strongly recommended that connect to the NC State Virtual Computing Lab page to reserve a Linux machine for your compilations. Be sure to reserve a "Linux Lab Machine".
At this point, I'd usually ask you to connect to your class workspace directory using the Linux cd command shown below where 6XX is your section number and unityid is your unity ID.
cd /afs/eos/lockers/workspace/ece/ece209/6XX/unityid
However, that isn't working right now. So let's just create and connect to a directory called ece209ps1/hello for this lab using the following two commands.
mkdir -p ece209ps1/hello cd ece209ps1/hello
You need to use a text editor to create your program. I suggest you use nano It's about as simple as it gets. Just enter the command nano, type in your program and use the commands at the bottom of the nano screen to save it.
Type in the program and store it in the file hello.c.
/* Problem Set 1 * * John Doe */ #include <stdio.h> int main(void) { int num = 209 ; printf("Hello ECE %d. This is such fun!\n", num) ; return 0 ; }
Compile your program using gcc.
gcc -o hello -g -pedantic -Wall -Werror hello.c
If the compile was successful, you'll see nothing. If it didn't work, you'll see error messages. Let's take apart the command you typed a little. The "gcc" is the name of the program you run. It's the GNU C compiler. The "-o hello" specifies that the compiled and linked program should be stored in the executable file hello. The option "-g" causes debugging information, such as the program's symbol table, to be included in the executable. "-pedantic" requires that your program follow the ISO standard. "-Wall" causes all warnings to be printed and "-Werror" causes all warnings to be interpreted as errors.
./hello
You will make programming mistakes. The GNU debugger gdb makes it easier for you to find your mistakes. Start the debugger with a single command.
gdb hello
You'll see a few informative messages and then "(gbd)", the debugger prompt. Start by typing the help command and follow that with the two word command help stack. This is a very helpful command for even gdb experts.
Next type run followed by quit.
That really wasn't very useful, so let's try again.
Start up gdb a second time, and
this time type the command list. Note the
number of the line in which num
is assigned
a value.
Now type the command break N, where
N is the number of the line where
num
was assigned a value.
Execute the run command. Notice that this time
your program stops at a breakpoint.
Use the command p num to print the
value of num
. You'll some weird value because
num
hasn't been assigned a value yet.
Now use the next command to execute the assignment
statement. Use p num to see the num
really does contain a useful value now.
Finish off with the continue and quit commands.
This time create a directory ece209ps1/balance and store within it the following program which is a buggy implementation of the optional C programming assignment of the Fall 2007 ECE 109.
/* Problem Set 1 * * John Doe */ #include <stdio.h> main(){ double sum = 0 ; double mi ; while (scanf("%lf", mi)==1) { scanf("%lf", &ri) ; sum += mi*ri*ri ; } printf ("Sum is %10.5lf\n", sum) ; if (sum<0) { puts("Leaning left") ; } else if (sum==0) { puts("balanced\n") ; } else { puts("Leaning right\n") ; } }
Modify the program so that it compiles and runs without an error. Leave the fixed program stored in your ece209ps1/balance directory.
If we don't get the lockers working, type the following two commands to "turn in" your program.
cd ls -lR ece209ps1 | Mail -s "ECE 209 PS1" brock@cs.unca.edu