The only way to attain proficiency in programming is to practise. This includes trying out and experimenting with the programs presented in the lectures, discussion sessions and lab programming exercises. It is not good enough just to read others' C programs. You must type them in, compile them, and run them yourself! Your programming aptitude can only be improved with programming experience. As you will be using a UNIX platform for C programming, we have provided a step-by-step walkthrough which will guide you through the followings:
You should be greeted with a Windows screen if a PC is up (if the PC is not
on, switch it on first). As instructed on the screen, press Ctrl-Alt-Delete.
A login screen will appear. You are to type in your NUSNET
user-id and password, and select the NUSSTU domain. For students
registered this year, your NUSNET user-id begins with a00 followed by 5 digits
(example: a0012345, note that it is a small letter
a), this is different from
your matric. number which has one more check character at the back
(example: A0012345K, note that it is a capital letter
A).
Note: all NUS
students are issued with the NUSNET account by the Computer Centre. The domain
name of this account is "@nus.edu.sg". All SoC students, as well as students
from other faculties taking SoC modules, are also issued with SoC UNIX account. The domain name of the SoC account is
"@comp.nus.edu.sg". This SoC UNIX account will be purged at the end of the
semester for non-SoC students. Do not mix up NUSNET account and SoC UNIX
account.
In class, you will need to login to sunfire, a UNIX server of SoC, to do your work. If you are accessing sunfire for the first time, you will need to first create (or re-enable) your own personalized SoC UNIX account: (1) start up a web browser; (2) go to https://mysoc.nus.edu.sg/~newacct; (3) follow the instructions to create your UNIX Account (use a maximum of 8 characters in your password or you may encounter problems). After obtaining a UNIX account, you can then login to sunfire using your UNIX account/password.
You are advised to create your UNIX account before the first lecture.
In class, you will use the SSH Secure Shell software to login to sunfire. The PCs in the programming labs come with SSH Secure Shell installed. To install SSH Secure Shell in your own Windows PC/notebook, you can download SSHSecureShellClient-3.2.9.exe from the Lab folder in the IVLE Workbin.
To launch SSH Secure Shell, look for its icon
on the desktop and
double click on it. A screen similar to the
one shown below will appear. Click on the Quick Connect button and a small
window Connect to Remote Host will pop up.

Under Host Name, enter sunfire (if you are logging from lab PCs) or sunfire.comp.nus.edu.sg (if you are logging from outside SoC, e.g., at home).
Under User Name, enter your SoC UNIX account name (cs1010 is used in the above picture as an example).
Ensure that the Port Number is 22 and then click on the Connect button.
If you encounter the following window, just click "Yes".
When this is done, another small window will appear to prompt you for your password. Note that the user-id and password are case-sensitive (i.e. an "a" is different from an "A"). The password will appear on the screen as asterisks (***) so you need to type slowly and carefully. Once you have logged into sunfire, you are automatically placed in your home directory.
You can also login to sunfire from the UNIX platform of your local machine (e.g., Mac or Linux). Assuming that your user-id in sunfire is a0012345, enter the following command (in bold) at the local UNIX prompt.
localuser@localmachine:~[xxx]$ ssh a0012345@sunfire.comp.nus.edu.sg
When you are prompted for password, type your password and then press the <ENTER> key. The password is case-sensitive and for security reasons, you will not get to see what you have typed. Once connected, you will notice that the UNIX prompt has been changed to "a0012345@sunfire0:~[xxx]$", indicating a successful login. You are now at the home directory of your UNIX account in sunfire.
UNIX User-Ids and Passwords
Always remember your password! If you forget it, you would need to go to I Forgot My SoC Password page to change your password online (this requires your NUSNET ID and password).
For this course, you will also need to configure your sunfire UNIX account. This step must be correctly done or else you will NOT be able to submit your programs for marking!
After connecting to sunfire using your UNIX account/password, type the following command in your SSH Secure Shell window (you should do this only ONCE):
~cs1010/lab0/setup
Check that you enter the above command ACCURATELY (the tilde character ~ is usually found on the top left of your keyboard next to the "1" key). This command initializes your account and prepares it for this initial lab session. It creates the "c" sub-directory (a directory is a folder), and copies some files into your home directory and the "c" sub-directory. Refer to Appendix A for a list of these files and their descriptions.
After the setup program runs successfully, you must type:
source .bash_profile
This sets up useful commands for your program submission later. Remember to type the SPACE in between "source" and ".bash_profile". Note that if the above command runs successfully, you will see no output before the next prompt appears. With many UNIX commands, "no news is good news". This is very different from Windows or Mac where you are used to seeing a dialog box pop out. Try to get used to UNIX quickly.
Whether you are using UNIX on your local machine or logging into sunfire, now is the time to start your UNIX adventure. The power of UNIX stems from the many commands it offers. The following are a few commonly used commands. This list is by no means exhaustive and you are urged to explore on your own. Note that UNIX commands are case-sensitive.
In the examples below, bold words are commands which you are expected to enter. All commands are to be entered after the UNIX (local or sunfire) prompt of the form
user@machine:~[xxx]$
~ indicates that you are currently in your home directory. The following examples assume that user a0012345 is logged into sunfire; however you can do it on your local UNIX platform too.
| pwd | to Print current Working Directory to show you which directory you are currently in |
a0012345@sunfire0:~[xxx]$ pwd /home/a00/a0012345 | |
| ls | to LiSt files in your current directory |
a0012345@sunfire0:~[xxx]$ ls c | |
| You may also use "ls -F" for more information (-F is one of the many options/flags available for the ls command. To see a complete list of the options, refer to the man pages, i.e., "man ls".) | |
a0012345@sunfire0:~[xxx]$ ls -F c/ | |
| The slash (/) beside the filename tells you that the file is a directory (folder). A normal file does not have a slash (/) beside its name when "ls -F" is used. | |
| You may also use the "ls -l" command (dash l) to display almost all the file information, including the size of the file and the date of modification. Try it now! | |
| cd | to Change Directory from current directory to another |
a0012345@sunfire0:~[xxx]$ cd c a0012345@sunfire0:~/c[xxx]$ ls -F example1.c example2.c example3.c | |
| Note that the prompt changes to ~/c to indicate that you are
now in the c directory below your HOME directory. Entering "cd" alone brings you back to your HOME directory, ie. the directory in which you started with when you first logged into the system. | |
a0012345@sunfire0:~/c[xxx]$ cd a0012345@sunfire0:~[xxx]$ | |
| mkdir | to MaKe a subDIRectory in current directory |
a0012345@sunfire0:~[xxx]$ mkdir another a0012345@sunfire0:~[xxx]$ ls -F another/ c/ | |
| rmdir | to ReMove a subDIRectory in current directory -- note that a directory must be empty before it can be removed. |
a0012345@sunfire0:~[xxx]$ rmdir another a0012345@sunfire0:~[xxx]$ ls -F c/ |
| cp | to CoPy files |
a0012345@sunfire0:~[xxx]$ cd c a0012345@sunfire0:~/doc[xxx]$ cp example1.c duplicate.c a0012345@sunfire0:~/doc[xxx]$ ls duplicate.c example1.c example2.c example3.c | |
| mv | to MoVe files from one directory to another; can also be used to rename files. |
a0012345@sunfire0:~/doc[xxx]$ mv duplicate.c new.c a0012345@sunfire0:~/doc[xxx]$ ls example1.c example2.c example3.c new.c | |
| rm | to ReMove files. Be careful with this command -- files deleted cannot be restored (unless they have been backed up during the normal backup cycle). |
a0012345@sunfire0:~/doc[xxx]$ rm new.c rm: remove 'new.c'? y a0012345@sunfire0:~/doc[xxx]$ ls example1.c example2.c example3.c |
| cat | to string together or display (CATenate) the contents of files onto the screen |
a0012345@sunfire0:~/doc[xxx]$ cat example1.c | |
| less | variant of "cat" (includes features to read each page leisurely) |
a0012345@sunfire0:~/doc[xxx]$ less example1.c | |
| In "less", use <space> to move down one page, 'b' to move Back up one page, and 'q' to Quit from "less". |
An online help facility is available in UNIX via the "man" command ("man" stands for MANual). To look for more information about any UNIX command, for example, "ls", type "man ls". Type "man man" and refer to Man Pages to find out more about the facility. To exit "man", press "q".
The process of creating a working C program involves the following steps:
+---------+
+---+-->| EDIT |
| ^ +---------+
| | |
| | |
| | v
| | +---------+
| +---| COMPILE |
| +---------+
| |
| |
| v
| +---------+
+-------| RUN |
+---------+ |
|
| |
|
Three sample C programs have been copied into your "c" subdirectory. Use "cat" or "less" to view the program "example1.c". Assuming that you are at your home directory (if not, enter "cd" to get to your home directory first), issue the following commands:
a0012345@sunfire0:~[xxx]$ cd c a0012345@sunfire0:~/c[xxx]$ less example1.c
|
/***************************************************** * Program example1.c * * This program computes the * distance between two points. *****************************************************/ #include <stdio.h> #include <math.h> int main(void) { /* Declare and initialize variables. */ double x1=1, y1=5, x2=4, y2=7, side_1, side_2, distance; /* Compute sides of a right triangle. */ side_1 = x2 - x1; side_2 = y2 - y1; distance = sqrt(side_1*side_1 + side_2*side_2); /* Print distance. */ printf("The distance between the two points is " "%5.2f\n",distance); return 0; } |
Compile and run the program as follows (use the "gcc" compiler):
a0012345@sunfire0:~/c[xxx]$ gcc -Wall -lm example1.c
This will compile "example1.c" and produce the default executable file "a.out". Note that the "-Wall" flag (dash Wall) is to enable warning-checking feature of the compiler and the "-lm" flag (dash lm) is used whenever a C program contains the line #include <math.h>.
Note that if gcc compiles your program successfully, you will see no output. With gcc, "no news is good news". You are now ready to execute your program by typing the name of the executable:
a0012345@sunfire0:~/c[xxx]$ a.out
This will execute the file "a.out" (Some of you may need to type "./a.out" instead of "a.out" to execute the program, if your account is set up in certain manner.)
You need an editor to write your programs. In this course, you shall be using vim - a powerful editor with many commands, but even with the knowledge of a few simple commands it is quite easy to use and very powerful.
Get into your c subdirectory if you are not there. Type "vim first.c", then press the "i" key. You will see that the words "-- INSERT --" appear on the bottom left corner of your screen. You are now in INSERT mode. While you are in INSERT mode, you may use the arrow keys (Up, Down, Left, Right) to move around your program, as well as the Backspace key and Delete key to delete text. Do not use the scroll bar as it does not always work as expected.

Notice that line numbers (1, 2, 3, etc.) are displayed on the left. This helps you to easily identify a line by its number. This is extremely useful when identifying the locations of compilation errors. However, note that line numbers are NOT part of C code.
Now type in the following program:
|
#include <stdio.h> int main(void) { int a=27, b=6, c; c = a%b; printf("The value of c is %d.\n", c); return 0; } |
Note that for simplicity, the above program has no documentation. A good program should include documentation, at least the identity of the author, the purpose of the program and other relevant information. Keep this in mind when you write your own programs.
When you startup Vim, it begins in COMMAND mode. One way to go into INSERT mode is to press the "i" key. While in INSERT mode, you can type in your C code. To switch back to COMMAND mode, press the "<ESC>" key.
When you are done with coding, press "<ESC>:wq<ENTER>" to save your program and exit from the Vim editor: <ESC> means press the Escape key, then press the colon key (shift+;), then press wq (lowercase), and finally <ENTER> means press the Enter key.
If you want to save your file without exiting from the Vim editor, press "<ESC>:w<ENTER>". It is a good habit to save your file periodically so that if the network or the system goes down for any reason, you will not lose your hard work completely.
The following shows you a list of useful commands in Vim:
There are many other powerful Vim commands which we will explore in lecture and discussion.
To undo or redo in Vim, ensure that you are in COMMAND mode and press lowercase "u" or control-R respectively.
Having "edited" your program, save your work and exit Vim. Compile your program by entering the following command:
a0012345@sunfire0:~/c[xxx]$ gcc -Wall first.c
There will be error messages if your program has errors. Go to Step 4.2 to make the necessary corrections and re-compile. If there are no compilation errors, proceed with program execution as follows:
a0012345@sunfire0:~/c[xxx]$ a.out
Note that this "a.out" file is the executable version of "first.c", and it has replaced the earlier "a.out" file which is the executable file of "example1.c", since there must not be two files with the same name in a directory. To avoid this, you may specify the name of the executable file, instead of leaving it to the system to use the default name "a.out". The trick lies in the "-o" option of the "gcc" command:
a0012345@sunfire0:~/c[xxx]$ gcc -Wall first.c -o first.exe
In the above example, the executable file for "first.c" will be called "first.exe". Note that the name of the executable file MUST IMMEDIATELY follow the -o. Be careful of this or you may lose your source file!
Do NOT ever type the following:
a0012345@sunfire0:~/c[xxx]$ gcc -Wall first.c -o first.cWhat is wrong with the above statement? It states that you want to output the executable file to first.c. This is wrong, because the executable file will overwrite your source program file. That means if you type the above command by mistake, you will LOSE YOUR SOURCE FILE, and you would have to start typing your source file all over again, wasting your time and your effort. So be careful in what you type.
Use the instructions in Steps 4.2 and 4.3 to create, compile and run the following program "second.c". What is the output of the program? How could you add a blank line before and after the line of output, to make it into a 3-line output?
| #include <stdio.h> int main(void) { double a; double b=6, c=18.6; a = b/c; printf("b is %3.1f, c is %3.1f. ", b, c); printf("a is %.6f.\n", a); return 0; } |
Experiment a little by changing the "3.1" and ".6" to some other values and try to figure out what they do. Try also to combine the two printf statements into one. Programming is fun in that you can try out all sorts of things and see the end results yourself! A lot can be learned through self-exploration alone and you need not worry about putting sunfire on fire!
The program "third.c" below contains syntax errors which will be reported during compilation. Though you may be new to C, the error messages do give some information, and by referring to the few correct sample programs above, you should be able to spot some, if not all, of the errors.
| */ This
program contains MANY syntax errors /* include (stdio.h); int main(void) { double num num := 305.68; printf("num is ") printf('%6.1f\n', Num); return 0; } |
Edit the program to correct the errors. Then compile it. If there are still errors, what must you do? Yes, edit the program again, and re-compile it. Assuming that all errors have been corrected, what is the output of this program?
You will use CodeCrunch for submitting your laboratory programs. Your source codes are uploaded to the CodeCrunch server and they are subjected to an automatic assessment. You will see the outcome of this automatic assessment shortly after submission. However, note that CodeCrunch is used only for a preliminary assessment; your submitted programs will be manually assessed by your discussion leader or grader later for a final grade.
Go to this URL: https://codes.comp.nus.edu.sg/ and login to the CodeCrunch with your NUSNET id.
After login you shall see the following home page.

At this moment, only Trial-lab is open. Click on it and you will be brought to the lab instruction page.

You shall use sunfire and vim, which are explained in the previous sections to do your programming. Once completed, you would upload your source codes to CodeCrunch for assessment. To do so, scroll down a bit until you reach the submission section:

Under Submission(Course), click on Browse and then choose your source code file to upload. After the file has been successfully selected, you would see an extra progress bar as shown below.

Now click on submit.
A green bar would now indicate that your script has been successfully submitted. Click on My submissions in that green bar, in order to review the grade of your submitted code.

If you get an A grade as shown below, then you have done well!

Or else, you can review your mistakes.

As mentioned at the beginning of this section, the submitted code goes through automatic assessment, and so you will get the preliminary grade immediately, based on correctness of your output. However, your discussion leader or grader WILL go through your code manually to assess it and give the final grade.
These are the files copied into your home directory by the setup
program.
In your home directory:
Notes: Filenames that begin with a dot (.) are "hidden files" and are not shown with the "ls" command. Use "ls -a" to see them.
In your "c" subdirectory:
There are two ways to transfer files between your local machine and your sunfire UNIX account.
If you have downloaded and installed SSH Secure Shell on your local
machine, perform the usual login to sunfire, then open the File Transfer
program by clicking "Window -> New File Transfer" or the respective
button on the panel.

You will see the contents of your local machine on the left window panel, and the sunfire home directory on the right window panel.

You can navigate around both panels to copy/delete files. To upload files to sunfire or download files from sunfire, just drag and drop them.
From the UNIX platform on your local machine, issue the command "scp <source> <destination>" in a similar way as the copy "cp" command, except that you need to also specify the user-id in sunfire.
For example, to copy the file "example1.c" from the local "c" directory to the home directory in sunfire, enter the following command from the local UNIX prompt.
localuser@localmachine:~[xxx]$ scp ~/c/example1.c a0012345@sunfire.comp.nus.edu.sg:~
Note that the source is "~/c/example1.c" and the destination is "a0012345@sunfire.comp.nus.edu.sg:~".
To copy a file "example2.c" from the "c" directory in sunfire to the home directory with a different file name "prog.c" on your local machine, enter the following command from the local UNIX prompt.
localuser@localmachine:~[xxx]$ scp a0012345@sunfire.comp.nus.edu.sg:~/c/example2.c ~/prog.c
Note that the source is "a0012345@sunfire.comp.nus.edu.sg:~/c/example2.c" and the destination is "~/prog.c".