CSCI 1300 Lab Notes for Tuesday, Aug 31.
1. Create, compile and run a program that displays your name on the computer screen.
2. Develop some understanding of what is happening inside the computer as you do this.
What to do: team up with one or two other students and work through the following steps. Make sure everybody understands the descriptions of what is happening that are included: DISCUSS what it says. Ask me if something is not clear, or if you are curious about any aspect of it. Choose one person to do the actual typing. When they have finished the work, change roles and have someone else do it. Continue until everybody can work through this.
Step 1. Create a folder in which to store your program.
Do this by double clicking My Computer, double clicking Scratch(D:) (this is a collection of bits on a disk, available for you to change) and choosing File >new >folder. Name the new folder after yourself.
What’s happening. The computer has a vast collection of bits, organized into blocks called files. Files are organized into groups called folders. In order to tell the computer what block of bits to use, you assign names to files, and to folders, and the computer keeps track of what bits are in each file and folder.
Step 2. Write your program, as follows:
Select the Notepad program by choosing start > programs > accessories > notepad.
What’s happening. As you know, programs are collections of opcodes, represented by bits. You are telling the computer that you want to run the particular block of bits that represent the Notepad program.
Type or copy-paste the following text into Notepad:
printf("My name is Fran Allen\n");
Use Notepad to change the name “Fran Allen” to your name (you can use Google to find out who Fran Allen is.)
What’s happening. Notepad is an editor program. That means it is a program that detects what keys you are pressing on the keyboard, and stores numbers representing the corresponding letters, punctuation, etc, in the computer’s memory. It also detects other things you do, like moving the mouse and clicking the mouse buttons, and uses this information to figure out where in the collection of letters something you are typing should go, so that you can make changes and corrections.
What do those pieces of program mean? Don’t worry about this right now. The heart of this program is the line printf(“My name is Fran Allen\n”); which says you want your program to display the letters F r a n, and so on, on the screen, and then prepare to display anything further on a new line (the n in \n stands for new line. The “print” part of printf is an anachronism dating from the days before most computers had displays, and you had to print stuff on a printer to see it.
Carefully save your program in the folder named after yourself, giving it the name myprogram.c. The Save operation is on the File menu.
Things to watch are, are you saving the program into the right folder?
DON’T save it as a text file... this will result in its being named
myprogram.c.txt, rather than myprogram.c. To avoid this select “All
Files” rather than Text Documents (*.txt) in the “Save as type:” portion of the Save As dialog box.
What’s happening. Notepad has collected the letters you typed in memory. In order to find them again later, you need to put them into a file and give the file a name. What you are doing asks Notepad to create a file with a name you specify, and put that file into the folder you created earlier. Unfortunately Notepad tries to be “clever” in assigning the name to your file: it will add .txt to the name you supply, if you don’t prevent it from doing this. The characters .txt are what is called an extension: a little tag added to a file name to tell programs that use the file what kind of information is in the file. The extension .c, which is what we want, tells programs that this file contains statements in the C programming language. Notepad assumes you are creating a text file, which is any old collection of characters, and that’s what the .txt extension means.
Step 3: Get a command prompt, by selecting Start > Programs > Accessories > Command Prompt
What’s happening. You are choosing another program you want to run. This one is a program that lets you type commands that specify operations for the computer to perform.
Step 4: Set up the CS1300 software for use. Do this as follows:
type H: and press Enter.
What’s happening. You are telling the computer you want to use material on the “H drive”, which is a collection of bits your computer can access over the Internet. It contains the programs we’ll use in this course.
type gocs and press Enter.
What’s happening. Unfortunately, your computer cannot find all of the programs on the H drive without help. The command gocs gives the computer some information about where to look for programs, that enables it to find them.
type D: and press Enter.
What’s happening. “D:” is the name of the Scratch collection of bits where you put your folder. You are telling the computer you want to work with that collection of bits.
type cd yourname and press Enter, where yourname is the name you used for the folder you created earlier
What’s happening. You are now telling the computer you want to work with material in the folder where you put your program. The command cd stands for “change directory”, and “directory” is another word for folder.
type gcc myprogram.c -Wall –o myopcodes and press Enter.
What’s happening. The command gcc tells the computer you want to run a program called the Gnu C Compiler. You also are saying that you want this program to work on your program file, that it should give you warnings on anything in your program that might not be right (that's what –Wall says to do...W stands for "warnings" and "all" means "all") and that it should put the opcodes it produces in a file called opcodes (the –o before opcodes stands for object code, which is a jargon term for a collection of opcodes.) So this command makes the compiler translate your C program into opcodes, and save them in a file.
Correct any errors.
If all is well the computer will respond with a blank line followed by D:\yourname>, which is a prompt telling you that you are working with the folder yourname that is part of the Scratch collection of bits. If you get anything else you probably mistyped something in Notepad. Go back to Notepad, correct any typos, and save again. Then try the gcc command as above again.
type myopcodes and press Enter
What’s happening. By typing the name of your collection of opcodes, you are telling the computer to run them.
You should see your name displayed on the screen.
What’s happening. The opcodes are run, and they make your name appear in glowing phosphor dots (or little dots made with liquid crystal) on the screen.
Step 5. Mail your program to yourself. Use a mail program you are comfortable with, and send a message to yourself, attaching your program file to it. Your program will then be available to you in your email.
What’s happening. The email program sends a collection of bits, including your C program, over the Internet to a computer that handles your email, where the bits are stored in a file. When you use a program to read your mail, later, it will find that file.