2)

Program 1 - Hello world

Program 1 Hello World Source code - prog1.c
Traditionally when learning a new programming language it is customary to write a small program to display "Hello World" on the screen. This serves to show the basic structure of a program, as well as show how to enter, compile and execute a program. The program code is shown below that can be typed straight into Miracle-C.

Typing each program in is strongly recommended for the first few programs as it will get you used to the editor and syntax of the C language. Alternatively, you can download the source code for the program from the link above ( right ).

Instructions on how to enter the program and a complete description of it can be found in the bottom panel below ( Entering, compiling and running programs )

 

Program code

#include <stdio.h>

void main(void)
{
printf("Hello World");
}

 

Description of the program code
The first line #include <stdio.h> will be the same for most of the programs that are written. What it does is include a "header file" into the program that enables the program to usee commands for displaying text on a DOS screen. A description of header files at this point would probably be a bit mind blowing so at this point it should probably be taken as red that it has to be included. A detailed description of what header files are is further down the line, but for now just type it in and don't think to much about it. Incidentally, with the Miracle-C compiler, the line doesn't need to be there, but for any other C compiler it is required.

The line void main(void) is required in all C programs, and it signals where each program starts from. Programs run serially from top to bottom, ie, they execute the first line, then the line beneath that and so on.. starting at the void main(void) line and finishing at the last line in the program.

The { ( open brace / "curly bracket" ) on the line below the void main(void) line is used to open or group a "block" of program code. Each open curly bracket should have a corresponding closing curly bracket } somewhere below, a quick glance to the last line of the program shows this. More will follow on the use of curly brackets to define blocks of program code later. In short they define portions of program code, every line of code between a { and a } can be thought of as an individual block of code.

Finally, the part of the program that is the most interesting, the printf("Hello World"); line. This simply displays the text Hello World on the screen. The printf command is an extremely powerful command used for displaying text on a DOS based screen. Here it is being used in it's simplest form to display text, but as you will see later it can do a lot more than this. An open bracket always follows printf followed by an opening speechmark. Everything in between the opening quotation mark(") and the closing quotation mark (") is displayed on the screen. Finally the format of the line finishes with a closing bracket and a semi-colon. It is important to include the semi-colon at the end of a printf line, otherwise the program will not compile. This is one of the most common mistakes made when starting programming in C.

 

Entering, compiling and running programs
To start Miracle-C use the Start button in Windows and select Programs -> Miracle C -> Miracle C Workbench. The compiler will now run. Select the File -> New menu option to create a new 'untitled' program. In the window that appears type in the code above exactly as shown. Once it is all typed in, choose the File -> Save menu option and enter the file name prog1.c , to save the source code program to disk. This is all pretty standard windows "stuff", but it has been explained for fullness.

When it's all been typed in and saved it is ready to be compiled, as explained on the previous page, this turns the source code into machine code. To compile the source code press the F7 key on the keyboard with the source code window active. A new output window will appear ( as well as a Miracle-c registration reminder ). Click the ok button or press enter to clear the registration reminder, and if all has been typed in correctly you will see the following appear in the output window :

Miracle C Compiler (r3.2), written by bts.
Compiling c:\program files\miracle c\prog1.c
main

0% cg space used
Time taken: 0.06 seconds

Linking object c:\program files\miracle c\prog1.obj,,c:\program files\miracle c\prog1.map,c:\Program Files\Miracle C\ccl.lib ;

Build completed

If the last line in the output window does not say Build completed then you have probably made a typing mistake when entering the code above. Re-check the code and press F7 again when the error has been found and corrected. This will re-compile the source code again, and hopefully it will result in a build completed message at the end.

Note 1 : If when you compile a program the output window displays aborting compile at the end, this means that the compiler cannot turn your program code into machine code because of a syntax error. A vague clue will be given on the line above "aborting compile" as to what the error is. This will help you find the error and correct it, although the error messages with compilers are very vague and cryptic.

Note 2 : When using Miracle-C you may want to "tile" the source and output windows on the screen. To do this choose the Window -> Tile menu option, so that the two windows don't overlap.

After successfully compiling the program, the compiler will have generated a prog1.exe file. This file is the one that can be exectuted to run the program or distributed to someone else to run on there computer. You can run the program from Miracle-C directly by pressing the F8 key and clicking the run button on the dialog that appears. The program will now execute and display a DOS prompt box with Hello World displayed in it - not very exciting, but a first working program nonetheless.

You can close the DOS box and edit the program further by altering the source code if you wish, but each time you want to run the program you have to compile it again with F7 and then run it with F8.

 

Summary
Even though this is a very simple program, it is probably the most important program to understand of all. It shows the basic format of a C program, which you can use over and over for all the programs you write :

#include <stdio.h>

void main(void)
{

Put lines of program code here

}

A lot has been skipped over in the description of the program, but don't let that put you off, it will all become more apparent later as the sample programs introduce more features of C. What you should be able to do now at least, is to use the sample framework above to start to write any program, by replacing the "Put lines of program code here" with lines of code relevant to the program you are writing. It's not a crime to copy and paste the above framework into each program you write, it certainly will save some typing :)

One thing that you should start to notice is the "symmetry" of C programs. Eg. Each open curly bracet { has a corresponding closing curly bracket }, each opening bracket ( has a closing bracket ), each quotation mark " has a closing quotation mark " ( the same key with opening and closing quotation marks ). Also in the header file an opening angle bracket < is followed by a closing angle bracket >.

The only C command that has been looked at in this first program is the printf command which is used to display text on a DOS based screen. As mentioned in the desciption, the command was used in its simplest form, but there are lots of other nifty things that the printf command can be used for. The next program goes on to look at some of the features of the printf command and also shows a program with more than one line of code ! Before moving on though, make sure you can do all the tasks below, there is no rush ..

 

Tasks

1.1) Modify the above program so that is displays Hello Universe on the screen, compile and run it to check it works.

1.2) Write a new seperate program called myprog1.c that displays your name, save, compile and run it to check it works.

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001