c++ tutorials, c tutorial, learn c++, c++ for beginners, intro to c, intro to c++ c++ tutorials, c tutorial, learn c++, c++ for beginners, intro to c, intro to c++ About Us Contact Us

C++ Programming - Structure of a program:


Structure of a program?


A C++ program begins with a function, a collection of commands that do "something". The function that begins a C++ program is called main; this function is always called when the program first executes.From main, we can also call other functions whether they are written by us or by others.

To access a standard function that comes with the compiler, you include a header with the #include directive. What this does is effectively take everything in the header and paste it into your program. Let's look at a working program:

// my first program in C++ #include
using namespace std;
int main()
{
cout<<"Hello World!";
cin.get();
}
Hello World!

The first panel shows the source code for our first program. The second one shows the result of the program once compiled and executed.

Elements of the program:

// my first program in C++
This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is.

#include 
The directive #include tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.

Using namespace std;
This line tells the compiler to use a group of functions that are part of the standard library (std). All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std.

int main ()
The next important line is int main (). This line tells the compiler that there is a function named main, and that the function returns an integer, hence int. The "curly braces" ({and}) signal the beginning and end of functions and other code blocks. Optionally, these parentheses may enclose a list of parameters within them.

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.
free online unix tutorial free online unix tutorial