Lab 1
Editing, Compiling, and Executing in a Linux 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 Linux

  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 by clicking:
    Start --> All Programs --> Utilities --> XWin32 2010 --> X-Win32 2010
    
    XWin32 is a terminal application for Windows computers, which allows Windows users to connect to Linux 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 Linux:

  3. Log onto the Linux system using your CCC username and password: You should see the X-Win configuration window on your desktop. In the panel labeled "Connections" select ccc.wpi.edu, and click "Launch". In the following window, provide your user name and password and click "Continue". This will open a new window on your desktop running the Linux command shell.

  4. Create a directory called cs2301 and make it your working directory. You can use the following Linux shell commands:

    mkdir cs2301 (make directory) and

    cd cs2301 (change directory).

    A directory in Linux is like a folder in Windows; 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, kwrite, or another editor to create a C source file called lab1.c. The command to start emacs in its own window is

    emacs lab1.c &

    while the command to start kwrite in its own window is

    kwrite lab1.c &

    The & at the ends of these commands signifies that the preceding command is to be run independently of the Linux shell. After you enter the command, you will have two windows open; we'll call them the Linux shell window and the editor 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 kwrite) that produces straight ASCII text files.

  6. Click in your editor window and enter the following program (you could copy and paste, but typing it in will help you get used to C syntax):
    /* Lab 1 -- type your name here
       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;
    }
    
    Be sure that there is a newline character (i.e., a carriage return) after the last curly bracket. 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 Linux shell, compile the program with the command

    gcc -Wall lab1.c

    The -Wall "switch" 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 Linux command

    ./a.out

    This is a strange-looking command. When Linux creates an executable file it names it a.out unless you explicitly give it another name. The ./ preceeding a.out tells Linux to look for the a.out file in the working directory (the "." means "the working directory" - i.e. the one that you most recently changed to using the cd or other command). Otherwise, Linux will just look in its standard list of places for the command and complain if it cannot find it. By default, your working directory is not included in the standard list of places.

    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 editor 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 (emacs displays the current line number at the bottom of the window; kwrite lets you find or go to a line number using the Edit --> goto menu command). In this case, 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. In this course, all submitted programs must compile without generating warnings.

    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 web-based turnin program. You should have already received an email from turnin containing your initial turnin password. If you are unfamiliar with turnin, ask a TA for help.

See you next week!