Lab 1: Using Unix and a first C program

Logging in

NOTE: This first part, about logging in, using netscape, and learning about Unix, is an optional part of this lab. If you are new to our systems and to Unix, you should take the time to go through this part. In this class we will assume you know the material that this part of the lab covers, and the Unix/Linux textbook is a great reference for finding your way around Unix.

You do not have to hand in anything from this part of the lab.

At the beginning of lab, the lab instructor should hand you a slip of paper with your username and your initial password, if you are a new student and do not yet have an account in the CS department.

Sit at a computer that is one of the group of the computers that the lab instructor has designated. If the screen is blank, hit the <enter> key. Something should appear. If it doesn't tell the lab instructor and find another computer to use.

You should either see a login window in the middle of the screen, or a login prompt in text at the bottom of the screen. At the login: prompt, enter your username, and hit <enter>.

You should now have a password: prompt, (or if you have a login window, the cursor should be in the password field). Enter your password and hit <enter>. Note that nothing will appear on the screen while you are entering your password (or maybe *'s will appear for each letter you type).

If something goes wrong, you will see Login incorrect, and a new login prompt will appear (or in the window the window will just clear and wait for another username in the login window). Try again. If it fails again, contact a lab instructor.

When it works, if you logged in through a window, you will eventually see a GUI, a graphical user interface, like Windows or the Mac, but also different. You should have at least one window where a command prompt is waiting for you to type in commands. This window is called a terminal window, and the program controlling the prompt is called a shell.

If you logged in on a plain text screen at the bottom of the screen, you need to start the GUI. If you see a prompt that says "Terminal type? [vt100]", Just hit <enter>. If it just says "Terminal type?", type in "vt100" and then hit <enter>. Now you can start the GUI. You do this by typing in startx, and hitting <enter>. You should eventually see that same thing that the person who logged in through a window is seeing. On these machines, when you "logout" or exit the GUI, you are returned to the text-only screen, but you are not yet completely logged out. Don't forget to "logout" after quitting the GUI!!!! If you do not logout from the text screen, someone can come along and erase all your files, or do whatever they want with your account. You logout just by typing "logout" and hitting <enter>.

Using the terminal to start Netscape

Now, move the mouse pointer around, in and out of your terminal window. You should see the border of the window change color. When the mouse pointer is in the window, the window is active, and you can type commands into it. When it is outside of the window, whatever you type does not go into that window, and the commands aren't executed.

Now, let's start Netscape. You do this by pointing to the terminal window, and entering netscape & and hitting <enter>. The ampersand tells the shell not to wait for this command to finish, but to continue to accept other commands right away.

Now that you have Netscape running, you can go to the Web pages for this class. In the box near the top of the Netscape window, with Location: in front of it, enter

http://www.cs.nmsu.edu/~rth/cs/cs271
and hit <enter>. The home page for this class should appear.

What is Netscape?

Netscape is a browser for the World Wide Web. It lets you look at Web pages -- which are documents that have text and pictures on them. Documents have a name, called a URL, or Universal Resource Locator. This is what is in the Location field, so for our class the URL is

http://www.cs.nmsu.edu/~rth/cs/cs271/

Documents can link to one another -- wherever you see underlined text, that text is a pointer to another document. You can go to the other document simply by clicking on the text (the mouse arrow shape changes to a little hand with a finger pointer). There are buttons near the top of Netscape that say back and forward -- these let you go back to your previous documents, or forward as well.

Learning about Unix

Now it's time to learn more about Unix. We will do this by browsing Web pages and doing some exercises. In the location box, click to bring up the prompt and change the URL to be

http://www.cs.nmsu.edu/~jcook/Classes/

This URL has a pointer to the CS271page, but also has pointers to other things. Near the bottom is a list of pages that are topics explaining Unix. If Unix is new to you, you should go through all of the pages describing it. After the first couple of weeks, we will not consider questions about Unix to be high priority for spending lab time on.
 


C Programming

For 25 years, the standard beginning C program has been one that prints "Hello World!" to the screen. So why break with tradition? Copy this program into a text file named "hello.c":

#include <stdio.h>

int main(int argc, char* argv[]) {
   printf("Hello World!\n");
   return 0;
}
Now that you have the source file "hello.c", you need to compile it into an executable file. The compiler on our Linux machines is "gcc", also known as "cc". So, in a terminal window, enter:
gcc hello.c

If you didn't accidentally type in some errors into the program, you should see this command finish silently, without printing anything out. Otherwise, if you saw some error messages, you'll have to edit the file and fix them, and try compiling again.

If you do an "ls" command in the terminal, it will list the files in your current directory. You should see a file named "a.out". This file is the executable file that the compiler produced. If you do not tell gcc what to name the executable file, it will always produce a file named "a.out". Now enter "a.out" in your terminal window, and hit Enter. You just ran your first C program! You should have seen "Hello World!" printed on the screen.

To make gcc create an executable with a more meaningful file name, we need to use the "-o" flag (that's an "oh", not a "zero"):

gcc -o hello hello.c

Now gcc will create a file named "hello", and this is our executable. Note: typically, executable files on Unix have NO extension. On DOS/Windows, executable files usually have a ".exe" extension. If you have a compiler on your own machine under Windows, you probably want to name your executable files with the ".exe" extension when working there. But remember that when you are on a Linux or Unix computer, your executable files should NOT have the ".exe" extension.


Questions to Answer

1. Change your Hello World program to print the same phrase 10 times, using a while loop. The C while loop is exactly like a Java while loop, and you can declare and use an integer (int) variable exactly the same as in Java.

2. Look at the C++ program in our textbook on page 4. Try typing that program in and running it. The C++ compiler is "g++" rather than "gcc", but it works the same. Instead of a ".c" extension, your source C++ file should have either a ".C" extension or a ".cpp" extension.

2a. How is this program different than the C Hello World program?

2b. How is it the same?


What to turn in

  1. The modified hello world program for problem 1
  2. The C++ hello world program for problem 2
  3. answers for question 2, in a separate text file

Submit these files through the Web Submission Page. Style, neatness, and good commenting of your program will all count towards your grade. Write your answers to the questions into a text file, and also submit that file through the Web.