Folder101   Home Notes Quiz Assigns Info  
Forum Forum
101 Home 101
  C++ Level 2

Hello World Program


Getting Started

Compile Errors

Extension Work


Time to start your very first program. By convention, the first program you should write when learning a new programming language is a Hello World! program.

So, you shall write a short program that outputs the sentence "Hello World!" to the command prompt console.

  Getting Started

Step 1- Starting VC++

Double-click on the Microsoft Visual C++ icon on the the start menu to launch Microsoft Visual Studio. It will come up as an empty window.

Step 2- Create a Project

When programming in Visual C++ you must use a project so that all the necessary files can be linked together properly. Select 'File>New' from the menu. The 'New' dialog box will appear.

Follow these steps.

1. Select the 'Projects' tab.

2. From the list at the left, select 'Win32 Console Application.'

3. In the 'Location' text box, type the name of the folder you wish to create your project in.

4. Type in a name for the project in the textbox "Project Name". You will notice as you type that the project's name is added onto the end of the folder in the 'Location' text box. All your project files will be placed inside this subfolder.

Now select OK to create your project. If you are using version 6 of the development environment, a dialog box will appear asking what type of application you wish, in that case answer 'An empty project' and click 'Finish'.

Step 3 - Creating your source file

At this point you shall have an empty project.

On the left side there should now be two tabs 'Classview' and 'FileView' to the leftmost window.

Click on the 'FileView' tab.

In 'FileView' you should be able to see a group icon called 'HelloWorld Files'.

At present all the different folders will be empty because you haven't created any files to go in your project yet.

You will need to add a source code file next so you can type in some code.

Select 'File>New' from the menu. The 'New' dialog box will appear again.

Follow these steps.

1. Make sure the the 'Files' tab is selected.

2. From the list at the left, select 'C++ Source File.'

3. Tick the 'Add to Project' checkbox.

4. Type in a name for your source file. I would suggest the name 'HelloWorldSource.'

5. In the 'Location' text box, Make sure the name of your project folder is displayed.

Step 4 - Type in the Program

Now type in the code for your 'HelloWorld' program as shown below.

#include <iostream.h>
int main()
cout << "Hello World!\n";
return 0;

Step 5 - Compile, link and run

To compile and link the program, select 'Build > Build HelloWorld.exe' from the menu.

You may then run the program by selecting 'Build > Execute HelloWorld.exe'

If your code is incorrect then errors and warnings may be displayed in the bottom frame.

If this is the case, then re-check all your code and ensure all the curly brackets and semicolons are in the right place. Try again.

If you still get errors then go to the next section on compiler errors.


If all goes well a Command Prompt Console windows should appear with the words:-

Hello World!

...written to the screen. If so then 'Well Done!' you have just successfully written your first program.



  Compile Errors

Compile-time errors can occur for any number of reasons. Usually they are a result of a typing error. The VC++ compiler will display any errors and warnings in the bottom frame.

If you scroll up this screen (or if you expand it) you will be able to see the details of errors and warnings; they follow this format:-

File name(line number) : error/warning code: description

As an example, lets intentionally put an error into the program by deleting the end curly brace '}' from the program code. Now rebuild your program and you will get an error similar to:-

...HelloWorldSource.cpp(7) : fatal error C1004: unexpected end of file found

The error messages can be quite cryptic but you will get used to interpreting them. In this case, a clue is given by the line number included in the message:- cpp(7).

If you double-click on anyone of the error message, the place in the source code where presumably the expression that causes the error is will be highlighted in your code. You need to use this location and the error description to correct it. You should note that sometimes the compiler does not diagnose the problem correctly and gets the line number wrong.

Once your program has been successfully built and executed for the first time an executable file with .EXE extension will be generated. It will be in the working folder for the project within a subfolder called Debug or Release.

  Extension Work

Today you learned how to create, compile, build, and run your first C++ program. Over the next few weeks we will be covering these topics in much more detail.

Carry out the following exercises on your Hello World! program:-

  1. Save your HelloWorld program by selecting 'File>Save All' from the menu. Then close Visual C++. Go to your HelloWorld project folder. You should have about 7 files plus a Debug or Release folder. Find the file with the extension .cpp. Open it in notepad. What do you conclude about this file?

  2. Double-click the file with the .dsw extension. Visual C++ should launch and load your HelloWorld project with all the files. This is an easier way of opening a project than having to go through the Windows Start menu.

  3. Look carefully at your code. Try changing your HelloWorld program so it outputs 'Goodbye Everyone!' to the screen.

  4. Lets create some compiler errors so you become more familiar with them. Locate the following line:-

    return 0;

    Remove the semi-colon at the end of line. Try and compile your program by selecting Compile or Build HelloWorld.exe from the Build menu. What error message do you get? Make sure you fix this error when you have finished.

That it!

The next tutorial explains compiling and linking.