"Hello world!" in C++

Status
Not open for further replies.

Baljeet

Member
Jan 31, 2011
76
0
In this lesson I will show you how to create a simple program, called Hello world, which will simply just write out "Hello world!". A Hello world program is the best way to start in programming language, because this gives a basic understand of the syntax of the code.

First set up an empty Win32 Console Application project and add a C++ file (main.cpp) )

Write the following code and press F5 to run it:
Code:
#include <iostream>

using namespace std;

int main(){
   cout << "Hello world!" << endl;
   system("pause");
}
The first line includes the iostream library which provides the input and output functions. Without this function we can`t write out the result of the program. Notice that wen a library is included #include is used.

The second line tell the compiler to use the std namespace, namespace allows to create groups of functions or objects under a name. Don`t worry, I will write more about namespace later. For now it`s good to know that if you leave this line from the code you will need to include std:: before each cout or cin from your code. Notice that the line is ended by a semicolon (";");

The third line starts a function called main, which will return and int (integer). The main function starts first in any program. The curly braces { and } shows the beginning and end of the function.

The next line writes out the "Hello world!" text. Notice that the line is ended by a semicolon (";").

The next line is telling the compiler to pause the program and wait for the user to press Enter. Without system("pause") the program will run so fast that you will not see the result.

Let`s see the program in action:
Hello+world.jpg
Note: Don`t worry if you haven`t understand everything. This lesson is written to have an idea about how a program looks and to see the basic syntax of C++.

Another useful and simple thing to learn are comments: In C++ you can add comments (notes) after the code lines. The comments are skipped by the compiler and they don`t affect the program. Comment are helpful to note or explain things. The comment syntax is:
Code:
//Comment goes here
or
Code:
/*Comment line 1
Comment line 2 */
Here you can find the original lesson:

All Credits goes to one who really made this...
 
Status
Not open for further replies.

Users who are viewing this thread

Top