Hello World C++ Tutorial

By Malarkatron

Email: Malarkatron@hotmail.com

 

In this tutorial I will use Microsoft Visual C++ 2003. If you do not use this then you will need to know how to navigate around your IDE/Compiler. I will also give instructions for Dev C++.

 

Some Abbreviations To Know:

MSVC++………….………Microsoft Visual C++

I\O…………………….…..Input\Output

IDE………………………..Integrated Development Environment

MSDN…………………….Microsoft Developers Network

 

First Things First:

A couple of things before we code.

1: C++ IS case sensitive, in other words cout is not the same as Cout or COUT if you get an compiler error message then check your spellings chances are you capitalized something you shouldn’t have.

2: C++ is NOT an easy language, it is extensive and huge, so if you’re not serious don’t even bother because you won’t like it.

3: You need a compiler if you don’t have one you can get

Dev C++, its free and a very good IDE, get it here http://www.bloodshed.net/devcpp.html. If you are really serious and want to spend some cash get Microsoft Visual C++ or Borland C++ both are great. Borland offers a free version at their website

http://www.borland .com.

4: The only way to get good at C++ is with practice, practice, and more practice. Also go pick up some books on it.

 

Now that that’s out of the way, let’s get to it.

 

Comment Tag:

 

A comment tag is a way to make your code more readable.

The tags are: //ß A single line comment

                      /*ß Starts a multi-line comment

                      */ß Ends a multi-line comment

 

The Hello World Code:

The simplest program is the ‘Hello World’ program, it may not look like much but it is where basically every programmer starts.

 

Here’s the code:

In MSVC++:

1.    Start a new console application

2.    Find the option to make an empty project. In MSVC++ in the dialog click on Application settings and check the ‘Empty Project’ checkbox. Then Click Finish.

3.    Right Click on the ‘Source File’ folder in the ‘Solution Browser’ and click ‘Add New Item’. Select a .cpp file and name it and click ‘Open’.

4.    Type in the code in that .cpp file.

 

In Dev C++

1.    Select A New Project.

2.    Select ‘Empty Project’.

3.    Go to File->New->Source File or press Ctrl-N and click ‘Yes’ in the dialog.

4.    Enter the code.

 

#include <iostream>

using namespace std;

 

int main()

{

            cout << “Hello World!” << endl;

            return 0;

}

 

*If the screen pops up and disappears very fast then add the line ‘cin.ignore()’ after the cout. (this happens in Dev C++)*

 

Make sure you type it in exactly as it appears with the correct cases or it won’t compile.

 

Explanation:

#include <iostream>

The #include directive is used to include C++ Header files(.h). These files contain functions to use in your programs. The ‘iostream’ file contains functions for I\O (input\output).

There are many header files, look in your IDE\Compilers directory for a folder named ‘include’ and see how many for yourself.

 

using namespace std;

The ‘using’ directive, basically, is a way to do less typing . The function cout would have to be written std::cout if you weren’t using a namespace. Namespaces contain libraries of functions, there is also another use for namespaces but that is for another tutorial.

 

int main()

Every single program you will ever write will have a ‘main’ function. The ‘main’ function is the starting point for your applications.

 

cout << “Hello World!” << endl;

Cout is a function defined in the Standard Library(that’s why we use the ‘std’ namespace). The function simply writes text to the console. The ‘<<’ is a bitwise operator, this one is a ‘Left Shift Operator’ and causes the bit pattern in the first operand to be shifted left the number of bits specified by the second operand(Taken from the MSDN Collection http://msdn.microsoft.com/ ). But for this kind of Console output just think of it as the Left Shift Operator puts stuff on the screen.

The ‘endl’ is just saying to make a new line. If you want to see it just take out the last ‘<<’ and the ‘endl’.

 

return 0;

The return statement simply exits out of the current function and returns to the function calling it.

 

The ‘{‘ and ‘}’ are required. The ‘{‘ signals the start of a body of a function and the ‘}’ signals the end of that function body.

 

And Now Congratulations To You For Completing Your First C++ Program!

 

Happy programming from Malarkatron.