Main Contents Page | CTEC1401 Programming in C |
Quick Reference |
Week 1: The Unix Environment |
Week 2: Introduction to C |
BEFORE YOU START.
We would be really grateful for your feedback on the induction week activities.
Please go to: Short Questionnaire
Your feedback is very much appreciated! Now the C Programming exercises continue below.
All of the work you will be doing for this module will require you to interact with
the operating system by typing commands into the shell. This is achieved
by running a Terminal
application.
The shell is a command interpreter which allows you to type in commands for Unix to execute; this is the typical way of interacting with Unix. You usually call up a shell by clicking a button displaying a picture of a terminal.
________ |>_ | | | |________|
This rather old-fashioned way of communicating is surprisingly effective when you get familiar with it. It can be more flexible, quicker and easier than trying to do the same thing by lots of pointing and clicking. The real advantage, however, is that you now have total control over what happens - you can issue the precise instruction to make Unix do exactly what you want without, for example, having to search for a (possibly unavailable) menu option.
Click on the relevant button to invoke a shell window and then read (click to) the section The Shell Interface and then return (Back) to this page.
Most of what you do with Unix will involve manipulating files of one sort or another. The Faculty's network supports a common file store which enables you to access your files from any of the Faculty's computers. How this is physically organised is a fascinating topic in its own right but for now we will simply look at how Unix views this filestore and how you can navigate around it.
Unix has a hierarchical file structure which is organised as a tree of directories (what Windows calls folders). Directories may contain files or (sub)directories. The root of the tree is the topmost directory beneath which all other files and directories are stored: its name is simply / (a single forward slash). Unlike Windows, there is not a separate root for each logical drive.
Read (click to) the section The Unix Filesystem and then return (Back) to this page.
foo
At the terminal enter the command
cd
This will change the current working directory to your home directory. You can confirm this by entering the command
pwd
cd
means "Change Directory". When typed on its own it always changes
the current working directory to your home directory. pwd
means
"Print Working Directory" and you use this to confirm your location at any time.
Finally, enter the command
ls
ls
means "List Directory". It provides a listing of the files (and
subdirectories) that are contained in the current working directory.
Enter the command
echo Hello > hello.txt
echo
means what it says. The word Hello
is echoed. The
greater-than symbol that follows it is a redirection symbol and tells Unix
to echo the word Hello
into a file called
hello.txt. If the file already exists in the current
working directory then it is overwritten, otherwise it is created.
Now check that the file exists (its name should appear in the listing)
ls
and look at its contents by "concatenating" them to the terminal
cat hello.txt
cat
means "concatenate files". It displays the contents of a list of
textfiles one after the other. The result is printed on the standard output (which,
in this case, is the terminal). For more information look at the section
Displaying and Printing Files and then return (Back)
to this page.
You have created a text file. Now it is time to create a new directory. Read the section Creating New Directories and then return (Back) to this page.
Create a directory by entering the following command
mkdir temp
You have created a new directory called temp. If you type
ls -l
then this provides you with a long listing and you should see some more information about your current working directory. In particular you should see the file hello.txt and the directory temp. The long listing shows you some information about "permissions" and ownership. Importantly, you will see the flag d for "Directory" as the first character on the line displaying your newly created temp directory.
It would be better to store your hello.txt file in your newly created temp directory (rather than your home directory which is where it is at present). You can achieve this by moving the file into the temp direcory.
mv hello.txt temp
List your current working directory and you will still see temp but the file hello.txt will no longer be there. This is because you have moved it somewhere else.
Change your current working directory to temp
cd temp
and check where you are.
pwd
Finally, if you list (ls
) the contents of the directory you
should see the (recently moved) file hello.txt.
Now suppose that you wish to rename the file hello.txt and call it instead greeting.txt
mv hello.txt greeting.txt
Notice that this time the second argument to mv
is not a directory
but a new filename. The behaviour of mv
is different in this case -
it will move (rename) the file. You can check the result by listing the current
working directory (ls
).
For more information on moving (renaming) files look at the section Renaming Files and Directories and then return (Back) to this page.
If you have followed everything up to this point then you will have created a directory called temp in your home directory; this will be your current working directory; and it will contain the (recently renamed) text file greeting.txt. Now it is time to make a copy of this file
cp greeting.txt salutation.txt
cp
means "copy files". For more information look at the section
Copying Files and then return (Back) to this page.
Now you should list the contents of the current working directory
(ls
) and check that both files are present.
Your current working directory is temp. We now want you to create another directory (within this one) called somewhere. Enter the command
mkdir somewhere
Perform a long listing (ls -l
) and check that all is well.
Now we wish to make a copy of salutation.txt and place
that copy in your new directory.
cp salutation.txt somewhere
Note that this uses an alternative version of the cp
command in which
the final argument is a directory name and not a filename. This
results in a copy of the file being made and placed into the directory. Enter the
command
ls somewhere
This allows you to see the contents of somewhere without making it your current working directory. You should see that it contains a copy of your salutation.txt file. Now let us make another copy and place it inside somewhere but give it a new name (otherwise it will overwrite the copy we just made). Enter the command
cp salutation.txt somewhere/hello.txt
Make somewhere your current working directory
cd somewhere
and list the contents. What does the following command now do?
cp hello.txt ..
Read the sections on Deleting Files and Deleting Directories and then return (Back) to this page.
Now remove all the files and directories you created during these lab exercises so far, and return to your home directory.
First of all we suggest you create a directory (within your home directory) that you use to store all your C programs. This will keep things tidy and ensure that your exercises for this module are not mixed up with those for other modules on the course.
cd mkdir ctec1401 cd ctec1401
Naturally, you can organise your C program files within subdirectories inside ctec1401. We will leave it up to you how you choose to organise your filesystem.
An easy way to launch the jEdit text editor is to go to the terminal and type:
jedit &(The trailing ampersand (&) is important because it allows the program to run as a separate process in the background.)
Using the jEdit text editor, create a text file hello.c which contains the following text.
#include <stdio.h> int main() { printf ("Hello World!\n"); return 0; }Save the file (be careful to ensure that you are saving it in the correct directory!) and double check that it has been saved correctly by typing
ls
in your shell window.
To compile the program type the command:
gcc -o hello hello.cThis says run the compiler "
gcc
" on the C source file
hello.c
and save the executable "-o hello
" in a file called
hello.
If your file contains any errors (you may have mis-typed or mis-copied something)
then you will need to check this and correct the problem. However, let us be
optimistic and assume that the compiler successfully performed the translation.
If you type ls
you should see the file hello
listed amongst the contents.
If you want to peek at what this file looks like, enter the following command
od helloYou should see lots of code (represented as numbers) typed to the screen. Clearly you would not want to write this directly - the C program ``source text'' hello.c is much easier for humans to read and write. To execute this program simply type its name the shell prompt:
./hello
Using the hello.c program as inspiration create the following C programs. For each you should compile and run the program to check that they are all working.