Learning C++ — Understanding Variables and Data Types

Julien Hora
codeFromTokyo
Published in
4 min readNov 10, 2023

In the last episode of this series, we took a look at the basics of C++, its importance, and how to setup your work environment. With that set, let’s dive deeper into the core concepts of C++ by looking at variables and data types.

What is a variable?

In programming, a variable is a storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value.

To visualize it simply, think of it as a labeled recipient. You can store grape seeds, sunflower seeds, etc… in a container labeled with “Seeds”.

The variable name is the way we refer to this stored value, within our programs.

The names of your variables: Identifiers

An identifier is a name given to entities like variables, functions, arrays, etc. Identifiers must begin with a letter or an underscore, which may be followed by a sequence of letters, digits, or underscores.

Which means that someName, AnotherName, _yetAnotherName, and final_name2 are all valid identifiers. However, 2name for example won’t work as it starts with a digit.

The fundamental data types

C++ offers a rich assortment of built-in as well as user defined data types.

Some fundamental data types available in C++ are:

  • int: Represents whole numbers.
    a number, no decimals.
  • char: Represents single characters.
    a character like “a”, “b” or “c”.
  • float: Represents single-precision floating point numbers.
    a number, with up to 7 decimals.
  • double: Represents double-precision floating point numbers.
    a number, with up to 15 decimals.
  • bool: Represents boolean values (true or false).
    literally can only either be true or false.

Each data type in C++ has an associated range of values it can represent, and the amount of memory it consumes.

C++ is a type safe language (mostly)

C++ is a statically-typed language, which means that the type of a variable is known at compile time. This type safety can prevent errors, such as attempting to perform operations that are not defined for that type.

Photo by Heather McKean on Unsplash

If we go back to our container analogy, you wouldn’t put water in a container labeled “Seeds”, would you?

What does it mean?

If you were to try assigning an int value to a string type variable, it won’t compile and your IDE should already be warning you that you are trying to perform an illegal operation.

// Declaring a string variable with a default value "Hello World"
string MyVariable = "Hello World"

// Assigning 33, an int value to this variable is illegal.
MyVariable = 33

Testing what we’ve learned so far

C++ uses the cin object for input and the cout object for output.

Create a file named main.cpp.

Let’s start by including the iostream library so that we can perform input and output operations.

#include <iostream>

Now we are going to go inside the main function, and declare a new variable of type int.

int main() 
{
int age;
}

We now have a variable that can hold a number. We will use it to store the age of the user of the program.

Next, add the following lines under your age variable declaration:

std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "You are " << age << " years old.";

std::cout << “Enter your age: ”will prompt to the user the message “Enter your age: ” in the console.

std::cin >> age will allow the user to input a value that we will store inside the age variable.

std::cout << “You are ” << age << “ years old.” will prompt the user with a message combining a start and an end string, “You are ” and “ years old”, with the age which was freshly stored from the user input.

The complete program in main.cpp should now look like this:

#include <iostream>

int main()
{
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "You are " << age << " years old.";
return 0;
}

Compiling and running

We now have something to try! Let’s try compiling and running our work.

In the console, enter g++ main.cpp. A new file named a.out will appear. This is the compiled version of our program.

In order to run the compiled program, in the console type ./a.out and press enter.

As you can see you are prompted for your age and told about it.

Photo by Brett Garwood on Unsplash

Congratulations! You have achieved your first C++ program.

What’s next?

This article provided an introduction to variables and data types in C++. Understanding these is fundamental to learning how to program in C++.

In the next article, we will look at how to control the flow of a C++ program with conditional statements and loops.

As always you can find the repo for this episode here.

--

--

Julien Hora
codeFromTokyo

Founder @ Dreamrunner Labs | Senior Software Engineer | Unreal Engine Generalist