Before starting this week's exercises, be sure you have completed all those from last week. If you have had problems with any of last weeks exercises, make sure you get help with them from a demonstrator or practical supervisor at this week's practical.
Your programming environment is based on an application called Cygwin Bash Shell (or Cygwin), which is provided by Cygnus Solutions. Cygwin is a set of software tools which offers a UNIX (a well-known operating system which has for many years been used on mainframe computers) command-line environment, emulated on your overall Windows desktop.
Before you get started on using Cygwin and its tools,
you have to go to the Start button, and then follow
the path:
All Programs->EEE->Cygwin->Cygwin Bash Shell
Alternatively, if the computer that you are using does not have this menu entry,
you can double-click on the file D:\cygwin\cygwin.bat.
This opens a new window which looks like this:
Note: you will need to start this window everytime you log onto a PC and wish to use Cygwin.
In order to witness the above-mentioned transparency do the following:
Clear understanding of this transperency is a key to success in your subsequent mastering of the C programming environment.
Do not panic, you don't have to write a C program yet (a little difficult at this stage of your module!), but you are going to compile and execute a program that has already been provided. This will give you some experience of using the Cygwin environment before you have to cope with writing your own programs.
To obtain your first C program that you can experiment with:
Having clicked on this link you will see the C code displayed in your Internet Explorer window. You now need to save the contents of this Window in a text file, called hello.c in a special folder dedicated for your first C program.
Go to the H:\EEE1004 folder using Windows Explorer. In that folder, create a new folder under name hello.
Go back to the Internet Explorer window. Click on File and Save As. In the "Save Web Page" dialogue box, first, inside the "Save in" section reach the newly created folder hello, and click on it. Then, in the "File name" section of the "Save Web Page" dialogue box, type hello, and in the "Save as type" section select Text file. Finally, click on the Save button.
Has it worked? Go into the Explorer window and see if your hello folder contains file hello.c. If the file name is different (such as hello.c.txt), rename the file to hello.c.
Now make sure that your Cygwin Bash Shell window is open.
Check your current directory by typing command pwd. First, make sure that your current directory is /cygdrive/h/EEE1004. If not, use command cd as you did in the previous exercise. Now, type command ls and see if your current directory contains the name of your new directory hello.
Change your current directory to hello by typing command cd hello. Again, examine the contents of this directory by typing command ls. See if the file named hello.c is there.
At this point you have reached full correspondence (transparency) between your Explorer and your Cygwin interfaces.
You are now ready to compile your first program.
Type the following command:
gcc hello.c
(here gcc is
the name of a "standard" C compiler in the
Cygwin environment.
Once the bash $ prompt appears back the compilation process is finished and object code (which is in this case also executable code!) has been generated. The executable code has been stored in a file called a.exe. See if this file has appeared in your hello folder, by both looking in your Explorer window and typing ls command in the Cygwin window.
If the a.exe file is there, the Hello program can now be run by typing command ./a.exe in the Cygwin window.
When your program runs it must print the following two
lines:
Hello Your name
Welcome to EEE1004
in the Cygwin window.
Congratulations! You've just compiled and run your first C program.
In this exercise, you are going to change the C program to personalise it. To edit your program you need a convenient text editor. While there is nothing wrong in using the Notepad editor, which is the one automatically invoked when you double click on the hello.c icon in the hello folder, we recommend you use another editor, called the Programmers File Editor. We believe this editor is more user-friendly than Notepad.
When you have finished, use the File menu to exit the Programmers File Editor. Then type command exit in the Cygwin window.
Try to experiment with another C program, called newhello.c, which is a bit more complex version of a greeting program.
This program, as you can see, includes a definition of an object, called name.Save this code in a file newhello.c in the same folder/directory hello.
First, use command gcc newhello.c to compile it. Run the new program with a.exe and see how it works. It should involve you in a dialogue.
Before logging out, make sure that you exit the Cygwin Bash Shell and all other applications.