Hello World in C++, the long way

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Hello World

Finally, we get to the notorious program.


#include <iostream>

int main()
{
    std::cout << "Hello World!" << std::endl;
}

#include <iostream> lets the compiler know about functions from the iostream header. Header files provide a list of the funtions and variables from other source files. You can think of them as a code equivalent of a table of contents in a book. The iostream header lets the compiler know about std::cout and std::endl. This std:: thing is there because most of the standard library is put into what's called a namespace. To get anything inside a namespace you have to preface the name of the variable/function with nameOfNamespace::. For all of the standard library, this would be std::. If you don't want to have to type std:: every time, you can instead use:


#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
}

The as you can see above, to output anything, first use cout << followed by whatever text you would like to output. In addition, any extra << after will act as glue and stick "components" together. endl represents a newline "component". We could also just use "HelloWorld!\n" since \n represents a newline character, however, Windows uses \r\n and endl will automatically use the correct one.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content