Monday, September 19, 2011

A simple C++ Program and Explanation of it


A simple C++ Program and Explanation of it:

#include <iostream.h>

int main (void)

{

cout << "Hello World\n";

}





            #include <iostream.h>


1.      This line uses the preprocessor directive #include to include the contents of the header file iostream.h in the program. Iostream.h is a standard C++ header file and contains definitions for input and output.


int main (void)

2.      This line defines a function called main. A function may have zero or more parameters; these always appear after the function name, between a pair of brackets. The word void appearing between the brackets indicates that main has no parameters. A function may also have a return type; this always appears before the function name. The return type for main is int (i.e., an integer number). All C++ programs must have exactly one main function. Program execution always begins from main.


{
3.      This brace marks the beginning of the body of main.

cout << "Hello World\n"; 

4.      This line is a statement. A statement is a computation step which may produce a value. The end of a statement is always marked with a semicolon (;). This statement causes the string "Hello World\n" to be sent to the cout output stream. A string is any sequence of characters enclosed in double-quotes. The last character in this string (\n) is a newline character which is similar to a carriage return on a type writer. A stream is an object which performs input or output. Cout is the standard output stream in C++ (standard output usually means your computer monitor screen). The symbol << is an output operator which takes an output stream as its left operand and an expression as its right operand, and causes the value of the latter to be
5.      sent to the former. In this case, the effect is that the string "Hello World\n" is sent to cout, causing it to be printed on the computer monitor screen.

}

6.       This brace marks the end of the body of main.

0 comments em “A simple C++ Program and Explanation of it”

Post a Comment

Give your valuable Comment...

 

Programming, Data Structures & Algorithms Copyright © 2011 -- Template created by JAK -- Powered by Blogger