The variables in C ++


#variables

All programs need, at some point, store data entered by the user. The value of this data is represented on the command lines in what we call variables. In language C ++, as in any other language, these variables must have a type, which is associated with it so that the program can compile correctly. It is important to take into account that it is not possible to change the type of a variable after it is defined.

  • Variable types

  • Declaration and assignment of variables

  • Error when defining a variable type (unsigned integers)

  • Error when defining a variable type (signed integers)

Variable types

There are several types of variables and each corresponds to a maximum size of numbers, characters or even a truth. The main types are char, int, double, float and bool. In addition, the C ++ language also allows the modification of some of the properties of each type. So-called modifiers come on the scene – the most used are short, long, signed and unsigned, which can be combined.

Bool: in general, uses 1 byte of memory and the values True or False.

Char: in general, uses 1 byte of memory, allows you to store a character or a character string.

Int: in general, it uses 4 bytes of memory and stores whole numbers (without decimal places) with values ​​ranging from -2,147,483,648 to 2,147,483,647.

Double: in general, it uses 8 bytes of memory and stores numbers with less than 15 decimal places with values ​​ranging from 2.2e-308 to 1.79e308.

Float: in general, it uses 4 bytes of memory and stores numbers with less than six decimal places with values ​​ranging from 1.2e-308 to 3.4e-38.

As for the modifiers, they change the range of values. For example, unsigned accepts only positive sign values. The modifier long ensures that the type will be 32 bits long while shorts restricts to 16 bits.

These modifiers apply only to types int and char, with the exception of long, which can also be used in the double. The size of the variables may also vary according to the computer’s specifications.

Declaration and assignment of variables

To declare a variable, simply indicate its type followed by its name. There are several conventions about variable names. Some prefer to separate the different parts of the name with _ (low stroke). Others prefer to write a capital letter to separate them. Example:

int receitaMês;
ou
int receita_mês;

The important thing is to be consistent and always use the same convention for your own programs.

When declaring a variable, you can also assign a value to it. To do this, simply insert the equal sign (=) and the value of the variable shown:

int receitaMês = 12301;

You can also make several statements on the same line, but in this case, all variables in the line will have the same type:

int receitaMês = 12301, receitaAno = 45644545;

Error when defining a variable type (unsigned integers)

What happens if the type we choose is too small? If the number is greater than the maximum allowed by the type, the value of the variable will be the minimum allowed value for that type. See what happens:

unsigned short int número = 65535; 
cout << número << endl; 
número++; 
cout << número << endl;

If you run this code, the second line will not write 65,536, but yes 0. This goes for all types.

Error when defining a variable type (signed integers)

For signed integers, the same logic is worth. Upon reaching its maximum size, the variable returns its minimum value:

short int número = 32767; 
cout << número << endl; 
número++; 
cout << número << endl;

If you run this code, the second line will not write 32,768, but yes -32,768.

Photo: © 123RF

Related Posts