Your First Program

The Code

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

Explanation

Let's go through this line-by-line.

#include <iostream>

This goes and includes some standardized libraries for input/output. It is mostly used for input and output to the Console.

The '#' at the beginning tells the preprocessor that the rest of this line isn't C++ code, but something for the preprocessor to do. Here, our command is 'include', which tells the preprocessor, "Go find this and bring it back to me". Here, we are looking for iostream, which we plan on using to print some text to the console.

Although here we tell the preprocessor what we want in angle brackets (< and >), you can also use quotes ("). Whichever you use is mostly style, but typically people use angle brackets for external libraries, and quotes for their own files.

int main()
{
    // ...
}

Here, we are defining a *function*. Although I won't get into the details right now, the main (no pun intended :P) thing you need to know is the function called main is special. It lets your computer know where to start running your code.

Here, we have several parts. We have int, main, (), and { ... }. Each of the these serve a special purpose. As you probably see, main is the function name. The other parts are important, too, but you will learn about that in a later tutorial, Functions.

std::cout << "Hello World!" << std::endl;

This is where magic happens. It might look slightly overwhelming, though, so we'll break it up in parts.

The first part, std::cout, stands for Console Output. When you write information to it, whatever you write appears on screen. How do you write to it? You use the left bitshift operator, which is overloaded for std::cout. Don't worry about what that means right now, just know that that is how you write data to std::cout.

Since we covered the overloaded left bitshift operator (<<) in the last paragraph, next is "Hello World!". If you run the program, you'll notice the quotes don't appear. Why? The quotes indicate that what is inside is a string1. A string is pretty much a piece of text.

Next, we see another left bitshift. This is because the way it was overloaded allows it to be chained. If you couldn't chain it, you could have to have "Hello World!" and std::endl print in separate statements.

Now, we get to std::endl. What in the world is that? It stands for End Line. It makes the output cursor go to the next line2. You could also put \n3 inside a string.

We're at the last part of that statement, now. Wait, a semicolon? What's so important about that? It tells the compiler that you are done with that statement. If you forget it, you'll get a bunch of syntax errors.

Summary

Congratulations, you just finished your first program!

You printed some text to the console. It may not sound like much, but you're on your way! Next we get to do some math. :)

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 3.0 License