Lab 1: Unix/C Tutorial

Introduction

This tutorial will cover the basics of working in the Unix environment for the and a small C tutorial. It assumes you have a CS account.

Submission

To make sure everything is working correctly, please submit the C file discussed below via the Lab 1 link in Moodle.

Unix Basics

Here are basic commands to navigate UNIX and edit files.

File/Directory Manipulation

When you open a terminal window, you're placed at a command prompt.

You can customize your prompt, to show things like the host you're logged in to, the current directory, the time, etc. If this interests you, Google can tell you more than you'd every want to know.
The tilde character is shorthand for your home directory. To make a directory, use the mkdir command. Use cd to change to that directory:

[taylorm@jazz510 ~]$ mkdir tutorial
[taylorm@jazz510 ~]$ cd tutorial
[taylorm@jazz510 ~/tutorial]$


The command cd .. will bring you up one level of the directory structure:
[taylorm@jazz510 ~/tutorial]$ cd ..
[taylorm@jazz510 ~]$


Other useful cd (call directory) commands include cd which will bring you to your home directory (and thus is equivalent to cd ~, and cd -, which will bring you back to the previous directory you were in.

To create an (empty) file, use the touch command:
[taylorm@jazz510 ~]$ touch test.txt

To copy this file to your new dircetory, you can use the cp command:
[taylorm@jazz510 ~]$ cp test.txt tutorial

If you wanted to copy more than one file, you could use cp * tutorial or cp *.c tutorial.
To see what's in a directory, use the command ls (list stuff).

Some other useful Unix commands:

The Emacs text editor

Emacs is a customizable text editor which has some nice features specifically tailored for programmers. However, you can use any other text editor that you may prefer (such as vi, pico, or joe on Unix; or Notepad on Windows; or TextWrangler on Macs; and others). To run Emacs, type emacs at a command prompt:

emacs helloWorld.c &


Here we gave the argument helloWorld.c which will either open that file for editing if it exists, or create it otherwise. When editing this file you may notice some of that some text becomes automatically colored: this is syntactic highlighting to help you distinguish items such as keywords, variables, strings, and comments.

Some basic Emacs editing commands (C- means "while holding the Ctrl-key"):

You can also copy and paste using just the mouse. Using the left button, select a region of text to copy. Click the middle button to paste.

C tutorial

Read the brief tutorial on C here: A quick C tutorial

Test program

Create a new file (helloWorld.c) with the following inside the file:
#include <stdio.h>

main()
{
    printf("Hello World");
}

At the command prompt, compile the file you just made into an executable called "helloWorld":
g++ helloWorld.c -o helloWorld
and then run your program:
./helloWorld

Now, try out some of the material from the tutorial in a test program. This is a chance for you to mess around with C, as well as an easy way to pick up some points for your lab grade average. We're going to be using C extensively this semester, but you know enough Java for this to be relatively easy. It will be up to you to pick it up as we go.

When you're done, submit the .c file and executable via moodle.