Min menu

Pages

VB

The concept of variables in C++

The concept of variables in C++

Variables variable ) These are places that are reserved in memory for the purpose of storing data while a program is running.
The type we give to a variable makes the operating system determine what type of data can be stored in the memory space reserved for that variable.

For example, if you want to store a number in a variable, the variable type must be originally intended to store numbers to allow you to do so.
And if you want to store text in the variable, the type of the variable must be originally intended for storing texts, and so on.

Example

main.cpp
                    #include <iostream>
                    using namespace std;

                    int main()
                    {
                    int a = 10; // its value is 10 a here we have defined a variable named
                    cout << "a = " << a; // a Here we have printed the value of the variable

                    return 0;
                    }
                  

We will get the following result when running.

                    a = 10
                  

Methods for defining variables in C++

In the beginning, the types of variables are the same as the primary data types that we mentioned in the previous lesson, which are:
int -  float -  double -  bool - char

Now, through examples, we will teach you several methods that you can follow to define variables and reduce the size of the code.


 Defining multiple variables of the same type without giving them initial values.

If you want to define multiple variables of the same type without giving them initial values.

You can define them like this.

                    int a;
                    int b;
                    int c;
                  

You can abbreviate the code as follows.

                    int a, b, c;
                  

 Declare multiple variables of the same type and assign them values ​​immediately upon creation

In case you want to declare multiple variables of the same type and give them values ​​right when they are created.

You can define them like this.

                    int a = 10;
                    int b = 20;
                    int c = 30;
                  

You can abbreviate the code as follows.

                    int a=10, b=20, c=30;
                  

 Defining several variables of the same type, but their nature is different and without values.

In the event that you want to define several variables of the same type, but their nature is different and without values.
Here even though it's  a a variable,  b it's a unary array, and  c it's a binary array; But they are all of their kind int.

You can define them like this.

                    int a;
                    int b[5];
                    int c[3][4];
                  

You can abbreviate the code as follows.

                    int a, b[5], c[3][4];
                  

Important terms about variables in C++

Focus on the following concepts to know how to correct errors that you may encounter when writing the code.

  • The process of defining a variable without giving it a value is called Declaration.

  • The process of assigning a value to a previously created variable is called Assigning.

  • The process of defining a variable and assigning it a value immediately upon creation is called Initialisation.


Comprehensive examples of variables in C++

Here we did Declare For a new variable, that is, we define a new variable without giving it an initial value.

                    int a;
                  

Here we did Assign to a variable, that is, we assign a value to a variable that originally existed.

                    a = 10;
                  

Here we did Initialise For a new variable, we create a new variable and give it an initial value.

                    double Sum = 0;
                  

note

If you declare a variable without giving it any value, and then try to print its value, the language interpreter C++ Returns the last value that was in memory where this variable was.
So this happens if you try to print the value of a variable and you only do it Declare.

Important tips for handling errors in C++

Now that you understand the basic terminology of variables, we will now give you some examples that contain intentional errors to teach you how to define the nature of the error and thus how to fix the error yourself.


The first error is displaying the value of a verb variable for it Declare Just without doing it Assign.

Sometimes the programmer defines a variable and displays its value before giving it any value, so random values ​​appear in it.
That is, the problem is that he tried to display the value of a verb variable for him Declare Just without doing it Assign.

Example

main.cpp
                    #include <iostream>
                    using namespace std;

                    int main()
                    {
                    int a; // without assigning it any initial value a here we have defined a variable named
                    cout << "a = " << a; // a Here we have printed the value of the variable

                    return 0;
                    }
                  

We will get a result similar to the following when running.
The value that appears in the variable  a is a random value that was present in memory, as we mentioned before.

                    a = 4360638
                  

In the program CodeBlocks You will get the following warning which means that you are trying to print the value of a variable that basically does not have a value.

Warning: 'a' is used uninitialized in this function [-Wuninitialized]


The second error is to put a variable name without having defined this variable before.

Sometimes the programmer puts the name of a variable without having defined this variable before.
What is the problem that he forgot to do to him? Declare. Basically, this leads to the entire program not running.

Example

main.cpp
                    #include <iostream>
                    using namespace std;

                    int main()
                    {
                    cout << "a = " << a; // Although we didn't define it in base a here we printed a value of

                    return 0;
                    }
                  

The following error will appear when running, which means that you are trying to print the value of a variable that you did not define in the first place.

error: 'a' was not declared in this scope

The third error is storing a value greater than the maximum value that can be accommodated in it.

In the previous lesson, we saw that each data type can contain specific values, as we saw the smallest and largest value that can be stored in it.

Now you have to know that if the value you are trying to store in the variable is greater than the value that can be stored in it, part of it will be put in the variable and not the value you wanted to store in it, and therefore you will lose the real value you wanted to store. This is something that you must pay close attention to in order to store the data correctly without losing any part of it.

Now, since the type  int can store a  2147483647 maximum value as we saw in the previous lesson, notice what would happen if we tried to store a value greater than the maximum value it could hold in it.

Example

main.cpp
                    #include <iostream>
                    using namespace std;

                    int main()
                    {
                    int a = 10000000000000; // and we realize that this large value cannot be stored in a here we put a value in the variable
                    cout << "a = " << a;

                    return 0;
                    }
                  

We will get a result similar to the following when running.
The value that appears in the variable  a is part of the value that you were trying to store in the variable as we mentioned before.

                    a = 1316134912
                  

In the program CodeBlocks You will see a warning  [Overflow] that the memory space allocated to the variable cannot hold the value you are trying to store in it.

warning: overflow in implicit constant conversion [-Woverflow]

In the next lesson, we will learn about the symbols that we can use in the code, which are called operators.