C Lab 0 - Introduction to C

CS3410 Spring 2015

Due in lab section. Also, can do on your own by Monday, January 26th, but it will be much easier in lab section if you need help!

Overview

Welcome to CS 3410! In this course we will learn about computer systems organization and programming. Most systems programming is done in the C programming language, or in a similar language. Today you will compile a simple C program. Also, you will install a virtual machine for the course that can run on your local personal computer. If you do not have a personal computer, then you can use the departments CS undergrad lab (CSUG) computers. Simply follow the instructions below.

Step 0a: Obtaining and Running the Virtual Machine

If you are working on your own computer, you will need to download and set up the CSUG lab virtual machine in order to have access to a copy of the lab's Linux environment. First you have to obtain the disk image as follows:

Once you have the file, you will need to unzip the file (with a 7zip extractor, such as 7Zip on Windows or Keka on Mac) and you will need virtual machine software to run it (either VirtualBox or VMWare Player). Once you have these software installed, follow the instructions under "Preparing your Computer and the VM" to get the Virtual Machine up and running.

Step 0b: Logging into a CSUG computer, if necessary

If you do not have a personal computer or you were not able to install successfully a virtual machine, you may log into a CSUG computer and complete the rest of the lab there.

For Linux and Mac users, simply typet the command:

 ssh [netid]@csugXX.csuglab.cornell.edu  

(Replace XX with a number between 01-14 and [netid] with your NetID.)

For Windows users, download an SSH client. The recommended SSH client is PuTTY. To connect to CSUG machines using PuTTY:
1.) Open PuTTY
2.) Under Host Name enter: [netid]@csugXX.csuglab.cornell.edu (Replace XX with a number between 01-14 and [netid] with your NetID.)
3.) Press Open
4.) A window labeled 'PuTTY Security Alert' should pop-up which provides a warning about the host key. Click Yes.
5.) You are connected.

Step 1: Hello World

Now that you are in the virtual machine or on the CSUG computer, you should be able to write, compile, and run simple C programs. Run the command:

  nano hello.c

This is a simple text editor, and we will use it to write a C program. Carefully type in the following C program:

  #include <stdio.h>

  void main() {
    printf("Hello world! I am [netid].\n");
  }

But replace [netid] with your NetID. When you are done typing, press Ctrl+X followed by Y to save and exit, and hit the Enter or Return key a few times until you are again at the command prompt. Now you are ready to compile and run the program you just created! Run the command:

  gcc -o sayhello hello.c

If this gives you any errors, go back to nano hello.c and fix the program. Otherwise, you have just compiled a C program. You can run your program by running the command:

  ./sayhello

And your program should run! It should print "Hello world! I am [netid]." and do nothing else.

What does ./sayhello mean? The C compiler (GCC) has compiled your source code hello.c into a program sayhello. The command ./sayhello means, "Run the program sayhello in the current directory."

Step 2: Upload the hello.c and sayhello files to CMS

Now you must upload the hello.c and sayhello files to CMS. To do this, you must first get those files out of the virtual machine. The easiest way to do this is by sending yourself an email with the two files as attachments. To do this, run the command:

  echo "Sending files." | mail -s CLab0 -a hello.c -a sayhello [netid]@cornell.edu

But replace [netid] with your NetID. Run this command. If succesful, this command will say nothing. In a few minutes you should receive the files for CLab0 in your cornell.edu mail account. Make sure to check your spam folder.

Upload both files to CMS, and pat yourself on the back. You are now officially a C programmer!

More Information

Section 1.3 of the assigned book "C: A Refernce Manual" is a good overview of C programming, and it includes a "Hello World" example like this lab.

There other ways to transfer files out of the virtual machine. Please look at these slides for more information on VMs and Unix, including some alternative ways of transferring out your files.