// hello.cpp : printing Hello World message
# include <iostream.h> void main () { cout << "Hello World"; }
Hello World

The header file iostream.h supports stream programming features with the help of predefined stream objects. The C++ stream insertion operator, << send the message “Hello World” to the predefined console object, cout, which in turns prints on the console.

First C++ program

First Line- Comment Line

  • The statements which starts with symbol // is treated as comment.
  • The compiler ignores the complete line starting from the // character pair.
  • Comment is used to increase the readability of the program.
  • The word cpp, in the program hello.cpp, is an acronym for CPlusPlus (C++).
  • The compiler will recognize the program as a C++ program only when it has .cpp extension.
  • Note: Some C++ compilers such as GNU under UNIX system expect program files to have cc as an extension.

Second Line- Preprocessor Directive

  • The second line is a preprocessor directive. The preprocessor directive # include <iostream.h> includes all the statements of the header file iostream.h
  • It contains instructions and predefined constants that will be used in the program.
  • It plays a similar role to that of stdio.h of C language.
  • The header file iostream.h contains declarations that are needed by the cout and cin stream objects.
  • These directives are processed before any other executable statements in the source file of the program by the compiler.

Third Line- Function Declaration

  • The third line in the program is void main ( )
  • Similar to C program, the C++ program must have one function with the name main, from where the execution of the program begins.
  • The name main is a special word (not a reserved word) and must not be invoked anywhere by the user.
  • The function name is followed by a pair of parentheses which may or may not contain arguments.
  • Note: Every function is supposed to return a value, but the function in this example does not return any value. Such function names must be preceded by the special word void.

Fourth Line- Function Begin

  • The function body in a C/C++ program is enclosed between two flower brackets.
  • The opening flower bracket ( { ) marks the beginning of a function.
  • All the statements in a function which are listed after this brace can either be executable or non-executable statements.

Fifth Line- Function Body

  • The function body contains a statement to display the message “Hello World”.
  • The output statement cout is pronounced as C-out (meaning Console Output).
  • cout << “Hello World” prints the message “Hello World” on the standard console output device.

Sixth Line- Function End

  • The end of the function body in C/C++ program is marked by the closing flower bracket ( } ).
  • When the compiler encounters this bracket, it is replaced by the statement, return which transfers control to a caller.
  • In this program, the last line actually marks the end of the program and control is transferred to the operating system on termination of the program.