C++ Programming : Hello World!

After you have done the previous tutorialwe will write a simple program to display “Hello World!” text in our console. Here is the step :

  • Assume you already have an empty .cpp file in the previous tutorial.
  • C++ file usually has three important parts : header declaration, variable initialization, and main function.

Header declaration

  • We declare all necessary header which used in our program.
  • To display “Hello World!” text in our program, we need standard input/output library and string library.
  • Here is the header that will be used:
// header declaration
#include <iostream> // handle standard input/output
#include <string> // handle string type
using namespace std;

Variable initialization

  • We initiate all variable (global) which used in our program.
  • Here is the variables that will be used :
// variable initialization
string text;
int key;

Main function

  • We write our program in the main function because this function will be the first function to be executed after the program running.
  • Here is the program to display “Hello World!” text in our console :
// main program
int main(int argc, char** argv)
{
text = “Hello World!”; // text that will be displayed
cout << text; // display text
cin >> key; // take integer input
return 0;
}

Executing the program

  • In Visual Studio, we run the program by clicking green play button.

runbutton

  • And the result is :

resulthelloworld