< Language Tutorials

Here are some instructions on how to make a program in the C++ programming language that would say "Hello World!".

Prerequisites

A text or code editor or an Integrated Development Environment (IDE in short) is required to write this program. There are many options available online.


Note Warning: Be careful when downloading programs from the internet; make sure to ask your parent or guardian first and download compressed files. If you just want to see how this works then use an online IDE.

Scratch Code

If you were making this with Scratch, you would code it like this:

when gf clicked
say [Hello World!]

C++ Code

The C++ code for this very similar to the Scratch code shown above. But there are some things you have not seen in Scratch which are needed for this program. They are #include <~insert library name here~> , using namespace ~namespace~, int main, and cout << "~enter what you want the program to say here~". Among these the first three are needed for every single C++ program, whereas cout is only needed when you want your program to say something.

Now, the C++ code is-

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
}

Explanation

  • #include <iostream> loads the neccessary libraries for the project to be able to output text.
  • using namespace std tells C++ that your program is using the output functions of std, which means "standard".
  • int main() defines the main function, which is what the program will run when started.
  • cout << "Hello World!" uses cout to output text; the << function sends the defined string "Hello World!" to the cout function.
  • Everything other than #include and statements that hold multiple lines of code (which would be similar to C blocks such as repeat () and if <> then in Scratch) must have a semicolon (;)at the end, to indicate that the line is finished.
  • Statements that can contain multiple lines of code, such as int main(), use curly braces ({ and }) to represent the body of the function.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.