Tuesday, September 21, 2010

The Assignment Operator

You can use the assignment operator within any valid expression. This is not the
case with most computer languages (including Pascal, BASIC, and FORTRAN), which
treat the assignment operator as a special case statement. The general form of the
assignment operator is
variable_name = expression;
where an expression may be as simple as a single constant or as complex as you
require. C/C++ uses a single equal sign to indicate assignment (unlike Pascal or
Modula-2, which use the := construct). The target, or left part, of the assignment
must be a variable or a pointer, not a function or a constant.
Frequently in literature on C/C++ and in compiler error messages you will see
these two terms: lvalue and rvalue. Simply put, an lvalue is any object that can occur
on the left side of an assignment statement. For all practical purposes, "lvalue" means
"variable." The term rvalue refers to expressions on the right side of an assignment and
simply means the value of an expression.

Type Conversion in Assignments

When variables of one type are mixed with variables of another type, a type conversion
will occur. In an assignment statement, the type conversion rule is easy: The value of
the right side (expression side) of the assignment is converted to the type of the left
side (target variable), as illustrated here:
int x;
char ch;
float f;
void func(void)
{
ch = x; /* line 1 */
x = f; /* line 2 */
f = ch; /* line 3 */
f = x; /* line 4 */
}
In line 1, the left high-order bits of the integer variable x are lopped off, leaving ch with
the lower 8 bits. If x were between 255 and 0, ch and x would have identical values.
Otherwise, the value of ch would reflect only the lower-order bits of x. In line 2, x will
receive the nonfractional part of f. In line 3, f will convert the 8-bit integer value stored
in ch to the same value in the floating-point format. This also happens in line 4, except
that f will convert an integer value into floating-point format.
When converting from integers to characters and long integers to integers, the
appropriate amount of high-order bits will be removed. In many 16-bit environments,
this means that 8 bits will be lost when going from an integer to a character and 16 bits
will be lost when going from a long integer to an integer. For 32-bit environments, 24
bits will be lost when converting from an integer to a character and 16 bits will be lost
when converting from an integer to a short integer.
Table 2-3 summarizes the assignment type conversions. Remember that the
conversion of an int to a float, or a float to a double, and so on, does not add any precision or accuracy. These kinds of conversions only change the form in which the
value is represented. In addition, some compilers always treat a char variable as
positive, no matter what value it has, when converting it to an int or float. Other
compilers treat char variable values greater than 127 as negative numbers when
converting. Generally speaking, you should use char variables for characters, and use
ints, short ints, or signed chars when needed to avoid possible portability problems.
To use Table 2-3 to make a conversion not shown, simply convert one type at a time
until you finish. For example, to convert from double to int, first convert from double
to float and then from float to int.

Multiple Assignments
C/C++ allows you to assign many variables the same value by using multiple
assignments in a single statement. For example, this program fragment assigns x, y,
and z the value 0:
x = y = z = 0;

Target                             Type Expression                          Type Possible Info Loss
signed char                        char                                   If value > 127, target is negative
char                                 short int                                              High-order 8 bits
char                                  int (16 bits)                                      High-order 8 bits
char                                  int (32 bits)                                        High-order 24 bits
char                                long int                                            High-order 24 bits
short int                             int (16 bits)                                                  None
short int                          int (32 bits)                                      High-order 16 bits
int (16 bits)                     long int                                            High-order 16 bits
int (32 bits)                     long int                                                    None
int                                     float                                     Fractional part and possibly more
float                                double                             Precision, result rounded
double                            long                                  double Precision, result rounded

Table 2-3. The Outcome of Common Type Conversions

No comments:

Post a Comment