Hello World in C++

In this C++ programming tutorial we will briefly explore the typical hello world program which demonstrates the bare bones that virtually every program will share. No matter how simple or complex every C++ program will require these few basic lines of code.

  #include <iostream>
  using namespace std;
  

Here is whats called a pre processor directive. The 'i' and 'o' in iostream stands for input and output. This is absolutely required in every C++ program. The using namespace std is telling the compiler that you will be using the standard library. Without stating this you would not be able to use cout, cin, along with endl. More on that in just a few moments.

  int main()
  {
  

Now here is where we start the main program, this is where all the action happens. The int stands for integer which is what will be passed back to the program (more on that in a second) also don't forget the parenthesis even though they are empty they are still important. Last is the curly brace, it's necessary so don't forget. Now let us print something to the screen using console out.

  cout<<"Awesome! My first program in C++"<<endl;
  return 0;
  }
  

If you're familiar with C you should realize that the cout acts like the printf statement. The cout prints text directly to the screen whatever is in the quotation marks. The endl means to end the line and continue on the next, this is much like hitting enter on the keyboard when typing a document also don't forget to end with the semi colon as this is required. The return 0 is the integer that is returned to the int main, don't forget the ending curly braces this signifies that main is finished. Here is the full program.

#include <iostream>
using namespace std;
int main()
{
cout<<"Awesome! My first program in C++"<<endl;
return 0;
}
  

So there you have a full functioning running program! Well okay maybe not full functioning but this is the basic structure on how all programs will look so do your best to memorize and understand it. Also remember how in order to use cout and endl you had to declare that you were using namespace std if not the compiler would throw an error and not run however there is another way. If using namespace std was not declared then every line of cout, cin, and endl would have to be declared with std::. Here is an example, the difference in this program will be highlighted in blue for clarity.

#include <iostream>
int main()
std::cout<<"Awesome! My first program in C++"<<std::endl;
return 0;
}

If you are new to c++ or programming in general then you will quickly learn that cout, cin, and endl are frequently used and this method would soon become very tedious therefore telling the compiler that you will be using namespace std saves a lot of time.