Lab 1
Editing, Compiling, and Executing in the Unix environment; Data Types; Using turnin

Objectives

Note: don't worry if you can't finish the entire lab exercise. Use turnin (see step 15) to turn in as much as you've completed before you leave the lab. Make sure you finish the rest of the lab on your own time.

What you should do...

  1. Sign the attendance sheet.

    Working with Unix

  2. If XWin32 is running on your computer, there will be a blue-X/monitor icon in the system tray at the lower right hand corner of your screen. If XWin32 is not running you must start it on your computer:
    click Start --> All Programs --> Utilities --> XWin32 8.0 --> XWin32
    
    XWin32 is a terminal application for Windows computers, which allows Windows users to connect to Unix servers on a local network or via the Internet. X applications running on those servers will be displayed onto the Windows desktop. There are two reasons why we run XWin32 when connecting to Unix:

  3. Log onto the Unix system using your CCC username and password:

  4. Create a directory called cs2301 and make it your working directory. You can use the Unix shell commands: mkdir cs2301 (make directory) and cd cs2301 (change directory). A directory is like a folder; you can organize your files by storing them in different directories. Try the command pwd (print working directory). Now go back to your "home" directory by typing cd .. (the ".." means "move up one level in the directory structure"). Type pwd again. Finally, make cs2301 your working directory again by typing cd cs2301 one more time.

  5. Use emacs to create a C source file called lab1.c. The command to start emacs in its own window is emacs lab1.c& (it is the & that signifies that emacs is to be run "in the background"). After you enter this command, you will have two windows open; we'll call them the Unix window and the emacs window.

    Note that C source files cannot be created with word-processing applications (like Microsoft Word) that embed formatting information in the files. You must use an editor like emacs (or pico) that produces straight ASCII text files.

  6. Click in the emacs window and enter the following program (you could copy/paste, but typing it in will get you used to C syntax):
    /* Lab 1
       Data types
     */
    
    #include <stdio.h>
    
    int main()
    {
      /* declarations */
      int int1, int2, int3;
      float f1=1.0, f2=2.5, f3=5.0, f4;
      double d1, d2;
      char ch1, ch2;
    
      printf ("float f1=%f, f2=%f, f3=%f\n", f1, f2, f3);
    
      int1 = 5;
      d2 = 5.0;
      ch1 = '5';
      printf ("int int1=%d, double d2=%f, char ch1=%c\n", int1, d2, ch1);
    
      int3 = f2;
      int2 = int1/int3;
      f4 = int1/int3;
      printf ("int int3=%d, int2=%d, float f4=%f\n", int3, int2, f4);
    
      d1 = f3 - -f1 * 6 / int3 + 8.0 * (int1 - d2);
      printf ("double d1=%f\n", d1);
    
      ch2 = ch1 - 2;
      int3 = 'a' - 'A';
      ch1 = 'W' + int3;
      printf ("char ch2=%c, ch1=%c, int3=%d\n", ch2, ch1, int3);
    
      printf ("enter an integer between 65 and 90:  ");
      scanf ("%d", &int3);
      printf ("that number is the ASCII code for the character %c\n", int3);
    
      return 0;
    }
    
    Save the file.

Understanding Data Types

  1. Before running the program, see if you can predict the program output by tracing through the program by hand.

  2. In the Unix window, compile the program with the command gcc -Wall lab1.c (the -Wall instructs the gcc compiler to display all Warning messages). If the compiler reports any error or warning messages, make sure your file looks exactly like the program given above. If you find any discrepancies, make changes and recompile. When you can compile with no errors, run the program with the Unix command ./a.out (This is a strange-looking command. When Unix creates an executable file it names it a.out. The ./ preceeding a.out tells Unix to look for the a.out file in the working directory (the "." means "the working directory")). If any of the values output by the program disagree with the values you predicted in step 7, see if you can figure out why. (Ask a classmate or a TA if you get stuck.)

Compilation Errors

  1. Click in the emacs window. Edit the lab1.c file by deleting the semicolon character at the end of the line char ch1, ch2;. Save the file, then compile it. You should see a compilation error message that looks something like this:
    lab1.c:  In function 'main':
    lab1.c:15: error: parse error before "printf"
    
    The 15 refers to line 15 in your source file (if you go to the emacs window and click on the first printf statement in the program, you will see L15 displayed at the bottom of the window, telling you the cursor is on line 15). Line 15 is actually syntactically correct. The error occurs a couple of lines above, but it wasn't until the compiler started working on line 15 that it detected a problem. This happens frequently. If the compiler tells you it found an error on a particular line, but you are sure that there are no errors on that line, then search for the error on the lines above the flagged line (start with the line immediately preceeding the flagged line and work backwards). Put the semicolon back where it belongs and recompile.

  2. Introduce another error by deleting the 0 from return 0; This time, compile with the command gcc lab1.c. No compilation errors are displayed. Compile again, this time with Warning messages turned on (gcc -Wall lab1.c). Now you'll see a warning. The error you saw in step 9 was a fatal compilation error - that is, the compiler couldn't translate your program into executable code. Warning messages, on the other hand, are not fatal - an executable program is created and you can run your program. A warning is displayed when some part of your code is syntactically correct, but contains a potential error in logic. Warning messages should always be taken seriously. If you get a warning message, figure out why and make changes. Fix the error by putting the 0 back in, and recompile.

  3. This time, take the line ch1 = 'W' + int3; and switch it around to 'W' + int3 = ch1; When you compile, you'll get a fatal error that says something about an "invalid lvalue". This is the compiler's way of telling you that the left-hand-side of an assignment statement must be a variable, not an expression or a constant. Fix the error and recompile.

  4. Remove the & from the scanf statement. Compile (you'll get a warning if you compile with -Wall, no error message at all if you compile without warnings). Run the program. What happens when you enter the input value? "Segmentation fault" can happen for a variety of reasons, which we'll discuss in detail later. For now, if you get a segmentation fault, check each of your scanf statements for missing &'s. Fix the error and recompile.

  5. On the third line, remove the * before the /. Compile. Whew! Sometimes a minor syntax error can generate an awful lot of compilation errors. If you get a screenful of compilation errors, start at the beginning of the list and try to fix the first one reported. Often, fixing one error results in many subsequent "errors" being fixed. Put the * back in and recompile.

  6. If you have time left, introduce new errors on your own (for example, take out a quotation mark, mis-spell a variable name, leave out a parenthesis, etc.). The more familiar you become with compilation error messages now, the easier time you'll have debugging programs in the future.

  7. Turn in your files using the Unix turnin program (click here if you are unfamiliar with the Unix version of turnin). The Unix command you should use to submit your files is
    /cs/bin/turnin submit cs2301 lab1 lab1.c 
    

See you next week!