Exercise 1: Hello World! (60%)




Document: Software Engineering 1: Lab Exercises

next Exercise 2: Branching out! (40%)
up Session 3: Week 6/7: Introducing C
gif Background

Exercise 1: Hello World! (60%)

Here is the minimal "Hello World" program already discussed in the lectures:

  #include <safe-c.h>

  void main(void)
  {
    put_string("Hello World!\n");
  }

Use PFE to create a file called hello.c in the directory D:softeng1 containing the program listed above.

Start up the MS-DOS Prompt application. MS-DOS Prompt (or DOS for short) is a command line interpreter. That is, it allows you to type in textual commands which it then tries to carry out. This is an alternative style of user interaction, compared to the "graphical" style of using the mouse to point and click in order to get things to happen. This textual type interaction will be more convenient for many of the interactions involved in developing C programs.

On some PCs in the CAE lab DOS is configured to run "full-screen". This means you can no longer see the windows for other applications such as Netscape and PFE. This is quite inconvenient. If this happens, press Alt-Enter (i.e. hold down the Alt key and press Enter). This will make DOS run in a window instead.

Under MS-DOS Prompt you will have, at any given time, a "default disk drive" and "default directory" on that drive. This simply means that if you refer to a file, without explicitly stating what disk drive or directory it is located in, then DOS will assume it is on the default drive and in the default directory. The DOS prompt will indicate the current default drive and directory. We are going to be working with files on drive D: and in directory softeng1, so it will be convenient to make these the defaults.

To change the default drive, simply type in the desired drive letter followed by a colon, and press Enter. The prompt should change to reflect the new default. Try changing to drive A: and back to drive D: now to see this happened.

To change the default directory (on the default drive) use the cd command (short for Change Directory):

    cd \tmp

Note the leading backslash character here. This character is used to separate drive specifiers from directory names, and directory names from subdirectory or file names etc. In this particular case, the signals that tmp is in the top-level or root directory of the current drive. If the were omitted, DOS would think that tmp was intended as the name of a sub-directory of the current directory (whatever that might be).

Experiment with changing the current directory to make sure that you understand its operation fully. You can only change to directories that exist. You can, of course, check what directories (and files) exist using File Manager. Alternatively you can use the dir command in DOS. You can get help on DOS commands by using the help command. Its operation is pretty self explanatory. Experiment with it now.

Note that case is not significant in DOS command, directory, file, or drive names - so, for example, CD has the same effect as cd etc.

Make D:softeng1 your current drive and directory. Use dir to check that the file hello.c is present there. You can use the command type in DOS to type the contents of a text file on the screen. Try this now to check that hello.c does indeed contain what you expect.

Compile your program by giving the command:

  scc hello

scc is part of the safe-c package. It invokes the C compiler in the appropriate way to ensure that the safe-c components get automatically combined with your program.

The program should compile without any problems - if you have entered it correctly. But if any problems are encountered try to figure out what is going wrong and fix them. If you get stuck, seek help from the demonstrator.

Check (using File Manager or the dir command in DOS) what files now exist in your directory. There should be a new file called hello.exe. This is the executable file - the file containing the binary instruction codes that the CPU can directly execute. Make a note of the size of this file in your report.

To execute the program under DOS you simply give its name as a command:

    hello.exe

In fact, it is enough to just say hello as the command name - DOS will automatically search for a file with that name and the .exe extension.

Check that the file executes as you expected. If it does not, try to figure out why, and fix it. Again, if you get stuck, ask for help from the demonstrator.

If all has executed correctly, then congratulations - you have just entered, compiled, and tested your first C program.

Make a directory called lab3 in the root directory of your diskette. Copy the file hello.c into this directory. There is no need to copy the file hello.exe onto your diskette - explain why in your report.




Document: Software Engineering 1: Lab Exercises

next Exercise 2: Branching out! (40%)
up Session 3: Week 6/7: Introducing C
gif Background



McMullin@eeng.dcu.ie
Tue Apr 30 14:15:37 GMT 1996