C Lab 0 - Introduction to C

CS 3410 Spring 2016


Due in lab section. You can do it on your own by Friday, February 5th at 11:59pm, 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. You will first have to 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 one of the lab computers in your section. If you do this, make sure you save the Virtual Machine to a USB stick or your network share drive so that you do not have to do this every time. Simply follow the instructions below.

Step 1: Obtain and Configure the Virtual Machine (VM)

Visit our VM GitHub page and follow the read me.

Step 2: UNIX commands

Now that you are logged in to your VM , it's time to familiarize yourself with some basic UNIX commands. Running the course VM, open up a new LXTerminal window to run the following.

  1. The ls command shows you all the files in your current working directory. Try it out.
  2. Type the command mkdir hello_world to create a new directory called "hello_world". Afterwards, use the ls command again - you should see your new "hello_world" directory among the other files.
  3. Use cd hello_world to change your working directory to "hello_world". The cd command is used to change the current working directory.
  4. cd .. moves you up from your current working directory to its parent. Use this command now to return to your original working directory.

The cd and ls commands let you navigate around directories and view files. You can also use the rmdir and rm commands to remove directories and files, respectively. Note that rmdir will abort removing a directory if it is non-empty. Be careful when using these two remove commands — once it is gone you will not be able to recover it. Finally, man lets you get help on how different commands work. Call it using man command_name, e.g. for more information on the cd command, simply type man cd.

Step 3: Hello World

On your VM, open up Sublime and create a new file called hello.c. Type in the following C program:

#include <stdio.h>

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

But replace [netid] with your NetID (don't print square brackets). When you are done typing, press Ctrl+s to save. Now you are ready to compile and run the program you just created! In your terminal, navigate to where you saved hello.c. Now run the command:

gcc -o sayhello hello.c

If this gives you any errors, make sure you are in the right working directory (use ls to confirm that hello.c is in the same directory). If you are in the right location, then you did not enter the program correctly — go back to Sublime 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. For example, if your NetID is "abc123", then compilation and execution would look like something like this:

~> gcc -o sayhello hello.c 
~> ./sayhello 
Hello world! I am abc123.

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

More Information

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

Part 1 of the alternate assigned book "All of Programming" is another good introduction to C programming. It also has an appendix that teaches the basics of UNIX, emacs, debugging, and other useful tools. You can download a book excerpt from the above link, the last 20 pages of which is all about UNIX.