cpp-primer-programa

Our First Program in C++

  • 4 min

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;
}
Copied!

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!
Copied!

What’s happening here? When you run this program, the compiler translates the source code you wrote into a language the computer can understand.

  1. The execution flow begins in the main() function
  2. When it reaches the line containing std::cout, the program displays the message “¡Hola, Mundo!” to the console.
  3. 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

Expanding the Program

The program we made is very simple. Let’s see some modifications you can make to practice.