Day 2 - 100 days of code with C++
Today I'm going to talk a little about variables, what they are and what their role is. or set of hundreds of commands. Each type of variable we create has, in addition to its name, a data type that defines the characteristic of that value in memory, for example:
Above we are saying that a variable - some value that will occupy a memory space - will be called varInteger and this variable will receive a value of type integer, that is, a value that is not fractioned.
Therefore, it is visible that, after compiling the code, the following message will be printed: The name integer variable is: 10.
Like the integer data type, C++ has other types, such as:
- type float - short precision.
- type double - long precision.
- type char - a character.
- type bool - value true or false.
Now you must be wondering: **" where is the string type ? "** Calm down, I'll talk about her soon...
Therefore, if we want, for example, to print a character on the screen, we would use a variable of type char, such as:
Therefore it will print on the screen - output - the message **The value of char variable is: D.**
" ok, but where is the string data type? … "
**ok ok, let's talk a little bit about.**
Unlike other data types, in C++ when the programmer wants to work with string input data types, he will have to include a new library in his code.
Complicated ? a little bit, but that's ok, see the example below:
In this image there is a lot - but don't be scared - let's focus only on the #include library that will be called in the body of our code by the std::string command assigned to the variable nameAluno.
Therefore, when we use the string nameAluno, that is, assigning a name, it will be using the resources contained in the library #include <string>.
It is important to remember that variables do not take that name by chance, if they are values that are allocated in the computer's memory, this means that they are susceptible to undergo some type of change - or not and in some cases, such as constants that their values are immutable.
Variables, due to their mutability ability, are fundamental for the execution of hundreds of thousands of programs, and how they behave in the pc's memory, because knowing how to use them you as a programmer will consequently determine the performance / efficiency of the program. That's why languages like C or C++ are still important languages in didactic use, because everyone who is learning is inevitably forced to learn to use them according to their type and according to their purpose.