Create a 'Hello World' Console Application in Visual C+
Create a very simple program in C++ that displays the text 'Hello World' and then exits.
Steps
- Install Visual C++ 2010 Express. This program is a compiler and IDE combo for creating C++ programs. We will be using only a small bit of its possibilities to create this 'Hello World' program.
- Set up a new project. In the top-left of the window is a picture of a window with a star in the corner. Click on that to open the new project creation wizard. Select "Win32 Console Application" and in the name box, type in whatever you want. On the next pop-up, click finish.
- Few different setup steps. When you click finish, the page that appears after will be the source code of your program. To comply with C++03 standards, change the 'int _tmain(int argc, _TCHAR* argv[]) {' to 'int main() {'. Now on the left, expand the 'Header Files' folder and open 'stdafx.h' in a new tab. Place the line '#include <iostream>' after the line '#include <tchar.h>' to allow access to the standard in/out streams (basically, allows user input and output to the console).
- Using Namespace std. Back at the main .cpp file, place 'using namespace std;' after the line '#include "stdafx.h"'. This tells the compiler to automatically assume that for every command, that the standard library is being used, transforming what would normally be 'std::cout<<x;' into 'cout<<x;'
- Creating the rest with explanation. Okay, now that everything is set up, go to the main function ( int main() ). This is a special function that is the first thing ran every time the program is called, and from which the rest of the program stems. Create a new line and write 'cout<<"Hello World";' underneath 'int main() {'. What this does is output the string literal "Hello World" to the standard output stream, which is the console window. The command is ended with a trailing semicolon to tell the compiler that that is the end of the command. To keep the program from closing before the output can be seen, also add the line 'cin.get();'. This waits for the user to press enter before continuing the program (aka returning 0).
- Cleaning up and building. Make sure that after 'cin.get();' you preserved the 'return 0;' and curly brace, because these are crucial to the program (the return function shows that the program worked correctly, and the ending bracket signals the end of the function's contents). Once you are sure that everything is done correctly, press [F7] to begin the build process.
- Testing the program. Open your projects file (usually found at "C:\Users\<yourname>\Documents\Visual Studio 2010\Projects" in Windows 7) and find your program (it will be in the file with the same name you gave it). Open the debug file, then run the .exe file inside and check the results. You should have the text "Hello World" printed to a console screen.
Tips
- Instead of building, searching your .exe and running it manually, you could also press "F5" instead of "F7" which will build and run your program in it's current configuration (Release or Debug).
- To find your .exe check your "bin\Debug" or "bin\Release" folder inside your projects folder. The "Debug" folder usually includes a .exe which comes with debug symbols to simplify your search for runtime errors. Your Release folder includes a cleaned and optimized .exe that you would ship to your customers. Remember to test both when building a bigger Project.
- If the program doesn't run or compile correctly, re-read the steps and make sure that you typed out everything correctly.
Things You'll Need
- A Windows computer
- Visual C++ 2010 Express (or Studio, if you happen to have it)
Related Articles
- Edit the Background of a Presentation in AppleWorks 6
- Generate Glass Cutting Pattern Using GNCutter
- Load an Image Onto a PC Using Ghosting
- Replace the Boot Drive on a PC Using Norton Ghost