3)

Program 2 - Hello world again ..

Program 2 Hello World again .. Source code - prog2.c
This second program is not much different from the first, but it does contain more than one main line of program code. It combines three printf statements to produce a program which displays Hello World on the first line and This is my second program on the next line down. There are two purposes to this program, firstly, it shows how more than one statement can be used to create a program, and secondly it introduces a \n ( newline ) to the "text string", so that you have some control over the displayed program output.

The description of this program is long, and it touches on optimising programs so that they run faster, and also gives a long description of escape sequences in text strings. It is not important that you understand either of these fully but would be helpful later. The main thing at this stage is to understand why the program listing produces the output it does.

 

Program code - new parts shown in red

#include <stdio.h>

void main(void)
{

printf("Hello World\n");
printf("This is my second ");
printf("program.");

}

 

Description of the program code
There is not much to describe with this second program as it is very similar to the first. You should notice the framework of the program is identical to the first program, with the only differences being the code in between the { (open brace) and } ( closing brace ).

The first printf line : printf("Hello World\n"); is slightly different from the first program because the \n has been added to the end of the line. By combining the \ and the n character in the text string, this forces the program to display everything after this on the next line down. Therefore the text Hello World will be displayed on the first line, then the \n will jump the cursor down a line, and the following printf statements will display on that line. It is probably better to compile and run the program above to see the effect then look at the code again. A screen shot of the output is shown below to help :

Notice how the This is my second program. is all displayed on the same line even though it is made up from two different printf statements. This is because there is no \n at the end of the second printf line, so the cursor does not go down a line. That is also why a space has been included at the end of the second printf statement, if it wasn't there it would display as secondprogram.

There are better ways to write this program, it is only written this way to show how a newline is inserted into a text string and what effect it has on any printf statements that follow. The program below does exactly the same thing but all in a single printf statement !

#include <stdio.h>

void main(void)
{

printf("Hello World\nThis is my second program.");

}

Any program can always be written in a number of different ways to achieve the same result, as can be seen from the above two programs. There are literally hundreds of ways of writing this simple program using printf statements. In the worst case you could have a printf statement for every single charater ! The second program is probably the most optimal way of coding it, but is not as clearly "readable" as the first. A third more readable program could be used :

#include <stdio.h>

void main(void)
{

printf("Hello World\n");
printf("This is my second program.");

}

Again this produces exactly the same output as the first two programs, but the program code is a lot easier to understand and read as it is laid out in a way which resembles the output of the code. This is where the first big difference between game and an application programming comes in. A game programmer would typically code this program using the second method, whereas an application programmer would use the third method. The first method is only written like it is for learning purposes. Using a basic un-optimised C compiler the second program will execute the fastest, although with the speed programs run at the difference is only a few Milliseconds.

Therefore one of the differences between programming games and applications is that a games programmer will optimise a program to save every tiny Millisecond, whereas an application programmer will try to create programs that are more readable and easy to modify at a later time.

Optimising programs is an advanced topic for later, but as a general rule of thumb, but untrue in a lot of cases, a smaller program will run faster than a bigger one.

Going back to the \n inside the printf text strings, this is called an escape sequence and as can be seen it isn't displayed on the screen. There are a few more escape sequences that can be used, they all start with a \ in the text string. A list of some of the more commonly used ones is shown below :

Escape sequence Description
\a Bell - beeps from the PC speaker
\n Newline - as described
\t Horizontal tab
\' Single quotation mark
\" Double quotation mark
\\ Backslash
\0 NULL character

This table is only included for reference, at the moment, with a couple explained in more detail below ( \" and \\ ).

You may be wondering reading the above how you display a quotation mark " using a printf statement. Think about it slowly and logically, and you'll discover that if you include a quotation mark in the string, for example printf("This is a " - quotation mark"); then the code won't compile. The reason being that the compiler reads each line from left to right, and when it meets up with the second quotation mark it thinks that it's the end of the string and throws up an error. Therefore the way you would code this is printf("This is a \" - quotation mark");

By inserting the \" escape sequence, it will tell the compiler not to treat the quotation mark as the end of what you want to print, but as a character in the text string.

The same is true for displaying backslash characters in a string \. When the compiler meets up with a backslash in a string it assumes that it is part of an escape sequence. By placing two backslashes together in a text string, it will know that you are intending it to display one backslash on the output.

 

Summary
Considering that was a small program a lot of things have been touched upon on this page. If you do not fully understand the parts about the escape sequences in text strings it is not important, but try to read through the page slowly again, and even try to use some of the escape sequences in the list to see what happens. You do not need to memorise them, I didn't, I just opened up a help file to refresh my memory to create the list.

This page also showed how the same program can be written in a number of different ways. For such a small program it is amazing how many ways there are of writing a program to do the same thing. As programs get larger the number of ways of acheiving the same output is increased, hence why there will never be a shortage of programs to write or finding better ways to write them. The differences in programming style between games and application programmers was also covered, using the different programs as examples. A games programmer will typically create a program the same as an application programmer, so that it is readable, and then optimise it later to achieve better and faster performance.

There are a couple of tasks below to try, and the first task at least should be done before moving onto the next program which introduces variables and format specifiers. The second task is harder, but if you can do it, then go for it, if not, don't worry about it for now.

 

Tasks

2.1) Write a program using a little or as many printf statements as you like to display your name and address on the screen as is seen on an envelope.

2.2) Modify the program above to put a blank line between your name and the first line of your address, also try to display your name in quotation marks. As below :

"Jason Spooner"

The Manse,
Ulsta,
West Yell,
Shetland Islands.
ZE2 9BG

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001