ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Hello World - How To Create Your First Visual C++ Program

Updated on June 10, 2014

Getting Started

So, you are ready to write your first C++ program. Well, in order to do so, you need to make sure you have a compiler installed. The compiler I recommend is Microsoft Visual C++ 2010 Express. If you have another one you are using, that's fine, but this programming tutorial will be demonstrated in Visual C++.

If you need to install Visual C++, you can find instructions for that here:

Installing Microsoft Visual C++ 2010 Express

Once that is taken care of, we are ready to move on and get this show on the road!

Launching Visual C++ and Creating a Project

Okay, the first thing we need to do is to launch Visual C++. You can do this by visiting the following location:

Start >> Programs >> Microsoft Visual C++ 2010 Express

If this is the first time you have launched it, there may be a slight delay while it creates the initial directory structure for the first time. When the program opens you will be taken to the "Start Page".

From here, you need to make a new Visual C++ project to house your C++ program. This can be done by selecting: File >> New >> Project as in the screenshot below.

Now you will be faced with a screen that asks what sort of project you would like to create. For the purpose of this tutorial, we will simply choose "CLR Empty Project".

We are choosing this because it allows us to create everything from scratch. That's what a computer programming tutorial is all about right? In order to learn to program properly, you need to be able to create what you need, not rely on others to build it for you.

On this same screen, you also need to name your project "Hello World", and when you do, it will automatically name the solution the same thing. Also, make sure to leave the option "Create Directory for Solution" checked. This way, it will create a whole new directory to house your project.

When you have finished making the selections above, simply click "OK" and the project and solution will be created for you in a brand new directory. Congratulations, you are now ready to move and create your first source code file!

Creating Your Source File

Now that you have created your project, you will see a screen that shows the "Solution Explorer" for your project. Other than the Solution Explorer, the rest of the project page will be blank.

So, to make our C++ Hello Word program, we need to write some source code that can be compiled and executed. But before you can write source code, you need a source file to put it in. Let's create it now.

In the Solution Explorer, right click on "Source Files" and select Add >> New Item

The next screen wants you to choose the type of item you would like to add to the project. Since this C++ tutorial is a simple "Hello Word" program, we only need to add one C++ file. These files will always have a .cpp extension.

Select "C++ File (.cpp)" as the type of item to add and then name it "Hello World", just like the project and solution.

After you have selected the type of file and named it, select "Add". This will now add the file "Hello World.cpp" to your collection of source files in the Solution Explorer. You will also now see the open "Hello World.cpp" file open and ready for adding your source code.

Adding You Source Code

In this C++ programming tutorial, you are simply creating a program that will print the phrase "Hello World" to your computer console. In doing this, you only need to write a grand total of 7 lines of code to the source code file. Below, let's go through each line and explain exactly what it does.

These lines should be added directly in the blank text editor space on the screen.

Line 1: #include <iostream>

This is known as a "pre-processor directive" and is used to pull in c++ libraries that need to be compiled with your source code. This line tells the compiler to add in the "iostream" library so your program can process simple input and output.

Line 2: using namespace std;

Namespaces are used to define a particular working space for a program. If you had multiple cpp files in your program, they would all have the same namespace so they could function together. Even though we only have one file, it is still good practice to define a namespace.

Line 3: void main()

Every c++ program must have a function called "main". This is always the starting point of execution for any c++ program. Every function must also have a "return type". The return type for our main function is "void" because it does not return anything. When it executes, it simply prints a line of text to the console and exits. If our function happened to perform some calculation and return an integer, it would be called "ïnt main()".

Line 4: {

All code within a function must be contained in "curly braces". These braces simply tell the compiler that all of the code in the function compiles as a single unit. This is the opening brace for our function before we place any code into it.

Line 5: cout << "Hello World" << endl;

"Cout" is the name of the function used to print output to the console. It is actually contained within the iostream library, which is why we included it in the first place. Then the compiler will output to the console anything after the "<<" delimiter.

"Hello World" is the string of text that cout is going to print to the console. The next "<<" is telling the compiler that there is something else after "Hello World" that needs to be sent to cout.

The "ëndl" statement is a reserved keyword in c++ that tells the compiler to print a new line on the console during execution. All this does is skip the cursor down one line and is basically used for readability.

Line 6: system("Pause");

The "system("Pause");" statement tells the compiler to pause the console until the user decides they are finished looking at it. If we did not pause the console, it would come up and go away so fast that you would not be able to tell if it printed your statement properly. This will pause the console until you "press any key" to exit.

Line 7: }

This is the closing curly brace that tells the compiler it has reached the end of the main function. At this point, since main is the only function in your program, compilation will stop and execution will begin.

Super congratulations !! You have completed adding your source code to your Hello World.cpp file. It should look something like this:


Compiling and Running Your Program

Now that you have all of the source code put in, you need to compile your program and see it work. To do this, simply press the green "play" button at the top of the Visual C++ Window.

You will be asked if you wish to compile and build the file. Select "Yes".

Then, if there are no errors, you should see a console screen come up and display your output like this:

What Next?

Now that you have written, compiled, and ran your first Visual C++ program, you can move on to more advanced topics.

You might also want to check out some of the books below. They contain some great C++ Programming Tutorials to build your expertise!

working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
ClickscoThis is a data management platform studying reader behavior (Privacy Policy)