Skip to content

Hello World Program

Nesdood007 edited this page Jan 18, 2017 · 8 revisions

Writing Your First C++ Program

The Hello World Example

In order to introduce you to programming in C++, we are going to start with a very simple program that prints the words Hello World to the Standard Output of the Terminal.

See Code Here

#include <iostream>

using namespace std;

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

How the code works

#include <iostream> includes the C++ IOStream Library, which handles printing to and receiving input from the terminal. We need this in order to print "Hello World" to the terminal. Also it is worth mentioning that anything with the # symbol before it is telling the compiler to do something.

using namespace std; imports the functions and public variables from the std namespace so that one can type statements like cout versus std::cout and have it behave the same way. More on that later.

int main () is the Main Function header, which returns an int. Some Main function headers take in Character Arrays for Arguments. More on that later.

cout << "Hello World" << endl; prints out the text "Hello World" to the terminal. cout is the output stream that is written to the stream, so in order to send something to that stream, you need to use angles brackets "<<" to route something there. endl is another way of adding a new line to the terminal output.

return 0; Returns zero and hands control above the main function, which in this case ends the program.

The Difference between Java and C++

The hello world program in Java is similar, however there are many differences between Java code and C++ code. For instance, the method of printing out text to the console in Java uses System.out.print() whereas C++ uses cout, and the <iostream> library must be included. Do not expect Java code to work in C++ at all.

Clone this wiki locally