C++ "Hello, World!" Program

C++ "Hello, World!" Program

In this example, we will learn to create a simple program named "Hello World" in C++ programming.

A "Hello, World!" is a simple program that outputs Hello, World! on the screen. Since it's a very simple program, it's often used to introduce a new programming language to a newbie.

Let's see how C++ "Hello, World!" program works.

If you haven't already set up the environment to run C++ on your computer, visit Install C++ on Your Computer.


C++ "Hello World!" Program

// Your First C++ Program

#include <iostream>

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

Output

Hello World!

Working of C++ "Hello World!" Program

  1. // Your First C++ Program

    In C++, any line starting with // is a comment. Comments are intended for the person reading the code to better understand the functionality of the program. It is completely ignored by the C++ compiler.
  2. #include <iostream>

    The #include is a preprocessor directive used to include files in our program. The above code is including the contents of the iostream file.

    This allows us to use cout in our program to print output on the screen.

    For now, just remember that we need to use #include <iostream> to use cout that allows us to print output on the screen.
  3. int main() {...}

    A valid C++ program must have the main() function. The curly braces indicate the start and the end of the function.

    The execution of code beings from this function.
  4. std::cout << "Hello World!";

    std::cout prints the content inside the quotation marks. It must be followed by << followed by the format string. In our example, "Hello World!" is the format string.

    Note: All C++ statements must always end with a ;.
  5. return 0;

    The return 0; statement is the "Exit status" of the program. In simple terms, the program ends with this statement.

Things to take away

  • We use std:cout in order to print an output on our screen.
  • We must include iostream if we want to use std::cout.
  • The execution of code begins from the main() function. This function is mandatory. This is a valid C++ program that does nothing.
int main() {
    // Write your code here
}