What are variables in C ++?

Tram Ho

Exercises to help improve case handling skills in C ++ ”]

1. Data

In the previous lesson about the statements and structure of a program , you learned that most of the code in a program is statements and statements grouped into functions. These statements perform actions to produce any result from the program with some request.

But how do programs actually produce certain results? In essence, to do so, we need to manipulate (read, change and write) data. In a computer, data is any information that can be moved, processed or stored by a computer.

Attention:

Programs are collections of code that manipulate data to produce the desired result.

A program can collect data to work in a variety of ways: from files or databases, over the network, from users provided by entering information using a keyboard or from the programmer. The input data into the program code itself. In the program Hello Word has learned, the string Hello Word! has been inserted directly into the program code to provide data for the program to use. And then the program will manipulate this data by sending it to the screen to display.

2. Objects and variables

All computers have memory, called RAM (short for random access memory), which is available to use for your programs. You can think of RAM as a series of mailboxes that can be used to hold data while the program is running. And a certain data stored in memory somewhere, called a value.

In some older programming languages ​​(like Apple Basic), you have direct access to these mailboxes.

In C ++, direct access to memory is not allowed. Instead, we access memory indirectly through an object. An object is a valuable (usually memory) storage area and other associated attributes (object details we will discuss in future lessons). When an object is determined, the compiler automatically determines the location of the object that will be placed in memory. As a result, instead of saying get the value stored in some digital mailbox, we can say, get the value stored by this object and the compiler knows the location in memory to find the price. That value. This means we can focus on using objects to store and retrieve values, and not have to worry about where they are actually located in memory.

Objects can be named or unnamed (anonymous). An named object is called a variable and the name of the object is called identifier. In our programs, most of the objects we create will be variables.

3. Initialize variables

To create a variable, we will declare that variable and call the definition (we will clarify the difference between the declaration and the following definition).

Below, an example of a variable named x:

first
int x; // define a variable named x, of type int

At compile time, when the compiler sees this statement, it will be noted that we have a variable, named x and that it is of type int (there are many different types). From that point onwards, whenever the compiler sees the identifier x, it will know that we have referenced this variable.

When the program is run, the variable will be initialized. In other words, the object will be created and assigned a memory address. Variables must be initialized before they can be used to store values. For example, we have the variable x initialized at memory location 140. Whenever the program uses variable x, it will access the value in memory at position 140. An object is initialized sometimes. Also called an instance.

4. Data types

We talked about variables being a storage area and being named, which can store a data value. A data type (often called a type) tells the compiler what type of value the variable is (for example, a number, a letter, text, etc.).

In the example above, our variable x is given type int, meaning the variable x will represent an integer value. An integer is a number that can be written without fractions, such as 4, 27, 0, -2 or -12. In a nutshell, we can say that x is an integer variable.

In C ++, the data type for a variable must be known at compile time (when the program is compiled) and that type cannot be changed without recompiling the program. This means that an integer variable can only hold integer values. If you want to store some other type of value, you will need to use another variable.

Integers are just one of many types that C ++ supports. For the illustration here of an example declaring a variable using the double data type:

first
double width; // define a variable named width, of type double

C ++ also allows you to create user-defined data types. This is what we will do a lot in the next lessons and it is part of what makes C ++ so powerful.

We will stick with integer variables because they are conceptually simple, but we will explore many other types of data in the following articles.

5. Definition of many variables

Multiple variables of the same type can be defined in a statement by separating the names with commas. The following 2 code snippets are really the same:

first
2
int a;
int b;

like:

first
int a, b;

When reporting multiple variables in this way, there are two common errors that new developers tend to make (not serious, since the compiler will know these variables and ask you to fix them):

The first error is to give each variable a data type when defining variables in sequence.

first
2
3
int a, int b; // wrong (compiler error)
 
int a, b; // correct

The second mistake is trying to declare variables of different data types in the same statement, which is not allowed. Variables of different types must be declared in separate statements.

first
2
3
4
5
6
7
int a, double b; // wrong (compiler error)
 
int a; double b; // correct (but not recommended)
 
// correct and recommended (easier to read)
int a;
double b;

Distorted

Although languages ​​allow you to do so, avoid declaring multiple variables in a single statement (even if they are of the same type). Instead, declare each variable in a separate statement (and then use a single-line comment to record what it is used for).

6. Summary

In C ++, we use variables to access memory. Variables have an identifier code, a data type, and a value (and some other attributes involved here).

In the next lesson, we will learn how to give values ​​to our variables and how to actually use them.

Share the news now

Source : Techtalk