Section 1.1 Structure of a C++ program | |
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> int main () { cout << "Hello World!"; return 0; } | Hello World! |
The left side shows the source code for our first program, which we can name, for example, hiworld.cpp. The right side shows the result of the program once compiled and executed. The way to edit and 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 section compilers and the manual or help included with your compiler if you have doubts on how to compile a C++ console 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. We are going to take a look at them one by one:
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 within. The content of the main function follows immediately to its formal declaration enclosed between key-bracket signs ({}), as in our example.
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).
Therefore, you may have noticed 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's 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 <<), these last ones were all included within the block delimited by the key-bracket signs ({}) of the main function.
The program has been structured in different lines in order to be more readable, but it is not compulsory to do so. For example, instead of
we could have written:int main () { cout << " Hello World "; return 0; }
int main () { cout << " Hello World "; return 0; }in just one line and with exactly the same meaning.
In C++ the separation between instructions is specified with an ending semicolon (;) after them. 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> int 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:
int main () { cout << " Hello World! "; cout << " I'm to C++ program "; return 0; }We were also free to divide the code in more lines if we considered it convenient:
And the result would have been exactly the same than in the previous examples.int main () { cout << "Hello World!"; cout << "I'm a C++ program"; return 0; }
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.
C++ supports two ways to insert comments:
// line commentThe first of them - the line comment, discards everything from where the pair of slash signs (//) is found up to the end of that same line. The second one, the block comment, discards everything between the /* characters and the next appearance of the */ characters, with the possibility to include several lines.
/* block comment */
We are going to add comments to our second program:
/* my second program in C++ with more comments */ #include <iostream.h> int 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, very likely, it will show up one or several error messages.
© The C++ Resources Network, 2000-2001 - All rights reserved |
Previous: Main Menu |
index |
Next: 1-2. Variables. Data types. Constants. |
Additional readings:
ANSI-C++: Standard Header Files.