Lab 1: Getting Started with Linux and C
Preliminaries
UNIX systems in general, and Linux distributions in particular, usually provide users with two interfaces:- Command-line interface (aka "console", "shell", or "terminal"): Allows the user to interact with the system through the use of commands which you must type in using the keyboard. There are many different types of shells, such as BASH, CSH, and TCSH. Probably the most common, and the one that we use, is BASH.
- Graphical interface: Allows the user to interact with the system through the use of graphical elements such as windows, buttons, text fields, etc. mainly using the mouse. Although the taxonomy of graphical interfaces in UNIX can be a bit complex, most new users will generally interact at first with high-level desktop environments such as KDE and GNOME, which are similar in many respects to graphical interfaces in Windows and Mac systems. Our lab computers run Ubuntu Linux, with the Unity shell overlying the GNOME desktop environment.
In this course, we will use both interfaces. We will compile and execute our C programs within the command-line interface and write our programs using the graphical interface via text editors.
Part I: Getting Started
- Press the Search your computer button found on the upper-left corner of the
screen, type Terminal at the prompt, and click the Terminal icon that appears under Applications. You will be typing
most of your commands in this terminal.
We will be using a project management system called SVN (short for Subversion). SVN is a powerful tool that is useful for backing up versions of your code, collaborating with others, syncing your work across computers, etc. For now, you will primarily use it as a way to submit your work.
- Type in the following command into the Terminal:
svn co https://phoenixforge.cs.uchicago.edu/svn/yourusername-cs152-sum-16
replacing yourusername by your CNet username in lowercase. You will be prompted for your CNet password. If you get a prompt asking you if you would like to accept the certificate, press p (meaning to accept permanently).(For example, since my username is ckimes, I would type svn co https://phoenixforge.cs.uchicago.edu/svn/ckimes-cs152-sum-16).
If you registered late or not at all, this step may not work for you. Please raise your hand if you are having trouble. You will need to submit your work through e-mail rather than SVN until your SVN repository is setup.
What we just did is check out the current version of your personal repository for this class. The repository is stored on a central university server, and is accessible only by you and the course staff. You will use it to save and submit your homeworks and labs.
If all goes well, the Terminal will respond with Checked out revision 0.
- Type these commands, pressing Enter after each one.
cd yourusername-cs152-sum-16
The first command takes you into your project directory, which is yourusername-cs152-sum-16 and the second command lists the directory's contents. (In UNIX-speak, cd is short for "change directory" and ls stands for "list contents".)
lsIf you are on the right track, the Terminal will tell you that there is nothing in the folder by showing you nothing on the terminal.
- Next we will create a folder named lab1. This folder will store our lab1 files. The mkdir command allows you to create folders on the terminal. Perform this by typing on the terminal the command
mkdir lab1
- Now, type in the ls command as before:
ls - Although you just created the lab1 folder, you still need to add this folder to your repository that is stored on the central university server.
The lab1 is only stored locally on your machine but you need to use svn to upload the folder to the server.
Type in the following command
svn add lab1
and the terminal will respond by showing you,A lab1
Note: Anytime you create a new file or folder you want to add to your repository, you must use svn add . You only need to do svn add only once.
- Leave this terminal window open for now. We will come back to it later.
Part 2: Creating your first C program
You will write and compile a simple C program that displays a message to the screen.
- You will write your programs in any text editor of your choice. For those of you who are familiar with emacs or vi, go ahead! For those of you who are not familiar with text editors on Linux, we suggest using the default text editor (Type Text Editor in Search your computer, like we did for Terminal).
- Once the text editor is open, create a blank file (or one should already appear in the editor) and save the file as lab1.c inside your lab1 directory.
- Inside the lab1.c file type the following C code:
/* FirstName LastName, username */ /* CS152, Summer 2016 */ /* Lab 1 */ #include <stdio.h> int main(void) { printf("Yay my first C program executed\n"); return 0; }
Changing FirstName,LastName,username to your actual information.
Make sure to save the file.
- Now go back to the terminal window from earlier and enter the lab1 folder using this command
- You will compile your programs using gcc, the GNU C compiler. To
compile a file (i.e. lab1.c), type
gcc -std=c99 lab1.c
- If the compilation is successful, the compiler
will create an executable named a.out. You can run the
executable by typing
./a.out
Once you run the a.out executable, you should see the message:
Yay my first c-program executed!
and then run it with ./lab1. You should see the same message as before.gcc -std=c99 -o lab1 lab1.c
cd lab1
Remember you need to run this list of commands after saving the program file everytime.
Part 3: Actual Lab Exercise (Temperature Conversion)
Write a C program that performs a temperature conversion from the commonly-used temperature units Fahrenheit (F), Celsius(C), and Kelvin (K). Your program should do the following tasks:
- Prompt the user for a temperature as a floating point number with an appended unit character. The character should be captialized.
- Print the temperature in all three of the units above (i.e., Fahrenheit (F), Celsius(C), Kelvin (K)), as a floating point number with a unit character.
- If the user enters in a character other than 'F','C', 'K', print a message that says "Invalid temperature unit" and exit the program.
- When displaying the temperatures, format them to one decimal place.
Here's an example of how your program should work:
Enter a temperature with units F, C, or K (e.g. 32.1F): 212F 373.1K 100.0C 212.0F
Hints and Formulas:
- Convert whatever temperature is inputed into K, and then convert that back to the three different output temperatures.
- The conversion formulas are:
- K = C + 273.15
- K = (F - 32) * 5 / 9 + 273.15
- C = K - 273.15
- F = (K - 273.15) * 9 / 5 + 32
- Recall the side effects of integer division!
Once completed, save the file and follow the steps in Part 2 to compile and test your program.
Part 4: Committing your work
- Anytime you make changes to your repository (i.e. adding or modifying existing files/folders) you need to svn commit your changes. The command svn add did not save your lab1 folder to you repository. It only told svn that when you perform a svn commit that these files and folders need to be saved to your repository.
- Before committing, you need to svn add your lab1.c to your repository. Follow the step before when you added your lab1 directory but now you want to add your lab1.c file. Note: ONLY add the .c files or to your repository for all labs and homeworks unless otherwise noted! Do not add the binary produced by gcc (e.g. a.out or lab1)
-
Now, submit your lab1 folder and your lab1.c to your svn repository. To do so, you need to go back to your main folder named "yourusername-cs106" by typing:
cd ..
The .. means go up one directory level (i.e. go to the parent directory).
- Now type:
svn commit -m "Commiting the lab1 directory and files"
This uploads your current work into the repository. The string after the -m flag is a message that explains your commit, and can be anything you like. Meaningful messages help you keep track of any additions and modifications to your code at each commit.You should get a message saying
Adding lab1
Adding lab1/lab1.c
Transmitting file data .
Committed revision 1.