05.structure of A Program
05.structure of A Program
05.structure of A Program
The best way to learn a programming language is by writing programs. Typically, the
first program beginners write is a program called "Hello World", which simply
prints "Hello World" to your computer screen. Although it is very simple, it
contains all the fundamental components C++ programs have:
7
[demo]
int main()
{
std::cout << "Hello World!";
}
[/demo]
Hello World!
Edit & Run
The left panel above shows the C++ code for this program. The right panel shows the
result when the program is executed by a computer. The grey numbers to the left of
the panels are line numbers to make discussing programs and researching errors
easier. They are not part of the program.
The function named main is a special function in all C++ programs; it is the
function called when the program is run. The execution of all C++ programs begins
with the main function, regardless of where the function is actually located within
the code.
This statement has three parts: First, std::cout, which identifies the standard
character output device (usually, this is the computer screen). Second, the
insertion operator (<<), which indicates that what follows is inserted into
std::cout. Finally, a sentence within quotes ("Hello world!"), is the content
inserted into the standard output.
Notice that the statement ends with a semicolon (;). This character marks the end
of the statement, just as the period ends a sentence in English. All C++ statements
must end with a semicolon character. One of the most common syntax errors in C++ is
forgetting to end a statement with a semicolon.
You may have noticed that not all the lines of this program perform actions when
the code is executed. There is a line containing a comment (beginning with "//").
There is a line with a directive for the preprocessor (beginning with #). There is
a line that defines a function (in this case, the main function). And, finally, a
line with a statements ending with a semicolon (the insertion into cout), which was
within the block delimited by the braces ( { } ) of the main function.
The program has been structured in different lines and properly indented, in order
to make it easier to understand for the humans reading it. But C++ does not have
strict rules on indentation or on how to split instructions in different lines. For
example, instead of
int main ()
{
std::cout << " Hello World!";
}
all in a single line, and this would have had exactly the same meaning as the
preceding code.
[demo]
int main ()
{
std::cout << "Hello World! ";
std::cout << "I'm a C++ program";
}
[/demo]
In this case, the program performed two insertions into std::cout in two different
statements. Once again, the separation in different lines of code simply gives
greater readability to the program, since main could have been perfectly valid
defined in this way:
int main () { std::cout << " Hello World! "; std::cout << " I'm a C++ program "; }
Edit & Run
The source code could have also been divided into more code lines instead:
[demo]
#include <iostream>
int main ()
{
std::cout <<
"Hello World!";
std::cout
<< "I'm a C++ program";
}
[/demo]
And the result would again have been exactly the same as in the previous examples.
Preprocessor directives (those that begin by #) are out of this general rule since
they are not statements. They are lines read and processed by the preprocessor
before proper compilation begins. Preprocessor directives must be specified in
their own line and, because they are not statements, do not have to end with a
semicolon (;).
Comments
As noted above, comments do not affect the operation of the program; however, they
provide an important tool to document directly within the source code what the
program does and how it operates.
The first of them, known as line comment, discards everything from where the pair
of slash signs ("//") are found up to the end of that same line. The second one,
known as block comment, discards everything between the /* characters and the first
appearance of the */ characters, with the possibility of including multiple lines.
[demo]
#include <iostream>
int main ()
{
std::cout << "Hello World! "; // prints Hello World!
std::cout << "I'm a C++ program"; // prints I'm a C++ program
}
[/demo]
If comments are included within the source code of a program without using the
comment characters combinations "//", /* or */, the compiler takes them as if they
were C++ expressions, most likely causing the compilation to fail with one, or
several, error messages.
cout is part of the standard library, and all the elements in the standard C++
library are declared within what is a called a namespace: the namespace std.
In order to refer to the elements in the std namespace a program shall either
qualify each and every use of elements of the library (as we have done by prefixing
cout with std::), or introduce visibility of its components. The most typical way
to introduce visibility of these components is by means of using declarations:
using namespace std;
The above declaration allows all elements in the std namespace to be accessed in an
unqualified manner (without the std:: prefix).
With this in mind, the last example can be rewritten to make unqualified uses of
cout as:
[demo]
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
}
[/demo]
Both ways of accessing the elements of the std namespace (explicit qualification
and using declarations) are valid in C++ and produce the exact same behavior. For
simplicity, and to improve readability, the examples in these tutorials will more
often use this latter approach with using declarations, although note that explicit
qualification is the only way to guarantee that name collisions never happen.