Wednesday, 20 May 2015

Structure of C++ program

#include <iostream>

using namespace std;
int main(int argc, char *argv[])
{
     cout << "Hello World!"<<endl;

     return(0);
}


The first line of the above program starts with a '#' sign and interpreted by the compiler as preprocessor. This line of code instructs the compiler to include a piece of standard C++ code to deal with standard input and output.

The second line of code is to include the namespace 'std' so that the commands in the std name space can be used without using the scope resolution operator '::'.

The third line of code is the main function where the execution of your program starts. In C++ the compiler always look for the main function to start the execution. Here the main function takes two arguments 'argc' & 'argv' and returns an 'int' value. The two arguments are the command line arguments passed to main function. The argument 'argc' is the number of strings pointed by 'argv'. The first argument is always the name of the program.

The fourth line ('cout') instructs to print the word 'Hello world!' on the screen. The command 'endl' instructs to go to new line.

The fifth line returns 0 to the operating system. In most operating systems 0 means normal completion of the program. Any other value means error has occurred in the program.

Hope this is helpful to someone.





 
 
 

No comments:

Post a Comment