Language: EN

cpp-primer-programa

Our First Program in C++

Now that we have our work environment set up, whether it be Visual Studio, Visual Studio Code + GCC (or another that 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 classic in programming, used in many languages as the first application.

So assuming you already have your favorite IDE installed and configured correctly, let’s take a look at 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 named 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 program execution begins
int main() { 
    // Print the message to the console
    std::cout << "Hello World from LuisLlamas.es!" << std::endl;

    // Indicate that the program ended successfully
    return 0;
}

Now, run this code by pressing F5 (it’s the same key in Visual Studio as in Visual Studio Code) and we will see what it displays on the screen.

Hello World from LuisLlamas.es!

What’s happening here? When you run this program, the compiler translates the source code you have written into a language that 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 “Hello, World!” on the console.
  3. The execution ends with return 0;, indicating that everything went well.

Let’s take a deeper look.

Explanation of the Code

Let’s examine each part in depth.

int main()

In C++, every program must have a main() function, which is the entry point where execution begins.

  • int before main indicates that the function returns an integer value (see later return 0).

#include <iostream>

The keyword #include indicates that the compiler should 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 on the console (using the functions from iostream)

  • std::cout is used to print information to the console.
  • The operator << is the insertion operator that “sends” the text "Hello, World!" to the output stream.
  • std::endl is used to insert a newline after the output (so that the message does not remain on the same line as the console cursor).

return 0;

This line indicates that the main() function finishes successfully and returns the value 0 to the operating system. This indicates that there were no errors during execution, and it is considered good practice to indicate that the program has finished successfully.

Expanding the Program

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

Modify the Message

You can change the message that is printed to the console. Simply modify the text between the quotes.

std::cout << "Welcome to programming in C++" << std::endl;

More Output Lines

You can add more output lines with multiple std::cout statements.

std::cout << "Hello, World!" << std::endl;
std::cout << "This is my first program in C++." << std::endl;
std::cout << "I am learning to program!" << std::endl;

Use Variables

As you progress, you will learn to use variables in C++. Here is a simple example with a variable of type string.

#include <iostream>
#include <string>

int main() {
    std::string greeting = "Hello, World!";
    std::cout << greeting << std::endl;
    return 0;
}

In this case, we have introduced a variable called greeting that contains the message, and then we print it to the console.