C++ Basics 1 : A simple first program in C++
If you've followed the tutorial on creating a new project in Visual Studio or in Dev-C++ then you'll be ready to write a program! This tutorial takes you through the steps of creating your first program in C++.
Right now you should have a big white empty space on your screen, waiting for your code.
Before you start: using comments
See the mini-tutorial about commenting your code before you start - it's an essential habit to get into so you might want to start now so that you can comment your first program as you go along.
Include the required libraries
Type the following command:
This command tells the compiler to find a library called iostream and include it in your program. A library is a set of functions that another programmer has already written to save you the job. In this case the iostream library allows you to get input from the user and put output on the screen.
Define the namespace
Hit
a couple of times and then type the following command:
Many of the commands that you will use in C++ are hidden away in classes - which means that to use the command you need to know the name of the class it belongs in.
To make life easier for you, you can specify the 'namespace' - enabling you to use the command without its object name as long as you've specified the namespace in which it belongs.
In this case you will be using two commands called cout and endl. Their full names are std::cout and std::endl but by using the line above you can refer to them as just cout and endl.
Create the main function
Hit
a couple of times and type the following:
Programs in C++ are broken up into smaller blocks of code called functions. Each of these blocks must have a unique name so that they can be referred to later.
When a C++ program is run, it starts in a block of code called main. If that function isn't there your program won't run. The line above creates that function.
- To create a function you first of all start off with a word to show what that function outputs. In this case, the word "int" means that the function outputs a whole number (known as an integer, hence the word int).
- Next, you give the function its unique name. In this case that name is, "main".
- After the name, in brackets (or parenthesis if you want to use the proper name) you must list the inputs that the function requires. I've used the word "void" again to show that there are no inputs.
- Finally you show the start of the function with an opening curly brace.
Write some code in your main function
Press
after the curly brace (if you haven't already done so) and type the following:
The command cout means "console output". It enables you to output something to the computer screen by using a stream of characters that flow from your data, through cout, onto the screen.
Following the command you must have what is known as the stream operator. Think of this as a connector that connects what you want to output to the cout command.
Notice how the operator looks like an arrow that flows the words "Hello world" to the console output. That direction of flow is important. The data that you want to output must 'flow' towards the cout command. 
Add a command to pause the program
Press
and type the following:
This is a command that is commonly used to display a "Press a key" message to the user that then pauses your program until the user presses a key.
Unfortunately this command relies upon a system command called PAUSE. This means that if you use this step, your program code might not be 100% cross-platform compatible. Another system (e.g. a small mobile device) may not have the PAUSE command as part of its operating system.
Normally when you run your program, the command window is displayed (also known as the DOS box) and your program output is displayed in that box. Unfortuantely when your program is complete, that box is closed. This command gives you the chance to see your program's output.
Add the command that returns the function's output value
Press
and add the following line of code:
Your main function should always return an integer value to indicate to the system (or another application that ran it) if the program completed successfully or not. The label EXIT_SUCCESS is actually a number. When your program is compiled the words will be replaced with the number zero, meaning that your program is ending normally (as opposed to ending early due to an error).
Close the main function
Press
after the last command (if you haven't already) and type the following character:
That single curly brace signifies the end of the main function. In this case it's also the end of your first program!
Running or building the program in Visual studio
To run the program with a "press a key" prompt at the end:
![]()
![]()
If you're using Visual Studio, hold down Ctrl and press F5 to run it. In the example code in this tutorial you won't need to do this because the system("PAUSE") command will ask for a key to be pressed anyway.
To run the program without a "press a key" prompt at the end:
Pressing F5 on its own also builds and runs your program but it doesn't ask the user to press a key at the end - which means you won't get to see the output of your program.
To just compile the program without running:
![]()
![]()
You might want to just compile your program to test for syntax errors. To do this you just need to press Ctrl+F7.
Running or compiling the program using Dev-C++
Press F9 to compile and then run the program.
![]()
![]()
If you don't want to run it, you can compile the program (to see if there's any errors in it, perhaps) by pressing CTRL+F9.

Complete program, including comments
// Include the input/output stream library
#include <iostream>
// Indicate that commands are being used from the std namespace
using namespace std;
// Begin the main function, specifying that
// there is an integer as the output but no inputs
int main(void){
// Stream the words "Hello world" followed by
// an end of line character to the screen
cout << "Hello world" << endl;
// Use the system command PAUSE to ask the user to press a key
system("PAUSE");
// Use the special code EXIT_SUCCESS as the return
// value for the main function. This means everything went OK.
return EXIT_SUCCESS;
} // End the main function
Exercise
To practice what you've learned in this tutorial you might want to try this exercise.
- Change the contents of the main function so that, instead of outputting "Hello World" it outputs the colours of the rainbow on seperate lines. In case you're not sure, the colours are: red, orange, yellow, green, blue, indigo, violet. Click here for a solution.
