Overview

14/09/2024

Simple hello world

Below is a simple C++ program that prints 'Hello world!'

#include <iostream>

int main() {
  std::cout << "Hello world!" << "\n";
  return 0;
}

Lets go over what all this does.

#include <iostream>

The #include, incudes a library into our code, allowing for basic functions like taking input from the user, and output to the screen. If you go over to https://en.cppreference.com/w/cpp/header/iostream and look under objects, iostream has 4 objects. Iostream provides cin, cout, cerr, clog, well get into what the rest do later.

int main() {

The int main() defines our main point of our program. When our program is run, anything in the int main is ran first, it pretty much acts as the entry point for our program when its ran.

std::cout << "Hello world!" << "\n";

This may look complex at first, but once you learn the correct syntax of std::cout, it really isn't hard. We first start out by using std::cout, std stands for standard, because iostream is a standard library header, we must start with std:: and then specify what object we want to use in that library, in this case we want to output something to the screen, so we use cout. Cout stands 'console output'.

We next use '<<' to say we want to output something, not input something. Cin which takes input from the user uses '>>', pretty much think of '<<' saying out and '>>' saying in.

Next, we output what we want to output, since we want to output a bunch of characters to the screen, we need to wrap the characters in a quotation mark. A string is a sequence of characters used to represent text. If we wanted to output a number we wouldn't need to wrap the number in the quotation marks, we could just put the number directly.

We use '<<' again to add another argument to the cout, for example if you wanted to print out 2 strings, one string containing "Hello," and the other containing your name then heres how you would do so.

std::cout << "Hello, " << "Buildinger!" << "\n";

Next we use "\n", this means new line. This will cause anything else being printed to be on a newline. For example look at the following code below.

std::cout << "Hello " << "\n";
std::cout << "Buildinger!" << "\n";

If I run this, in my console I see.

Hello
Buildinger!

With my the newlines removed my code looks like.

std::cout << "Hello ";
std::cout << "Buildinger!";

And the output is.

Hello Buildinger!
return 0;

The return 0 is to indicate our program completed successfully to the operating system. If one is any other number for example 1, it indicates our program failed.

Task

At the end of completing a section, I'll ask you to complete a task, this allows you to practice using the language.

Before you starting writing C++ you'll need an IDE to actually write the code on, use a tutorial like this https://www.youtube.com/watch?v=DMWD7wfhgNY to help you.

I want you to: 1. Make a C++ program that outputs 'Hello world!' to the screen, with a newline, and 2. Make a C++ program that outputs 'Hello ' and 'YOUR_NAME' to the screen with newlines.

You've reached the end.