Getting familiar with Unix, vi, compiling, and debugging (5 points) Solution



In this assignment, you will make certain that you can (details are provided below):

  • find the main CS lab at NPB 2.118. (It is no longer in the Flawn Science Building.)
  • login to your user id.
  • learn some essential Unix commands (pwd, mkdir, ls -al, cd, man, rm, gcc, cat, vi) and get familiar with the Unix cheat sheet.
  • learn to use vi and get familiar with the vi cheat sheet
  • learn to use gcc to compile your code
  • learn to use ddd to debug your code
  • optionally, learn to use Microsoft Visual Studio

Some useful stuff:

viCheatSheet This word document provides a brief list of the most important commands for using vi. It is available on my webpage under “setup”.

unixCheatSheet This word document provides a brief list of the most important Unix commands. It is available on my webpage under “setup”.

dddTips This word document gives some tips on using the ddd debugger. It also has a reference to a detailed tutorial. It is available on my webpage under “setup”.

vimtutor This provides a tutorial that allows you to practice using the Unix vi editor. Change to the ~/tmp directory (cd ~/tmp) and execute vimtutor. Follow the directions provided in the file.

usingMicrosoftVisualStudio (optional) This word document describes how to use Microsoft Visual Studio. It is available on my webpage under “setup”.

ssh download (optional) This will download a copy of ssh suitable for windows. It is available on my webpage under “setup”.


1. Login

1.1 Go to the CS lab NPB 2.118 (assuming it is available). If working remotely, see how to login remotely on my setUp and skip to step 1.5.

1.2 There should be two types of workstation configurations in the CS lab:

Dell Thin Client – (left side of lab) which access VDI. See how to login to VDI on my setUp.

Linux Workstations – (right side of lab) these run Linux 14.04 and have a good desktop.

1.3 Use a Linux workstation. Login locally using your id (3 letters and 3 numbers). Your password will be defaulted to your UTSA banner ID without the @.

1.4 Open a terminal window. If your Linux login has a menu, use Applications>Accessories > Terminal. If not, use

  • Click on the bottom left icon which will show an applications menu.
  • Click on Accessories
  • Click on LXTerminal
  • Most of the discussion below uses the Terminal window.

1.5 Since your password was defaulted, change your password using the passwd command. (Note that the “$” is being used to indicate that the Linux shell is prompting you for input. The actual prompt may include your user id.)In the terminal window type:

$ passwd

Notes: If you are logging in remotely (from home):

If using linux:

  • Start a terminal window:

$ ssh -X (where ii is one of 01 through 06)

If using windows from home:

  • You should install ssh (see the setup web page).
  • You can transfer files, use vi, and the gcc compiler, but you can’t use ddd without an x-window.
  • When you use ssh specify (where ii is one of 01 through 06)
  • (In the future, consider using ubuntu as a dual boot or use Cygwin.)

2. Check out some Unix directory-related commands. (You may want to look at the unixCheatSheet.)

2.1 After logging in, check your current directory using print working directory:

$ pwd

2.2 See what files are in your directory:

$ ls -al

This will list all files with long details). You should notice some dot files (begin with “.”) such as .vimrc

2.3 Create a directory for cs1713:

$ mkdir cs1713

2.4 Change to your cs1713 directory:

$ cd cs1713

Verify that you are in that directory by using the pwd command.

2.5 Change back to your home directory:

$ cd ~

Verify your current directory using pwd.

2.6 The following are done to practice creating directories and subdirectories:

2.6.1 Create a subdirectory called Fruit and another called Veggie under your home directory.

$ mkdir Fruit

$ mkdir Veggie

2.6.2 Create two subdirectories under Fruit called Apple and pineApple.

$ cd Fruit

$ mkdir Apple

$ mkdir pineApple

2.6.3 Change directory to the Apple directory and then use pwd to check where you are:

$ cd Apple

$ pwd

2.6.4 Create a subdirectory under Apple called Green

$ mkdir Green

2.6.5 Go up to the Fruit directory by using

$ cd ..

$ pwd

Note that cd .. took you up one directory.

2.6.5 You can specify multiple directories in a path when using cd

$ cd Apple/Green

$ pwd

You should be in Fruit/Apple/Green

2.6.6 Go to the Veggie directory using ~ for the home directory.

$ cd ~/Veggie

$ pwd

2.6.7 See what files exist now in your account.

$ cd ~

$ ls -alR

This lists files recursively following subdirectories.

If you have more than a page worth of files, use ls -alR | more. To exit out of more, type q.

2.7 Use the man command to see how it gives you manual pages for most Unix commands by typing man command. You can scroll through the manual pages by pressing the space bar to scroll a page or enter key to scroll fewer lines. To quit out of man, type q.

$ man ls

2.8 Practice using the find and rm commands.

2.8.1 Use the find command to find all files containing the word “Apple”.

$ find . -name “*Apple*”

2.8.2 Use the rm command to remove the Fruit folder and all its contents.

$ rm -r Fruit

2.8.3 Use the up arrow several times to get Linux to show the find command you did. If you go up too far, use the down arrow. Execute that find again and notice that it doesn’t find the Apple and pineApple directories.

2.8.4 Remove the Veggie directory.

3. Use the vi editor.

3.1 Look at the viCheatSheet.

3.2 Try the vi tutorial:

3.2.1 $ cd ~/tmp

3.2.2 $ vimtutor (follow the instructions in that file)

3.3 Create a .vimrc file to help you with indentation and showing line numbers.

3.3.1 $ cd ~

3.3.2 Use the Unix cat command to create that file (input is terminated by pressing the CTRL-D key). Note that this only has to be done once.

$ cat >.vimrc

:set ai sw=4

:set number

:set expandtab

:set softtabstop=4

:set smartindent


3.4 Create your first program for this class using vi

3.4.1 $ cd ~/cs1713

3.4.2 $ vi cs1713p0.c

3.4.3 This is the code (note that the colors are for readability and may vary in vi):


cs1713p0.c by your name


This program computes the average grade for each student.

Command Parameters:



The standard input file stream contains multiple lines (terminated by EOF).

Each line contains:

dExam1 dExam2 dFinalExam szSudentNr szName

10.2lf 10.2lf 10.2lf 6s 20s (but the name may have blanks)


Prints the student Id, name, exam scores, and average for each student.


0 – normal

2 – bad input data, not enough good data values on a line


When bad data is encountered, the program terminates.


// If compiling using visual studio, tell the compiler not to give its warnings

// about the safety of scanf and printf


/* Include Files */

#include <stdio.h>

#include “cs1713p0.h”

int main(int argc, char *argv[])


Student student; // student structure for holding student data

FILE *pfileStudent; // FILE variable for student file

char szInputBuffer[MAX_LINE_SIZE + 1]; // input buffer for fgets

int iScanfCnt; // scanf returns the number of successful inputs

double dAverage; // grade average for a student

pfileStudent = stdin;

// Print a heading

printf(“%-10s %-20s %10s %10s %10s %10s\n”

, “ID”, “Name”, “Exam 1”, “Exam 2”, “Final”, “Average”);

// read data input lines of text until EOF. fgets returns NULL at EOF

while (fgets(szInputBuffer, MAX_LINE_SIZE, pfileStudent) != NULL)


// if the line is just a line feed, skip it.

if (szInputBuffer[0] == ‘\n’)


iScanfCnt = sscanf(szInputBuffer, “%lf %lf %lf %6s %20[^\n]\n”

, &student.dExam1

, &student.dExam2

, &student.dFinalExam

, student.szStudentIdNr

, student.szStudentFullNm);

// Check for bad input. scanf returns the number of valid conversions

if (iScanfCnt < 5)


printf(“invalid input when reading student data, only %d valid values. \n”

, iScanfCnt);

printf(“\tdata is %s\n”, szInputBuffer);



dAverage = (student.dExam1 + student.dExam2 + student.dFinalExam) / 3;

printf(“%-10s %-20s %10.2f %10.2f %10.2f %10.2f\n”

, student.szStudentIdNr

, student.szStudentFullNm

, student.dExam1

, student.dExam2

, student.dFinalExam

, dAverage);



return 0;


4. Copy the include file, cs1713p0.h into your cs1713 directory:

$ firefox

4.1 The firefox internet browser will open. Within firefox, go to my webpage:

4.2 Go to my Programming Assignments web page.

4.3 Right click on the cs1713p0_h.txt file. Save it into your cs1713 folder, renaming it cs1713p0.h

5. Compile your program, calling the executable examavg:

$ gcc -g -o examavg cs1713p0.c

where -g tells the compiler to generate information suitable for the debugger,

-o specifies the name of the result (in this case an executable)

This compiles cs1713p0.c, creating an intermediate compiled object file, and generates an executable named examavg. Until we have more complex assignments, we will use that form of the gcc.

If you have any compilation errors, the compiler will tell you the line number. Correct your mistakes using vi and repeat step 5.

6. Execute your program and provide data when prompted. Press the CTRL-D when there is no more input. (In Windows, use CTRL-Z when there is no more input.) Note: if your program goes into an infinite loop, press CTRL-C to terminate.

$ ./examavg

100 110 100 11111 Jean E Uss

50 0 40 22222 Marcus Absent

90 70 85 33333 Perry Scope

CTRL-D (CTRL-Z in Windows)

7. Create an input file for your program called examavgInput.txt using either vi or cat. Use the data from above.

8. Run your program again, but redirect the input from that file.

$ ./examavg <examavgInput.txt

9. Use the debugger.

9.1 Look at dddTips.docx under my setup web page. You will find it important to refer to that information while using ddd.

9.2 Invoke the ddd debugger. (Note that if you didn’t use the -g compiler switch, the debugger will not work. Also, if you are not executing from a Unix terminal window, ddd will probably not work.)

$ ddd ./examavg

You may want to show the line numbers for each statement. See dddTips.

9.3 You will see the ddd screen which has the following areas:

Menu Bar This is at the menu at the top of the window and includes File, Edit, View, etc.

Tool Bar This is immediately below the menu bar and contains some important tools like Find and Break.

Data Area At the beginning, this shows an empty grid area. It is used to show values of variables which you wish to closely examine.

Source File Area This displays your source code.

Command Tool Area This appears on the right side of the Source File Area and includes buttons:

Run run the program.

Step execute the current step. It will step into a called function.

Next execute the current step, but do not step into a called function.

Cont Continue execution until the next break or stdin.

Finish continue execution until the finish of the current function.

Kill Kill the execution of your program.

Console Area This is at the bottom of the screen:

  • debugger commands are shown/entered
  • standard input is entered (depending on preferences)
  • standard output is shown (depending on preferences)

9.4 Run your code using the Program menu item.

  • Click on Run in Execution Window to give you an execution window when you run. You only have to do this once.
  • Select Program > Run
  • Click the Run button in that Run Program dialogue window.
  • Since we are using stdin (and you didn’t redirect it in the run arguments within that dialogue window), the Execution Window that appears is used to enter the keyboard values. Enter a student’s data and then press Enter.
  • Your program should execute properly and then you can stop data entry by pressing CTRL-D.
  • Your program should run to completion unless there is a bug. If it didn’t work and you wish to terminate the program press the Kill button in the Command Tool Area.

9.5 Set a Breakpoint on the first printf statement. Click on that line and then press the Break button on the Tool Bar. A stop sign will appear on that line. This will cause the code to break execution so that you can examine what is happening at a point in the code. You can set many breakpoints.

9.6 Run your code using Program > Run. This time tell the Run dialogue window to use the input file.

For arguments, specify (you must type that less than symbol):

< examavgInput.txt

9.7 Execution should have stopped at the first break point. Notice that an arrow will appear in the Source File Area indicating the current statement to execute. In this section, you will step through your code using the Next button and examine variable values. You can hover the cursor over a variable to see its value.

9.8 Execute your program again. It is often necessary to see what is happening with your variables. You can display variables in the Data Area. Examining arrays is a little more difficult. Please see the dddTips for more information.

Step through your code using Next and see how the values in the Display Area change.

9.9 Another useful feature is to tell ddd to interrupt execution when a particular variable’s value changes.

Clear all your breakpoints (except the one on the first printf) by clicking on each breakpoint and then pressing Delete Breakpoint.

Run your program using the input file. It will stop at that first printf.

Insert a Watch point to watch for when the dAverage changes. Press Cont to continue execution. Notice that execution stops when that variable changes. This would also happen if a corresponding parameter changed its value.

When you are finished using ddd, type quit in the Debugger Console.

10. We want to save the output of your program by redirecting the output to p0Out.txt

$ ./examavg <examavgInput.txt >p0Out.txt

That redirected the stdin (standard input) to come from examavgInput.txt and stdout (standard output) to p1Out.txt.

To save the output when using Microsoft VS, see the instructions in setup.

11. Upload your results to blackboard.

How do I login to blackboard?  From the utsa main web page,

1.      On the menu at the top of the page, select: myUTSA > Blackboard Learn

2.       Enter your abc123 id.

3.       Enter your pass phrase password.

4.       Blackboard will show a list of courses.  Select CS1713  (but not the recitation)

5.       You will see some items listed on the left side that I provided.

Select Programming Assignments

Select Program 0 Upload

Follow the directions on the screen to upload multiple files. Upload cs1713p0.c and p0Out.txt