0% found this document useful (0 votes)
29 views4 pages

Executed in any-WPS Office

In C++, every program must have a main function, which is defined with parentheses and contains the program's executable code within braces. The statement 'cout << "Hello World!";' outputs text to the standard output stream, while 'return 0;' signifies successful program termination. C++ allows flexibility in formatting code, with statements separated by semicolons, regardless of whether they are on the same line or different lines.

Uploaded by

natapnahk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

Executed in any-WPS Office

In C++, every program must have a main function, which is defined with parentheses and contains the program's executable code within braces. The statement 'cout << "Hello World!";' outputs text to the standard output stream, while 'return 0;' signifies successful program termination. C++ allows flexibility in formatting code, with statements separated by semicolons, regardless of whether they are on the same line or different lines.

Uploaded by

natapnahk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

executed in any C++ program.

For that same reason, it is essential that all C++ programs have a main

function.

The word main is followed in the code by a pair of parentheses (()). That is because it is a function

declaration: In C++, what differentiates a function declaration from other types of expressions are

these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters

within them.

Right after these parentheses we can find the body of the main function enclosed in braces ({}).

What is contained within these braces is what the function does when it is executed.

cout << "Hello World!";

This line is a C++ statement. A statement is a simple or compound expression that can actually

produce some effect. In fact, this statement performs the only action that generates a visible effect in

our first program.

cout represents the standard output stream in C++, and the meaning of the entire statement is to

insert a sequence of characters (in this case the Hello World sequence of characters) into the standard

output stream (which usually is the screen).

cout is declared in the iostream standard file within the std namespace, so that's why we needed to
include that specific file and to declare that we were going to use this specific namespace earlier in

our code.

Notice that the statement ends with a semicolon character (;). This character is used to mark the end

of the statement and in fact it must be included at the end of all expression statements in all C++

programs (one of the most common syntax errors is indeed to forget to include some semicolon after

a statement).

return 0;

The return statement causes the main function to finish. return may be followed by a return code (in

our example is followed by the return code 0). A return code of 0 for the main function is generally

interpreted as the program worked as expected without any errors during its execution. This is the

most usual way to end a C++ console program.

You may have noticed that not all the lines of this program perform actions when the code is

executed. There were lines containing only comments (those beginning by //). There were lines with

directives for the compiler's preprocessor (those beginning by #). Then there were lines that began

the declaration of a function (in this case, the main function) and, finally lines with statements (like

the insertion into cout), which were all included within the block delimited by the braces ({}) of the
main function.

The program has been structured in different lines in order to be more readable, but in C++, we do

not have strict rules on how to separate instructions in different lines. For example, instead of

int main ()

cout << " Hello World!";

return 0;

We could have written:

int main ()

cout << "Hello World!";

return 0;

All in just one line and this would have had exactly the same meaning as the previous code.

In C++, the separation between statements is specified with an ending semicolon (;) at the end of
each one, so the separation in different code lines does not matter at all for this purpose. We can

write many statements per line or write a single statement that takes many code lines. The division of

You might also like