0% found this document useful (0 votes)
41 views

Structure of A Program

The document discusses the structure and components of a basic "Hello World" C++ program. It examines each line of code, explaining what it does. It covers comments, functions, namespaces, and how code is structured for readability versus functionality. While indentation and line breaks don't affect execution, they make the code easier for humans to understand. The "Hello World" program introduces fundamental components like main functions, output statements, and comments.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Structure of A Program

The document discusses the structure and components of a basic "Hello World" C++ program. It examines each line of code, explaining what it does. It covers comments, functions, namespaces, and how code is structured for readability versus functionality. While indentation and line breaks don't affect execution, they make the code easier for humans to understand. The "Hello World" program introduces fundamental components like main functions, output statements, and comments.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

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
1
2
3
4
5
6
7
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";

Hello World!
The left panel above shows the C++ code for this program. The right panel shows the result
when the program is e!ecuted by a computer. The grey numbers to the left of the panels are
line numbers to ma"e discussing programs and researching errors easier. They are not part of
the program.
#et$s e!amine this program line by line
#ine % !! m" #irst $ro%ram in &''
#ines beginning with two slash signs &!!' are comments by the programmer and have
no effect on the behavior of the program. (rogrammers use them to include short
e!planations or observations concerning the code or program. )n this case, it is a brief
introductory description of the program.
#ine * (include <iostream)
#ines beginning with a hash sign &(' are directives read and interpreted by what is
"nown as the preprocessor. They are special lines interpreted before the compilation
of the program itself begins. )n this case, the directive (include <iostream),
instructs the preprocessor to include a section of standard C++ code, "nown as
header iostream, that allows to perform standard input and output operations, such as
writing the output of this program &Hello World' to the screen.
#ine + A blan" line.
,lan" lines have no effect on a program. They simply improve readability.
#ine - int main ()
This line initiates the declaration of a function. .ssentially, a function is a group of
code statements which are given a name in this case, this gives the name "main" to
the group of code statements that follow. /unctions will be discussed in detail in a later
chapter, but essentially, their definition is introduced with a succession of a type &int',
a name &main' and a pair of parentheses &()', optionally including parameters.
The function named main is a special function in all C++ programs0 it is the function
called when the program is run. The e!ecution of all C++ programs begins with the
main function regardless of where the function is actually located within the code.
#ines 1 and 2 { and
The open brace &{' at line 1 indicates the beginning of main$s function definition, and
the closing brace &' at line 2, indicates its end. .verything between these braces is
the function$s body that defines what happens when main is called. All functions use
braces to indicate the beginning and end of their definitions.
#ine 3 std::cout << "Hello World!";
This line is a C++ statement. A statement is an e!pression that can actually produce
some effect. )t is the meat of a program, specifying its actual behavior. 4tatements are
e!ecuted in the same order that they appear within a function$s body.
This statement has three parts /irst, std::cout, which identifies the standard
character output device &usually, this is the computer screen'. 4econd, the insertion
operator &<<', which indicates that what follows is inserted into std::cout. /inally, a
sentence within 5uotes &"Hello world6"', is the content inserted into the standard
output.
7otice that the statement ends with a semicolon &;'. This character mar"s the end of
the statement, 8ust as the period ends a sentence in .nglish. All C++ statements must
end with a semicolon character. 9ne of the most common synta! errors in C++ is
forgetting to end a statement with a semicolon.
:ou may have noticed that not all the lines of this program perform actions when the code is
e!ecuted. 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 bloc" delimited by the braces & { ' of the main
function.
The program has been structured in different lines and properly indented, in order to ma"e it
easier to understand for the humans reading it. ,ut C++ does not have strict rules on
indentation or on how to split instructions in different lines. /or e!ample, instead of
1
2
3
4
int main ()
{
std::cout << " Hello World!";

We could have written


int main () { std::cout << "Hello World!";
all in a single line, and this would have had e!actly the same meaning as the preceding code.
)n C++, the separation between statements is specified with an ending semicolon &;', with
the separation into different lines not mattering at all for this purpose. ;any statements can
be written in a single line, or each statement can be in its own line. The division of code in
different lines serves only to ma"e it more legible and schematic for the humans that may
read it, but has no effect on the actual behavior of the program.
7ow, let$s add an additional statement to our first program
1
2
3
4
5
6
7
// my second program in C++
#include <iostream>
int main ()
{
std::cout << "Hello World! ";
std::cout << "*+m a &'' $ro%ram";
Hello World! *+m a &'' $ro%ram
,
)n this case, the program performed two insertions into std::cout in two different
statements. 9nce 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 << " *+m a &''
$ro%ram ";
The source code could have also been divided into more code lines instead
1
2
3
4
5
6
7
int main ()
{
std::cout <<
"Hello World!";
std::cout
<< "*+m a &'' $ro%ram";

And the result would again have been e!actly the same as in the previous e!amples.
(reprocessor 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. (reprocessor 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 program0 however, they provide
an important tool to document directly within the source code what the program does and how
it operates.
C++ supports two ways of commenting code
1
2
// line comment
/* block comment */
The first of them, "nown 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, "nown as block
comment, discards everything between the !- characters and the first appearance of the -!
characters, with the possibility of including multiple lines.
#et$s add comments to our second program
1
2
3
4
5
/* my second program in C++
with more comments */
#include <iostream>
Hello World! *+m a &''
$ro%ram
6
7
,
.
1/
int main ()
{
std::cout << "Hello World! "; // prints
Hello World
std::cout << "*+m a &'' $ro%ram"; // prints
!"m a C++ program

)f comments are included within the source code of a program without using the comment
characters combinations !!, !- or -!, the compiler ta"es them as if they were C++
e!pressions, most li"ely causing the compilation to fail with one, or several, error messages.
Using namespace std
)f you have seen C++ code before, you may have seen cout being used instead of
std::cout. ,oth name the same ob8ect the first one uses its unqualified name &cout', while
the second 5ualifies it directly within the namespace std &as std::cout'.
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.
)n order to refer to the elements in the std namespace a program shall either 5ualify each
and every use of elements of the library &as we have done by prefi!ing 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:: prefi!'.
With this in mind, the last e!ample can be rewritten to ma"e un5ualified uses of cout as
1
2
3
4
5
6
7
,
.
// my second program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "*+m a &'' $ro%ram";

Hello World! *+m a &'' $ro%ram


,oth ways of accessing the elements of the std namespace &e!plicit 5ualification and using
declarations' are valid in C++ and produce the e!act same behavior. /or simplicity, and to
improve readability, the e!amples 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.
7amespaces are e!plained in more detail in a later chapter.

You might also like