Site hosted by Angelfire.com: Build your free website today!

Hello World


Home
Up

Simple Hello World C++ Program



// Hello World C++ Program

# include<iostream.h>

void main(int)

{

      cout <<" Hello World! ";

      return(0);

}

 

This is a simple Hello World C++ program that will compile in any compiler. The first line is a commentary. The second line shows the method of include a function or function of an external file called a header file. The pound sign tell the compiler that this file contains a function that must be included when building the object code for the execution file.

In this case the function that we are using from input output stream header file is the cout function. I will talk more about this function later on. The line "void main(int)" is the main function. All C++ program must have a main function or the program will not compile an exe file. The syntax for a function definition is return-type followed by the function-name and its parameters in curved brackets. A the end of each line of code in a C or C++ program you have to put a semi-colon to indicated that this is the end. See example below:

return type Function-name (parameter);

The curly brackets holds the body of every function and it will not work without it. If you try to compile without a return type, function name, parameter, or  curly brackets you will get a compiler error. The cout function takes a string as its parameter with the insertion operator. The insertion operator tell the compiler to create a pointer for that will be accepted by the cout function. The cout function then print to the screen the string that the pointer points to in memory. The return function with a parameter zero exits the main function and returns control to the operating system.