Structure of a C++ Program

In this lesson, you will learn the basic structure of a C++ program. Mainly, a C++ program has three elements: The Libraries Section, the main() Function, and the Function Body. A C++ program, however, can have more elements, such as Constructors & Destructors, Variables sections, and more.

Your first C++ program explained

To explain the structure of a C++ program, let’s look at the famous “Hello World” program, which prints “Hello World” to the console.

// My first program in C++

This program prints "Hello World" to the console.
This program takes no input.
#include <iostream>

int main() {
  std::cout << "Hello World";

Below are the elements of the “Hello World” program with an explanation of each of them.

More like this:

1- C++ Libraries Section

Most C++ programs start with the libraries section. This section includes the libraries and namespaces that are required for a program to run. For example, the “Hello World” example needs the library <iostream> to function properly. The <iostream> library contains the Keyword cout that is used in this program.

#include <iostream>

The Keyword #include precedes the library name, which tells the compiler to include that library in the program.

2 – C++ Main Function

When you run a C++ program, the main() function is the first thing that a compiler executes. It’s the entry point of any C++ program, which may contain calls to other functions, input or output, and return keyword.

int main()

The main() function can have arguments/parameters, which are called command-line arguments:

int main(string name)

We will explain in detail the usage of arguments later in this tutorial.

3 – C++ Function Body

The function body starts and ends with the curly brackets { }.

  std::cout << "Hello World";

A missing open or close curly bracket will throw an exception. For example, missing the closing curly bracket will throw the error message to the console:

error: expected'}' at end of input

The function body in our “Hello World” example tells the compiler to use the library std and function cout (C out) to print “Hello World” to the console, which is one of the easiest forms of programs in C++.

Comments in C++ program

Comments are important parts of any C++ program. They are used by developers to leave a note anywhere in the program. The comments are ignored by the compiler and are not part of the deployed package.

A comment in C++ can be in two forms:

  • Single line comment
  • Block comment

Single line comment, such as in the “Hello World” example above, starts with //

// My first program in C++

A block comment is helpful if you need more than a line for your note. The block comment starts with /* and ends with */

 This program prints "Hello World" to the console
 This program takes no input

More C++ Program Elements

A program in C++ can have more than what we discussed above, such as a section for variables declaration, a constructor, a namespace definition, and much more. We will discuss these topics in the next few lessons.

Points to Remember

  • C++ main three sections are Libraries, main function, and the function body.
  • In C++ the main function, and any function, start and ends with curly brackets {}
  • C++ comments types are single-line comments and block comments.
Back to: Learn C++ Programming > Introduction to C++

Leave a Reply

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.