Description
Assigning values to variables.
Syntax
The syntax for assigning a value to a variable is:
variable_name = value;
Notes
· variable_name is the variable to be assigned a new value (Note: variable must already be declared)
· value is the value to be assigned to the variable variable_name.
· value can be a scalar value (such as a number, character, string), another variable, or a pointer.
· Ensure variable_name is capable of holding whatever value is carrying. If the data types are incompatible, variable_name will not hold the correct information.
· when two different sized floating or integer operands are combined, the smaller is converted to the other operand's type, but only for that operation.
· when floating point and integer operands are combined, the integer is converted to the same type as the floating operand
Examples
The following example shows two different ways of how to assign values to variables. The candy variables are assigned values immediately upon declaration; whereas the chocolate variables are not assigned a value until later in the program body. The two styles are equivalent to each other.
// this is the variable declaration area
int
num_candies = 25,
num_chocolates;
float
price_candies = 1.25,
price_chocolates,
amt_candies = num_candies * price_candies,
amt_chocolates;
// here are the assignment of variables
num_chocolates = 5;
price_chocolates = 1.00;
amt_chocolates = num_chocolates * price_chocolates;
Tutorial: Type Conversion
Description
Converting a variable from one type to another.
Syntax
The syntax for type conversion is:
(DestinationType) Expression
Notes
· DestinationType is the type of value (e.g. int, double, float) that Expression is to be converted to, if possible.
· Expression is any valid C++ expression to be converted into type DestinationType.
· Explicitly defining the destination type of an expression is called casting the expression.
· Casting will only work successfully if the current type of Expression can be expressed in DestinationType.
· Casting a variable does not change the original value of the variable.
Tips
· Although casting variables is a convenient way of converting variable types into a desired type, it should be used judiciously. For example, if a program only required integer operations, it makes no sense to declare all variables to be of float type, and then cast the variables to an integer whenever they are required in an operation - not only will this be a waste of resources, but it can also lead to other problems.
· Some compilers only give a warning, instead of an error, in assignments where the source and destination types are mismatched (e.g. int to float). For the most part, these same compilers automatically cast the variables to the destination type.
· Ensure that DestinationType is capable of representing the data in Expression. For example, if DestinationType was int and our Expression contained a value of 3.1415, then casting will produce a result of 3 - a loss in precision.
Examples
In the following example, we will use casting to convert an integer variable to a float because the square() procedure only accepts float arguments. As a side note, CC automatically casts integers into floats whenever required, but not all compilers are this "smart". Of course, one should still explicitly cast a variable instead of allowing the compiler to do it for the purpose of portability.
// This program demonstrates the use of casting in a C++ program.
#include <iostream.h>
float square (float d) {
float answer;
answer = d * d;
return answer;
}
int main() {
int i;
float f;
i = 7;
f = 3.1415;
cout << "Square of 7: " << square ( (float) i ) << endl;
cout << "Float value: " << f << endl;
i = (int) f;
cout << "Casted value (note precision loss): " << i << endl;
}
The result of this program is:
Square of 7: 49
Float value: 3.1415
Casted value (note precision loss): 3