Please note that any blocks starting with % mean that what is after the % should be typed into the terminal / command-line.
To receive your mark for the lab: You must ensure that you submit your work using the give
command prior to the end of this week. However, this does not provide you with the mark for the lab. You need to show your tutor your work either in this lab or the lab in week 2.
All labs work like this. You must submit the week of the lab, and get it marked off that week or teh following week.
The CSE labs use the Linux operating system rather than Windows or Macintosh.
You can connect to these CSE machines remotely by following this guide
When you log in, by default, you'll be using the xfce4 window manager get a linux desktop. Don't worry, it won't bite.
The first time you log in you will be asked to select a panel config as shown in the screenshot below. Make sure you click the "Use default config" button.
Then you'll get a linux desktop.
Along with menus that you can see, such as the Applications Menu in the top left corner, it also has a simple menu you can access by right-clicking anywhere on the desktop. Have a look around and see if you can work out how to open a web browser.
Although there are lots of things you can do by clicking on menu items and icons, we really want you understand how to use linux commands. If you get stuck with any of this, don't hesitate to ask one of your tutors for help, that's why they're here!
If there isn't a terminal that opened automatically click on the terminal icon from the row of icons at the bottom of the screen
This will bring up a window where you can type in linux commands.
There are a few commands you can use to check your account details:
Each of these is a command, and when entering into your terminal, you need to press enter (or return) after each command. Try the three we just mentioned above.
Now we're going to learn about some commands for working with directories (remember a directory is what you may think of as a folder from your days of using windows):
/import/cage/1/z7654321
This means I'm inside my (z7654321's) home directory and my home directory is stored on the fileserver called 'cage'. What does pwd say for you?
For those used to working on Windows, on unix systems a filepath that begins with "/" implies a "root directory" folder, or a folder that is in the root directory. The root directory is the directory where you can't "go up" any further. Keep typing in cd ..
and you'll see you eventually can't keep going. welcome to the root directory. This information won't be very valuable in this course, but it's good to know.
ls prints a list of the files in the present directory. At the moment you have a fresh account, so your home directory most likely only contains your Desktop and a directory called public_html. If you add files to the public_html folder they can be accessed by going to http://cgi.cse.unsw.edu.au/~z1234567/ where you replace that with your own z-number.
Type
$ ls
cd is a command that you can use to change your directory. If you type in cd on its own as follows, you will move into your home directory. Since you should already be in your home directory anyway, this will not do much. But try it anyway in case you have accidentally ended up somewhere else. To move back up a directory type cd ..
$ cd $ pwdto confirm that you are indeed in your home directory
$ mkdir 1911Now if you type
$ ls
you should be able to see the directory you just created
Now we will use cd in a slightly different way where we tell it what directory we want to change into
$ cd 1911To confirm we really are in the 1911 directory type the pwd command again. To see what is inside this new directory type the ls command again. There should be nothing in here! We would now like to create a directory inside our 1911 directory to contain all our work for lab01. So now type
$ mkdir lab01 $ cd lab01Again, use the 'pwd' command to confirm you've now moved into your new directory, and 'ls' command to confirm that it's empty.
Now it's time to create a file. We are going to use the text editor gedit
Type:
$ gedit badPun.c &
This will open up a graphical editor where you can manipulate text.
Adding the &
to the end of a command which invokes a graphical user interface (GUI) application (one that opens
and runs in a new window) is always a good idea. It makes the GUI return control to the terminal as soon as it
starts (i.e., run in the background) rather than after it is closed. If you forget the &
,
you will not be able to type commands into the terminal until the GUI application is
finished. This is because the operating system is "hanging" (not doing anything else) until the program completes (which in the case of a GUI program means to be closed)
Here's a simple example of C, copy it into the gedit window, and save.
// A simple C program that attempts to be punny // Written 01/02/2019 // by Angela Finlayson (angf@cse.unsw.edu.au) // as a lab example for COMP1911 #include <stdio.h> int main( void ) { printf("Hello, it is good to C you!\n"); return 0; }When you save, it will place the contents of the editor into the file 'badPun.c'. If you simply ran
gedit &
instead of gedit badPun.c &
then you would simply have to type in the filename upon saving the file.
Handy Tip 1: On many linux systems copying can be acheived by simply highlighting with the left mouse button and pasting can be acheived by simply clicking with the middle button
Handy Tip 2: Make sure gedit is displaying line numbers down the left hand side. This is important for when we need to fix compile time errors. If it is not displaying line numners, go to the Edit->preferences menu item and check the 'Display Line Numbers' option.
Once you have pressed saved, click on the Terminal window again and type this command to compile your program:
$ dcc -o badPun badPun.cNote: If you are not on a cse machine or vlab and you don't have dcc, you would type the following instead:
$ gcc -Wall -Werror -O -o badPun badPun.c
The -o badPun
option tells the compiler to give
the newly compiled program the name badPun.
Test what happens if you leave the -o badPun
option out. What is the default name dcc (or gcc)
uses? You may wish
to use the rm
command to delete(remove) the file created. (You can google rm or rm linux if you are not sure how it works)
If dcc does not print out any error messages, then your program has been successfully compiled. Otherwise you will need to find the errors in your code, fix them, save the file and compile again.
Handy Tip 3: Look for the line numbers that are displayed in the error messages as they are major clues to where the problem is in your code.
Handy Tip 4: Always start with fixing the first error first. Sometimes fixing one compile error,saving and recompiling can make all or some of the other errors go away!
After successfully compiling you can check that dcc (or gcc) has produced an executable by typing ls -l
and
looking for a newly-created badPun
file (check the file creation time to see if it really is new).
A useful Unix command is man short for manual. Find out what the -l
option for the ls
command
you used before does. Type:
$ man ls
Press 'q' to exit man.
Run the program to test that it works. Type:
$ ./badPun
The ./
before the program name specifies that the program is found in the current directory. When you try and just run the badPun
command without the ./
then the unix operating system will attempt to search for an executable called badPun
in a number of predefined locations. The folder you compiled your program in is not one of these files, so we have to explicitly tell it to look in this directory. Note
that Unix executable (program) names do not require the .exe
extension that you may have have
seen under Windows.
badPun.c
to a new file named bird.c
, like
this:
$ cp badPun.c bird.c
You are now ready to start editing the file bird.c
using your favourite editor.
$ gedit bird.c &
Note that the basic structure of the program can be retained; you just need to change the
comments, and modify and/or add printf statements the program.
Edit the file bird.c
to produce a new program that behaves
as follows:
$ dcc -o bird bird.c
$ ./bird
___ ('v') ((___)) ^ ^
Make sure you save your modified program before you compile it. Make sure you re-compile your program every time you modify the code. Note: One of the most common mistakes student's make is thinking their code isn't working when in actual fact they just have forgotten to compile it
Handy Tip 5 : linux remembers the commands we have recently typed in. By pressing the UPARROW key, it will bring up your previous command and save you retyping it in! Try it. You can press the UPARROW key repeatedley to go back to the second last command, third last command and so on.
Handy Tip 6 : At your linux command prompt, type in ./b
and then press the tab key. Linux will automatically try to fill in your partially typed command for you!
kangaroo.c
from the course account to your
lab01 directory by typing:
$ cp ~cs1911/public_html/23T2/tlb/01/kangaroo.c .PLEASE NOTE: The dot at the end of this command is important
The cp
command takes in two arguments (an argument is text separated by a space after the program command/path). The first argument is where the file is copied from, and the second argument is where the file is copied to
~cs1911 is the home directory of the course. Just like you have a home directory, so does the course. Only certain parts of the course account home directory are available for you to copy.
The dot '.' is a shorthand for the current directory and there is a space between kangaroo.c
and the next dot. You can check that the file has been copied by
typing:
$ lsYou can examine the contents of the file by typing:
$ less kangaroo.c
(less
is an improved version of an earlier utility called more, which shows you long text files one
page at a time and allows you to go forward and back using the space bar and the 'B' key. Less is
more! Press 'Q' to exit less.)
Then try to compile it. You should see a list of confusing error messages.
$ dcc -o kangaroo kangaroo.c
kangaroo.c:4:1: error: expected identifier or ?(? before ?/? token / by Andrew Taylor (andrewt@cse.unsw.edu.au) ...
You job is to fix the errors.
Open kangaroo.c
in gedit.
When you have fixed all the errors you should be able to do this.
$ dcc -o kangaroo kangaroo.c
$ ./kangaroo
/\ ___ <__\_/ \ \_ / _\ \_\ / \\ // \\ =// \==Hint: the error messages are confusing but they usually indicate where the problem is.
Hint: treat it as a puzzle. Look for differences between it & the working programs you have been shown or have just created.
Hint: all the errors but one involve a single missing character.
Hint: it looks like there are too many backslashes in the program - they are correct. Don't delete any backslashes (you do need to add one backslash).
Hint: Backslash has a special interpretation in C so to print one \ character you need to type \\ in your program. Putting a backslash in front of a special character in C does what we call escaping it. This means that the character following it escapes it's original purpose and meaning and just becomes a boring old character.
bird.c
// A simple C program that prints an ASCII bird // Written 1/3/2012 // by Angela Finlayson (angf@cse.unsw.edu.au) // as a lab example for COMP1911 #include <stdio.h> int main(void) { printf(" ___\n"); printf(" ('v')\n"); printf("((___))\n"); printf(" ^ ^\n"); return 0; }
kangaroo.c
// A simple C program that prints an ASCII kangaroo // Written 7/3/2015 // by Andrew Taylor (andrewt@unsw.edu.au) // as a lab example for COMP1911 #include <stdio.h> int main(void) { printf("\n"); printf(" /\\ ___\n"); printf(" <__\\_/ \\\n"); printf(" \\_ / _\\\n"); printf(" \\_\\ / \\\\\n"); printf(" // \\\\\n"); printf(" =// \\==\n"); printf("\n"); return 0; }
Your tutor has sent an email to your official UNSW email account. You need to show this email to your tutor (or reply to it, if you are finishing this at home) to get any lab marks. We want to make sure you can receive official emails! How to access your student emails. Instructions on forwarding mail to your personal email address.
In our class forum is a welcome thread.
Leave a comment (and optionally a cute photo of any pets you have).
lab01
directory):
give cs1911 lab01 badPun.c bird.c kangaroo.cSubmit advanced exercises only if you attempt the advanced exercise.
Remember the lab assessment guidelines - if you don't finish the exercises
you can finish them in your own time, submit them
by 19:59:59 Sunday using give
and ask ask tutor to assess them at the start of
the following lab.
You can also just run the autotests without submitting by typing
1911 autotest lab01