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.
Start --> All Programs --> Utilities --> XWin32 2010 --> X-Win32 2010XWin32 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:
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.
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.
/* 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.
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.)
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.
Fix the error by putting the 0 back in, and recompile.
See you next week!