The default code explained

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. Introduction

    /

  4. The default code explained

The code inside the file main.cpp

It is time to explain the default code inside the file main.cpp!

Note that the default code may be different if you use an other IDE than Code::Blocks, but it should be similar. If you want, you can copy the default C++ code of Code::Blocks to the code editor of your IDE:

main.cpp

1
2
3
4
5
6
7
8
9
#include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; return 0; }

Line #1

By default, not all the functionalities of C/C++ are included in a project. We have to include, when needed, specific files that will provide the tools that we need. The first line includes the file iostream of the C++ Standard Library. That file provides tools to write and read text into/from the console opened at the start of the program (When it is a console application).

What is a library in C/C++?

A library is a set of tools that we can add to our projects to get access to more functionalities.

Standard libraries

The C++ Standard Library is the official library of the C++ language and the C Standard Library is the official library of the language C. We do not need to install them, they comes with the C/C++ compilers and they are the same no matter what compiler or operating system we use.

Note that in C++, we can use the C Standard Library, but in C, we can not use the C++ Standard Library.

Other libraries

We can install and use other libraries and it is often done since even though the C/C++ Standard Libraries offer a very great amount of tools, there are many things they do not allow us to do. Those libraries can be multi-platform (Available on many different systems (Ex. Windows, GNU/Linux, Mac, Android, iOS, ...) or specific to one operating system (Ex. Windows only).

Line #3

Let us consider that, in our code, we name something name, and that we include a library that also name an other thing by the name name. That would cause a conflict (Name collision)… and an error. To avoid that, there is a C++ feature called namespace. The C++ Standard Library use a namespace named std. That means that if an element of the C++ Standard Library is called name, it will actually by referred to as std::name. However, writing std::name takes more time than just name (And it takes more space on the screen...). The line #3 makes it possible to forget the std:: before the name of the elements of the C++ Standard Library. Without that line, the line #7 would have to be: std::cout << "Hello world!" << std::endl;.

Using instructions like in the line #3 is however considered a bad practice. Namespaces are used for a good reason (Avoid name collisions). In the rest of that course, we will not use the using namespace directive and always write the std:: prefix of the elements of the C++ Standard Library.

Line #5

That line contains the definition of the main function (We will learn, later, what are functions in C/C++). Basically, what is inside the braces {} after int main() (So between the line number 6 and 9 here), is what will be executed by the program. When the end of the braces of the function main is reached (Line number 9 here), the program ends.

Line #7

cout is an object (We will learn what that is later) that is used to write text to the console. In C/C++, text needs to be surrounded by quotes "". The part cout << "Hello world!" means: Send the text "Hello world!" to the object cout that will then display the text inside the console. Then, the part << endl means: Tell cout to change the cursor of the console to the start of the line below (Create a new line inside the console). Finally, the semicolon ; is there to mark the end of the instruction. Every instruction in C/C++ is ended with a semicolon ;.

Line #8

The keyword return means: Exit the current function (We will soon learn what a function is) and return the value following the return keyword (Nothing if there is nothing). Since the line number 8 is inside the function int main(), which is the main function of the program (So when it ends, the program ends), when that line is reached, the program ends. Since the keyword return is followed by the number 0, the value zero is returned (Which in that case is an error code used to tell the operating system that the program ended correctly). Finally, there is a semicolon ; because, well, every instruction ends with a semicolon ;.

Note that when the end of the braces of a function is reached, the function stops even if there is no return keyword. That keyword is used to either return a value or end the function before the end of the braces is reached.

Conclusion

That was a long explanation, but that is because it was a pretty detailed one. I never said that coding was easy, but, believe me, it is very rewarding. It is normal if those explanations felt vague, it should become clearer as you progress in your journey to master C/C++!