3. Basic C++ program and it's structure

Let start by opening the code editor and :

  • - create a source file that will contains our basic program source code. Call the file hello.cpp.
  • In order for the compiler to recognize the C++ file, the file must have the extension : .cpp or .cc or .cxx, it depend on compilers but the most used extension is .cpp.
  • - write the below code in the source file :


#include <iostream>


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


  • - To compile and run this program, execute these 2 commands :
// To compile
g++ hello.cpp -o hello


// To run
./hello


  • - You should see the result in your code editor or terminal :



Code explanation


C++ standard library

One of the main strength of C++ is it's rich and useful standard library. Library is collection of software resources like classes, functions, types etc... written by others programmers that we can import and use in our own program.

The C++ standard library provides as a lot of component that will facilitate our software development. For example to display data on the screen, or receive user input.

All these services are provided to use through header files that we include in our project.


#include directive

This is a preprocessor directive to instruct the compiler to include a header file before compilation. The preprocessor act before the compiler and work with him.

In our code we have at the start :

#include <iostream>


This code means :

#include = the prepocessor directive. It always start with #.

<iostream> = is the name of the header file to include. This header file contains classes and functions for input and output functionalities (at the beginning of the name i = input and o = output).

NB : There is no semi-colon at the end.


Program entry point

C++ program start it's execution in a special function called main. without this function the program won't compile.

This function look like this :

int main() {

}


main = is the function name, in all lowercase.

() = these parentheses serve to transmit data or argument to the function. See this article

{} = curly braces limit the function body. From the opened bracket ({) to the closed bracket (}), everything between belong to the main function.

int = is an integer, it means the function should return an integer.


Statement

Is a command the program will execute when it runs. Statements are written at the start of lines and always end with semi-colon.

In our code we have this statement :

std::cout << "Hello world" << std::endl;


std = is the standard library namespace (we will learn about them later.). This means we are about to use a code from the standard library. Here it's cout.

cout = c means character and out means output. So cout is used to output characters or display characters on the screen.

<< "Hello world" = This is used to send the "hello, world" string to the std::cout so it will be displayed on the screen.

std::endl = std is the standard library and endl means end line. It adds a newline character to the cout so the cursor will go to the newline after printing the string.

This std::cout and std::endl are define in the iostream header file we included at the top, and now we have access to this code to use for free.


There is another statement before the closing curly brace :

return 0;


This statement instruct to return 0 when the program finish running.