Structure of C++
Structure of C++
Structure of C++
int main(void)
{
return 0;
}
And what goes between the brackets? Firstly there is a list of declarations which tell the
compiler the variables you are going to use (more about this later). Secondly there is a list of
statements which the processor is going to carry out. Both of these are optional, so the program
above is perfectly valid, though it does absolutely nothing at all.
int main(void)
{
optional declaration list
optional statement list
}
#include <iostream.h>
int main ()
{
cout << "Hello World!";
return 0;
}
The left side shows the source code for the first program, which can be saved as, for
example, helloworld.cpp. The right side shows the result of the program once compiled and
executed. To edit and compile a program depends on the compiler being used. It depends on
whether it has a Development Interface or not and on its version.
Scope of variables
All the variables that we are going to use must have been previously declared. An
important difference between the C and C++ languages, is that in C++ we can declare variables
anywhere in the source code, even between two executable sentences, and not only at the
beginning of a block of instructions which is done in C.
Global variables can be referred to anywhere in the code,
within any function, whenever it is after its declaration.
In addition to local and global scopes there exists external scope, that causes a variable to be visible not only in the
same source file but in all other files that will be linked together.
Comment (optional)
This is a comment line. All the lines beginning with two slash signs (//) are considered
comments and do not have any effect on the behavior of the program.
In C++ you can use two styles of comments:
new style: using the // used in front of each comment line. Any text on the line after the
comment mark // is a comment, which is ignored by the compiler.
old style: using /* to mark the beginning of a comment and */ to mark the end. Old style
comments can span over any number of lines. Any text contained between the opening
and closing comment marks is a comment and is ignored by the C++ compiler.
#include <header_file>
Sentences that begin with a pound sign (#) are directives for the preprocessor. They are
not executable code lines but indications for the compiler. In this case the sentence #include
<iostream.h> tells the compiler's preprocessor to include the iostream standard header file. This
specific file includes the declarations of the basic standard input-output library in C++, and it is
included because its functionality is used later in the program.
b. #define Directive
The #define directive is used to define an identifier and a string that will be substituted
for the identifier each time it is encountered in the source file. A common use of #define is to
define the size of things that might change over the evolution of the program, for example,
#define size 5.
Function Prototype
A function prototype provides the compiler with the name and arguments of the function
contained in the program and must appear before the function is used.
int main ()
The body of any C++ program always begins with main. Anything you write before this
statement can be regarded as "setup" code which prepares things so the program can begin.
Every C/C++ program must have a main function.
A C/C++ program can only have one main function in any program.
The main keyword is case sensitive, and must be entirely in lowercase, so Main, or MAIN,
or any combination involving uppercase letters will result in an error.
The location of main doesn't matter (so long as it is after the setup code) but
conventionally it goes right at the beginning of the program, just as in all the examples
you will see.
a. Variable Definition
b. Program Statements
The real work of the C++ program is done by its statements. C++ statements display
information on the screen, read keyboard input, perform mathematical operations, call
functions, read disk files and all other operations that a program needs to perform. A
statement is a complete direction instructing the computer to carry out some tasks.
c. Expressions
e. Return Statement
The return 0 instruction causes the main() function finish and return the code that the
instruction is followed by, in this case 0. This it is most usual way to terminate a program that has
not found any errors during its execution. As you will see in coming examples, all C++ programs
end with a sentence similar to this.
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
int main ()
{
cout << " Hello World ";
return 0;
}
in just one line and this would have had exactly the same meaning.
Here is a program with some more instructions:
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 have been perfectly defined thus:
int main () { cout << " Hello World! "; cout << " I'm to C++ program "; return 0; }
We were also free to divide the code into more lines if we considered it convenient:
int main ()
{
cout <<
"Hello World!";
cout
<< "I'm a C++ program";
return 0;
}