|
C++ Programming Tutorials
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 TechPreparation ";
return 0;
} |
We could have written:
| int main () { cout
<< "Hello TechPreparation"; 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 code in different
lines serves only to make it more legible and schematic for the humans that may
read it.
Let us add an additional instruction to our first program:
// my second program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello TechPreparation! ";
cout << "I'm a C++ program";
return 0;
} |
Hello TechPreparation! I'm a C++ program
|
In this case, we performed two insertions into cout in two
different statements. Once again, the separation in different lines of code has
been done just to give greater readability to the program, since main could have
been perfectly valid defined this way:
| int main () { cout << " Hello TechPreparation!
"; cout << " I'm a C++ program "; return 0; } |
We were also free to divide the code into more lines if we
considered it more convenient:
int main ()
{
cout <<
"Hello TechPreparation!";
cout
<< "I'm a C++ program";
return 0;
} |
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 and do not produce any code by themselves. Preprocessor directives
must be specified in their own line and do not have to end with a semicolon (;).
Comments
Comments are parts of the source code disregarded by the compiler. They simply
do nothing. Their purpose is only to allow the programmer to insert notes or
descriptions embedded within the source code.
C++ supports two ways to insert comments:
// line comment
/* block comment */ |
The first of them, known as line comment, discards everything
from where the pair of slash signs (//) is 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 more than one line.
We are going to add comments to our second program:
/* my second program
in C++
with more comments */
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello TechPreparation! "; // prints Hello
TechPreparation!
cout << "I'm a C++ program"; // prints I'm a C++ program
return 0;
} |
Hello TechPreparation! I'm a C++
program
|
If you include comments within the source code of your
programs without using the comment characters combinations //, /* or */, the
compiler will take them as if they were C++ expressions, most likely causing one
or several error messages when you compile it.
NEXT >> Variables. Data Types.
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
C Aptitude Questions
for more C Aptitude Interview Questions with Answers
Check
C Interview Questions
for more C Interview Questions with Answers.
|