Writing your first C++ Program using Visual Studio 2012

In previous blogs I have shown you how to set up Visual Studio 2012 so that it will function on Windows 8, 7, and so forth using the Azure VM, although we haven’t created a program to upload to the store.  That will be covered in another parallel blog series over at http://blogs.msdn.com/silverlightgames.

I won’t use a video this time, although I am tempted to. Smile

Open Visual Studio 2012 using the Windows 8 Client or the Azure VM (if you are using a Windows 7 or Apple computer), we are going to start with the usual boring Console Window.  Sorry.  Visual Studio 2012 differs from VS 2010 as it will create a default name.


Since you won’t be living with this app just keep the default name.  Not a good practice, but hey, let’s keep it really easy:


It would appear the next dialog box is a useless, but if you click application settings you will also see the following:



Select Empty Project and then Finish.  If your IDE looks like the following, that would be pretty boring, so add the solution explorer by clicking on View-Solution Explorer or use ctrl+alt+L (good idea to memorize the keystrokes while you are a student)


After the solution explorer shows up, you have an empty project, which means it doesn’t have anything in it.  You will need to add a simple couple of lines of code.


Select the C++ File (.cpp), give it a name or not.  This is just boring console app that you will forget about in a few minutes after reading this blog.



Add code that looks like the following to your new Source File, the file with the extension of *.cpp:

#include <iostream>

int main()
    std::cout<< "Hello world and all that" << std::endl;
    return 0;

If you run your program using F5 then the console will pop up and then close.  To see your beautiful code press CTRL+F5, when you do this then the console app will wait for you to press the enter key to continue, which means it will close.


Pretty simple.  And useless, except that it is your first program in C++.  Not something you can show Mom, or maybe you can show it to your Mom, she will smile and think you are pretty smart.  But not anyone else will be overly impressed.  Except for you.  You should be.  Now you can say you have written some C++ Code.  Good work.



What you console app might look like:


So what is <iostream>?  Why the two colons ::, return 0 and so forth.

Comments (24)

  1. cheong00 says:

    It's nice small tweak for the "Start without Debugging" behaviour… Many a time I have to add equivalent of Console.ReadKey() to the end of console application to see it freeze and let me have the chance to check the output.

    I'm interested that whether the line "Press any key to continue" is added by complier option, or just by running "the application" and "PAUSE" command sequentially…

  2. Surf4Fun says:

    Hey Cheong00,

    Good to hear from you. I am back and the C++ story is going to be awesome.

    Also check out touchdevelop.com 🙂

  3. ERandallN says:

    Using VS2012 ExpDesktop, I like not having to include "system("pause");" before my "return 0;" to see the output. Starts to compensate for the SCREAMING!!! menu headings.

    Looking forward to more from you, Surf4Fun.

  4. shewilliam says:

    I am using Visual Studio 2012 and I have followed all your instructions here but why am I getting this error:

    1>—— Build started: Project: ConsoleApplication2, Configuration: Debug Win32 ——

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Please help me! Thanks in advance. c:

  5. JamesB says:

    Hmm, had a question…

    Is there a difference between visual studio express and regular? Im kinda confused about #include "stdafx.h"

    It is similar to this other one I found earlier draalin.com/writing-your-first-c-program

    Is that include actually required??

  6. Surf4Fun says:

    Hey SheWilliam,

    Sorry about the delay.  You would get the fail if your code isn't correct for some reason, there should be an error message above it.

    Hello James B.,

    There are a lot of differences between Express and Regular or professional, the main one is in testing and using addons, as well a bunch of other stuff like the debugging capabilities.  If you are not a student and strapped for cash, then Express is likely the best thing for you.  If you have the cash to buy Regular or Professional then do so.  If you are a student then get Visual Studio Ultimate from your school's MSDNAA or Dreamspark administrator.

  7. Abednego Sibero says:

    Hello Surf4fun..

    I have installed VS 2012 in Windows 7.

    Is it problems if I Installed in Windows 7.

    And I have follow your example "Hello World" c++ in VS 2012 but I still have problem.

    Windows said : " 1>LINK : fatal error LNK1104: cannot open file 'kernel32.lib'

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== "

    can you help me about my problem?

    please send your email to "abednego.tarigan@gmail.com"



    Abednego Sibero

  8. SALMAN ELAHI says:


  9. churamani Paudel says:

    Thanks great help

  10. sara says:


  11. zack says:

    i  love  this  artical

    it  really  helped me 🙂

  12. Deepak Patil says:

    I am getting the message of, The system cannot find the path specified.

  13. Surf4Fun says:


    You may be using the wrong command line, make sure you are using the Visual Studio command line.  

  14. rajesh says:

    It's nice small tweak for the "Start without Debugging" behavior… Many a time I have to add equivalent of Console. ReadKey() to the end of console application to see it freeze and let me have the chance to check the output.

  15. Rajesh Hanswal says:

    Thanks for this help!!!

    Finally got rid of the non sense of codeblocks or devc IDE in which the headache of downloading the compiler and still it never gets compatible to the windows 8.1.

    Enjoying the ease of coding in Visual studio 🙂

  16. varalakshmi says:

    i am using opencv on visualstudio 2012…while performing build it shows an error

    1>—— Build started: Project: test, Configuration: Debug Win32 ——

    1>LINK : fatal error LNK1104: cannot open file 'opencv_core248d.lib'

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========

    i hav included the core file…still its displaying d same error

  17. Fatima shaikh says:

    this was quite good but i need to learn more with mfc also

  18. said says:

    i recommend tu use:

    using namespace std;

    in order to not use the std::cout…etc

  19. Ejigu says:

    It is nice so far. Thank you. So what is next? Where is the next lesson?

  20. Surf4Fun says:

    Hi all, will update this with XAML/C++ and Window 10, along with using the XAML/C++ on the Raspberry Pi!

  21. Hasrat Ali says:

    i done the above steeps but they not show  me a right result ( data is out of project) what is tha solution of this problem plzzz tell me

  22. maliha says:

    itz really great to learn from thiz trick

  23. Shahriar Setu says:

    Nice for starting c++ in visual stdio

  24. americk says:

    you have no idea how useful this has been. I have taken and successfully done many tutorials. but they skip the most basic things needed to use ide, setting it up, as simple as it sounds, it isn't at all. you are hit with a ton of options and it is so helpful having the basic starting point clearly demonstrated. thank you

Skip to main content