Now that we have declared some variables, we can use them to store values. We do that with an assignment statement. An assignment statement has the form variable = value;. This example shows a few assignments, and the comments illustrate different ways people sometimes talk about assignment statements:
int hour;
int minute;
int second;
hour = 11; // assign the value 11 to the variable hour
minute = 59; // 59 gets the value 59
second = 30; // set second to 30
Warning3.4.1.
Because C++ uses the = symbol for assignment, it is tempting to interpret a statement like a = b as a statement of equality. It is not.
One major difference is that assignment is not commutative. For example, in mathematics if \(a
= 7\) then \(7 = a\text{.}\) But in C++ the statement a = 7; is legalβit says βset a to store the value 7β. While 7 = a; is not. It would say βset 7 to store the value of aβ. It would not make much sense to give 7 a new value!
In some programming languages an alternate symbol is used for assignment, such as <- or :=, in order to avoid confusion. However, in C++, you must learn to mentally translate = it as βgets assigned the valueβ as opposed to βis equal toβ.
The variable type that you declare must match the type of the value assigned to it. A type mismatch will generate a compile error. For example, you cannot store a string in an int variable:
The type of hour is int. It can only store whole number values, not strings like "six". It would also be an error to try to store "6" as that is still a string, not a number.
This rule is sometimes a source of confusion, because there are many ways that you can convert values from one type to another, and C++ sometimes converts things automatically. But for now you should remember that as a general rule variables and values have the same type, and weβll talk about special cases later.
Variables must be initialized (assigned for the first time) before they can be used. You can declare a variable and then assign a value later, as in the previous example. You can also declare and initialize on the same line:
int main() {
int hour = 11; // Declare hour and assign a value
int minute = 59; // Declare minute and assign a value
}
When a variable is not given a value, it is said to be uninitialized. In C++, an uninitialized variable has an unknown value. (It will contain whatever value is represented by the bits that happened to be in the memory where it was placed!). Using an uninitialized variable can cause the program to behave unpredictably and is never desired. Fortunately, the compiler will generally provide you with a warning. The program below has this issue:
If we ignore the warning and run the code anyway, the output will be unpredictable. The value of hour will be whatever value is represented by the bits that were already in the memory location chosen for the variable! Try running the program below multiple times. You should see different values appear for the uninitialized variable.
Make sure to ALWAYS initialize variables. Always using the type name = value; syntax to declare and initialize a variable all at once is a good way to make sure your variables are always initialized. If you do not have a value to assign yet, use type name = 0; or something similar to assign a reasonable default value.