Hello World!

Last section we created a C++ program that will output "Hello World!"

Let's Explain how it works line by line.

Line 1:

#include<iostream>
This tells the compiler that you are using stuff from the
iostream
header file, which contains input and output.

Line 2:

using namespace std;
This tells the compiler that you are using things declared in the
std
namespace. A namespace is an area that things are declared and will not conflict with stuff with the same name in another namespace.

Line 3:

int main(){
This is the start of the program. It is called the main function, every C++ program starts executing from the main function and it MUST appear. Anything inside the curly brackets is part of the function, and the area that the curly brackets enclose is called the scope of the function.

Line 4:

cout<<"Hello World!"<<endl;
The start of this line is cout(pronounced see-out). It is an object (a "thing") that prints output into a container called the buffer. When the buffer is full, it "flushes" so that the buffer is cleared and everything that was in the buffer gets to the console (the terminal). The buffer will auto-flush at the end of the program. The << is called the insertion operator, when used with cout, it inserts things behind it into the buffer. Endl prints a newline and flushes the buffer.

Line 5:

return 0;
This marks the end of the main function. The 0 tells the compiler that the program has ended normally, IT MUST BE A 0!!

Line 6: A curly bracket, this tells the compiler that the main function has officially ended and its scope is finished, it also must be included to pair up with the openting bracket. The return statement can also be used to terminate the function in advance, so the compiler does not know it has ended, ONLY THIS tells compiler the main function is at an end.

Note: All statements in C++ end with a semicolon, and C++ is case-sensitive, meaning that case matters, endl has a different meaning than Endl.

Note: You can also delete Line 2, and add std:: before anything that is part of that namespace, like

std::cout,std::endl,std::max,std::min,std::sort
and much more.

Now head to the Next Page to learn how to print other things with cout.