Hello world with C++
Here are the steps:
- Open Microsoft Visual Studio and create new project. If you copy-paste code directly into the program or open a pre-written .CPP (C++ ) file, it will not compile properly, therefore, it won’t run.
- Go to File -> New -> Project
- Then pick C++ from the main menu located in the window that pops up. A submenu will drop down; select WIN32 on the left hand side.
- Afterwards, select Win32 Console Application on the right hand side.
- Give it a name.
- Click OK.
- After the project is created Microsoft Visual Studio will ask you to configure the project.
- Click Next on the overview page to customize your project a little further.
- One the second screen, please select
- Console Application under Application type
- Empty Project under Additional options
Your project is now configured.
- After the project is done, you are ready to start programming.
/ Typically on the left hand side you will see The Solution Explorer. This is actually the file structure of your project. Microsoft Visual Studio creates 3 folders by default. Header files, Resource files, and Source files. Header files folder usually contains header file that you include into each file, created by the programmer. Resource files folder usually contains images or text files that I needed for the program. Source files folder contains the actual source code of the program. You will be creating your CPP files and they must be saved in Source files folder in order to run.
- To create new file right lick on Source files -> Add -> New item. A dialog window will pop up. Select C++ File (.cpp) option. Then give it a name and hit Add.
The file will be displayed in Source files folder and will be opened on the screen.
- Hello world program
#include <iostream>
int main()
{
std::cout < "Hello world\n";
return 0;
}
- Program should begin with including IOSTREAM library.
#include <iostream>
IOSTREAM library provides input – output functionality using strings. IOSTREAM is object-oriented library .
- Define MAIN function, data type must be INT
int main()
{
}
Int main() identifies where the operating system should start executing the program
c. Print Hello world on the screen by using COUT command. Please note that since namespace has not been defined , a prefix STD should be used with following syntax
std::cout < "Hello world\n";
STD = standart
< - defines where complier should start executing COUT command
\n – inserts a new line
- Return proper INT value to the operating system
return 0;
(Comment: All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. http://www.cplusplus.com/doc/tutorial/program_structure.html)
- Verification. In order to test the program, please go to Debug -> Start without debugging or use Ctrl+F5. Your outcome should look like: