Home » Programming » Writing your first C++ program

Writing your first C++ program

In this tutorial for C++ we will create and run our first C++ program. Remember that the best way to start learning any programming language is through practice. So lets get started!


First thing we need to do is create our project file.

Open up Microsoft Visual C++ 2010 Express and browse through the following settings:

File -> New -> Project



Select Win32 Console Application as we are using a simple command prompt to show our program.



Click Finish.



Remove all of the code that is currently sitting in the draalin-MyFirstProgram.cpp file.



Now I will go over what this program contains below.


This is a programmers block! 

Anything in here is a comment, which can be used to create a programmers block. Very useful for anyone opening your program and wanting to understand exactly what it is about! 


#include "stdafx.h"

Is a preprocessor directive, meaning it tells the compiler to insert code from the required library functions. This  is required for the Microsoft Visual C++ 2010 Express edtion.

#include <iostream>

Is a preprocessor directive, meaning it tells the compiler to insert code from the required library functions. This one is used for input/output of screen messages.


using namespace std;

Contains all the classes, objects and functions of the standard C++ library that we need.


int main()
Code inside here

Every C/C++ program must contain the main() function. In most C++ programs you will put quiet a bit of your code in here.

Output stream

cout << "This is my first program";

Cout is a way to print messages to your command prompt. It requiress the <iostream> library functions as it is an object of them.


return 0;

This is a requirement for the main() function. It means that if it executes this line of code your program has executed without any errors!

Example of the C++ program below

You can see a small example of a simple program that outputs “This is my first program” to your command prompt.

Program Name: draalin-myFirstProgram.cpp
Developer: Tyler Bailey
Environment: Microsoft Visual C++ 2010 Express
Description: Simple output program
Variables: N/A
Constants: N/A
Functions: main

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
cout << "This is my first program";

return 0;

Paste in the following code above so it looks like the screenshot below!



Now lets save our c++ program so we can run it after.

File -> Save draalin-myFirstProgram.cpp



Now we can run our c++ program by doing the following:

Debug -> Start Debugging



You can now see your program is running!

Press any to exit the prompt and go back to Microsoft Visual C++ 2010 Express.



That is it, you have created and ran your first C++ program!



If you require additional support on this tutorial or want some more help check out our forums found here.


Added gallery for additional support below for writing our first C++ program:

This slideshow requires JavaScript.

About Tyler Bailey

Tyler Bailey is a university student who owns and runs draalin.com. Follow me on