กก

Section 1.1
Structure of a C++ program
cplusplus.com

Probably the best way to start learning a programming language is with a program. So here it is our first program:

// my first program in C++

#include <iostream.h>

main ()
{
  cout << "Hello World!";
  return 0;
}
Hello World!

The left side shows the source code for our first program, which we can assign any filename, for example hiworld.cpp. The right side shows the result of the program once compiled and executed. The way to compile a program depends on the compiler you are using. Depending on if it has a Development Interface or not and on its version. Consult the section compilers and the manual or help included with your compiler if you have doubts on how to compile a C++ program.

The previous program is the first program that most programming apprentices write for the first time, and its result is the printing on screen of the "Hello World" sentence. It is one of the simpler programs that can be written in C++, but it already includes the basic components that every C++ program has. Take a look at them one by one:

// my first program in C++
This is a comment line. All the lines that begin by two slash signs (//) are considered comments and they do not have any effect in the program's behavior. They can be used for that the programmer include explanations or observations within the program's source. In this case, the line is a brief description on what our program does.

#include <iostream.h>
Sentences that begin by a pound sign (#) are directives for the preprocessor. They are not executable code lines but indications for the compiler. In this case the sentence #include <iostream.h> tells the compiler preprocessor to include the iostream library. This is the basic standard input-output library in C++, and it has to be included because it is used later in the program. This is the classic way to include the iostream C++ library.

main ()
This line corresponds to the beginning of the main function declaration. The main function is the point by where all C++ programs begin their execution. It is independent from where it is (at the beginning, end or by the middle of the code) but its content is always the first to be executed when a program starts. In addition, for that same reason, it is essential that always exist a main function in each C++ program.

main goes followed by a pair of parenthesis () because it is a function. In C++ all functions are followed by a pair of parenthesis () that, optionally, can include arguments. The content of the main function follows immediately to its formal declaration enclosed between key-bracket signs ({}), like in our example.

cout << "Hello World";
This instruction does the most important thing in this program. cout is the standard output stream in C++ (the screen) as defined in the iostream library and what this sentence does is to send the string of characters "Hello World" to this stream (the screen).

Notice that the sentence ends with a semicolon character (;). This character has the meaning of finishing the instruction and must be included after every instruction you include in any C++ program (one of the most common errors of C++ programmers is indeed to forget to include a semicolon ; at the end of each instruction).

return 0;
The return instruction makes the main() function to finish and return the code that the instruction is followed by, in this case 0. This it is the normal termination of a program that has not found any errors during its execution. As you will see in coming examples, this way is the most usual to terminate a C++ program.

Therefore, you may notice that not all the lines of this program did an action. There were lines containing only comments (those beginning by //), lines with instructions for the compiler preprocessor (those beginning by #), then there were lines that initiated the declaration of a function (in this case, the main function) and, finally lines with instructions (like the call to cout <<) that were included within key-bracket signs ({}) in the main function.

The program has been structured in different lines for that it become more legible, but it is not compulsory to do so. For example, instead of

main ()
{
  cout << " Hello World ";
  return 0;
}
we could have written:
main () { cout << " Hello World "; return 0; }
with exactly the same meaning.

In C++ the separation between instructions is specified with an ending semicolon (;). The division of code in different lines only serves for that it can become more legible and schematic for the humans that read it.

Here is a program with more instructions:

// my second program in C++

#include <iostream.h>

main ()
{
  cout << "Hello World! ";
  cout << "I'm a C++ program";
  return 0;
}
Hello World! I'm a C++ program

In this case we used the cout << method twice in two different instructions. Once again, the separation in different lines of the code has just been done to give greater readability to the program, since main could perfectly have been defined thus:

main () { cout << " Hello World! "; cout << " I'm to C++ program "; return 0; }
Also we are free to divide the code in more lines if we consider it convenient:
main ()
{
  cout <<
    "Hello World!";
  cout
    << "I'm a C++ program";
  return 0;
}
And the result would be exactly the same one than in the previous examples.

Preprocessor directives (those that begin by #) are out of this rule since they are not true instructions. They are lines read and discarded by the preprocessor that finaly do not produce any code. These must be specified in their own line and do not require to include a semicolon (;) at the end.

Comments.

Comments are pieces of source code discarded from the code by the compiler. They only serve for that the programmer can put notes or descriptions on parts of a program.

C++ supports two ways to mark comments:

// line comment
/* block comment */
First of them: the line comment, discards the code from the pair of slashes (//) until the end of the same line. The second one, the block comment, starts at the first /* characters and ends when the first */ characters are reached, with the possibility to include several lines.

We are going to add comments to our second program:

/* my second program in C++
   with more comments */

#include <iostream.h>

main ()
{
  cout << "Hello World! ";     // says Hello World!
  cout << "I'm a C++ program"; // says I'm a C++ program
  return 0;
}
Hello World! I'm a C++ program

If you include comments within the sourcecode of your programs without using the comment characters combinations //, /* nor */, the compiler will take them as if they were C++ instructions and, more likely, it will show up one or several error messages.

© The C++ Resources Network, 2000 - All rights reserved

Previous:
Main Menu

index
Next:
1-2. Variables. Data types. Constants.

Additional readings:
ANSI-C++: Standard Header Files.