Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Types of Variables | Variables
Introduction to C++ | Mobile-Friendly

bookTypes of Variables

In the previous chapters, we printed some expressions. However, what to do if we need to store these expressions for future usage? In C++ (like in other programming languages) there are containers to keep data - variables. For each type of data (numbers, strings, characters) there is a type of variable.

Let’s take a look at the most often used types:

Why do we need to know the type of each variable? In Python, for example, you can store the data without worrying about its type.

When you declare variables in C++, the type of variable is a mandatory parameter, so knowing and correctly using data types will help to avoid many mistakes.

Let's look at an example of a variable declaration:

12
int num = 42; cout << num;
copy

You can also reassign your variable if it’s needed:

1234
double num = 3.2; cout << num; num = 12.44; cout << num;
copy

You might also declare a variable but assign the value later or define the multiple variables of the same type in one declaration:

 int a, b;
 a = 4;
 b = 3;
question mark

Choose the correct way to declare the variable a type of string.

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 3.7

bookTypes of Variables

Sveip for å vise menyen

In the previous chapters, we printed some expressions. However, what to do if we need to store these expressions for future usage? In C++ (like in other programming languages) there are containers to keep data - variables. For each type of data (numbers, strings, characters) there is a type of variable.

Let’s take a look at the most often used types:

Why do we need to know the type of each variable? In Python, for example, you can store the data without worrying about its type.

When you declare variables in C++, the type of variable is a mandatory parameter, so knowing and correctly using data types will help to avoid many mistakes.

Let's look at an example of a variable declaration:

12
int num = 42; cout << num;
copy

You can also reassign your variable if it’s needed:

1234
double num = 3.2; cout << num; num = 12.44; cout << num;
copy

You might also declare a variable but assign the value later or define the multiple variables of the same type in one declaration:

 int a, b;
 a = 4;
 b = 3;
question mark

Choose the correct way to declare the variable a type of string.

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1
some-alt