Lesson 3: Breaking down our first program

Now let’s break down the code we see in our HelloWorld.cpp file.


#include "stdafx.h"
#include <iostream>
int main()
    std::cout << "Hello Beautiful World of Programming" << std::endl;
    return 0;



The big kahuna here is the “main” function.  This is the main body of our program.  The execution of said program will start here.  It is preceded by an int (which means integer) return value type.  I will be discussing variables and variable types in Lesson 3.  For now, just know that main() is usually an int, which means it returns an integer number.

int main()


Now the code we wrote went inside of your main() functions curly brackets {}.

Everything inside of the curly brackets are statements.

Each statement must end with a ‘;’  This is super important! Don’t’ forget this!

I’ve seen so many code breaks that had a root cause of no trailing ‘;‘!

int main()
{ #Everything in here is a statement.
    [all of your statements go here];
    return 0;


Essentially, a function is made up of statements.
And statements are just instructions.

Pretty simple so far huh?

Now, let’s take a look at the line of code that actually prints our words to the screen.


std::cout << "Hello Beautiful World of Programming" << std::endl;

I know what you’re all thinking.  STD?  Really?  We’re giving our program an STD?!?! What the heck!

OK, maybe nobody is thinking that, but it made me chuckle…

std:: is calling the standard namespace.  But what’s a namespace you ask?  Well let me just tell you!

A namespace is basically a group or collection of functions and other data.  The information in this particular namespace is pulled from the standard library.

Why do we do this?  As an example, let’s say we have two libraries that have a function called print() that each do different things.  You can’t just import the libraries and then write code that says “print()” because the compiler won’t know which library to pull that information from.  So you would create a namespace for each of the libraries so you can preface your print() function so the compiler knows which library to pull from.  myNamespace::print() and yourNamespace::print().

NOTE: It is possible to import a namespace so you don’t have to preface all of your code.  This is technically considered bad programming practice but still warrants mentioning.  It makes things easier, but can cause conflicts in the future.  It’s best practice to just preface the pieces of code that need to be pulled from the namespace.
Notice in the code below that cout and endl are not prefaced with std::
#include <iostream>
using namespace std;        
int main()
    cout << "Hello Beautiful World of Programming" << endl;
    return 0;

Ok, let’s get back to the rest of our code (the concepts of the rest of the line are pretty simple).


We’ve talked about what std is, so let’s talk about “cout” now.

cout is the standard output stream. Think of it as whatever you pass into that stream will be output to the screen.

The string in “” is passed to the output stream by the left facing chevrons <<.

cout << "Hello Beautiful World of Programming"

If we take the concept we just learned, we can guess that we are passing endl into the cout stream as well.

And we’d be correct!  Endl adds a new line to the end of our string, and also calls the flush() function which makes sure the file stream is updated with our data.  Some operating systems require a flush at the end of a cout.  This is not the case for Windows, but if you are having issues getting your text to display, try adding an endl or flush to the end of this line if you haven’t already.

cout << "Hello Beautiful World of Programming" << endl;

The last piece of our code is the return statement.


The “return 0;” portion of the main function basically just means “SUCCESS”.

return 0;

A return code of 0 in Windows means something completed successfully.

So if we run through all of the statements (instructions) in our main function successfully and hit the “return 0;” portion, we return the integer 0, which the OS takes as SUCCESS.


Alright, that covers all of the main concepts and pieces of code in our first program.

If you have any questions or comments on any of the information, please make sure to let me know in the comments below and I’ll work on getting you answers.


Good luck out there fellow knowledge seekers!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s