Monday, August 4, 2008

C++ program - Hello World!






Program 2. C++ source file—main.cpp

#include <iostream>
using namespace std;

int main()
{
cout << "Hello World!" << endl;
}

The main function serves a special purpose in C++ programs:

* The run-time environment calls the main function to begin
program execution.

* The opening curly brace indicates the beginning of the
definition of the main function.

* To print a value to the screen, write the word cout, followed by the insertion operator (<<), which you create by typing the less-than character (<) twice. Even though this is two characters, C++ treats it as one.

* A commonly-used alternative to the newline character \n is endl

* The return statement terminates the execution of the main
function and causes it to return the integer value 0, which is
interpreted by the run-time system as an exit code indicating
successful execution.

* The closing curly brace indicates the end of the code for
the main function.

Compiling a single C++ source file:

The C++ compiler is called g++.

> g++ main.cpp -o main.cpp.o


Output:



0 comments: