Now that we have our working environment set up, whether it’s Visual Studio, Visual Studio Code and GCC (or another you have chosen), it’s time to see our first program.
This first program will be a console application that simply displays “Hello World” on the screen. It’s simple, but it serves to check that everything is correct and as a starting point.
The “Hello, World!” program is a programming classic, used in many languages as a first application.
So, assuming you already have your favorite IDE installed and configured correctly, let’s see our first code 👇.
Creating a New C++ Project
We start by creating a new folder for our project, for example HelloWorld, and inside it a text file called main.cpp.
You could choose any other name
Write the following code in the main.cpp file:
// Include the standard library for input and output
#include <iostream>
// Main function where the execution of the program begins
int main() {
// Print the message to the console
std::cout << "Hello World from LuisLlamas.es!" << std::endl;
// Indicate that the program finished correctly
return 0;
}
Now, run this code by pressing F5 (it’s the same key in both Visual Studio and Visual Studio Code) and we’ll see what appears on the screen.
Hello World from LuisLlamas.es!
What’s happening here? When you run this program, the compiler translates the source code you wrote into a language the computer can understand.
- The execution flow begins in the
main()function - When it reaches the line containing
std::cout, the program displays the message “¡Hola, Mundo!” to the console. - Execution ends with
return 0;, indicating everything went well.
Let’s look at it in depth
Code Explanation
Let’s look at each part in depth
int main()
In C++, every program must have a main() function, which is the entry point where execution begins.
intbeforemainindicates that the function returns an integer value (seereturn 0below).
#include <iostream>
The #include keyword tells the compiler to include the contents of the iostream library before compiling the code.
This library contains functions for performing input and output tasks, such as displaying information on the console (for example std::cout and std::endl, which we see below)
std::cout << "Hello, World!" << std::endl;
This line is what makes the program print the message to the console (using the functions from iostream)
std::coutis used to print information to the console.- The
<<operator is the insertion operator that “sends” the text"Hello, World!"to the output stream. std::endlis used to insert a line break after the output (so the message doesn’t stay on the same line as the console cursor).
return 0;
This line indicates that the main() function ends correctly and returns the value 0 to the operating system. This indicates there were no errors during execution, and it’s considered good practice to indicate the program has ended successfully.
Expanding the Program
The program we made is very simple. Let’s see some modifications you can make to practice.
