Previous: 5.4.1 Precedence and Associativity
Up: 5.4 Operators and Expression Evaluation
Next: 5.4.3 Some New Operators
Previous Page: 5.4.1 Precedence and Associativity
Next Page: 5.4.3 Some New Operators

5.4.2 The Data Type of the Result

The data type of an expression value depends on the operators and the types of operands. If the operands are all of the same type, the result is of that same type. When there are operands of mixed type in an assignment expression, the right hand side is always converted to the data type of the object on the left hand side. This follows common sense since the type of the object on the left of an assignment is fixed and cannot be changed. When any other binary operator is applied to operands of mixed type, the operand of a type with lower range is converted to the type of the higher range operand before the operator is applied; and the result is of the higher range type. Of course, values of characters in an expression are considered to be int type. Again, some examples will illustrate:

int n = 3, m = 2;
long large;
float x = 9.0, y = 5.0;
double z = 4.0;

As with the precedence and associativity rules, when in doubt as to the type and/or precision of an expression evaluation, cast operators may be used to force conversions to the desired type. Remember, only values of variables are converted for the purpose of computation, NOT the variables themselves.



Previous: 5.4.1 Precedence and Associativity
Up: 5.4 Operators and Expression Evaluation
Next: 5.4.3 Some New Operators
Previous Page: 5.4.1 Precedence and Associativity
Next Page: 5.4.3 Some New Operators

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:40:40 HST 1994