Expressions. McGraw-Hill The Complete Reference Fourth Edition
विज्ञापन
विज्ञापन
52
C + + : T h e C o m p l e t e R e f e r e n c e an array, the expression within square brackets provides an index into that array. For example,
#include <stdio.h> char s[80]; int main(void)
{ s[3] = 'X'; printf("%c", s[3]);
} return 0; first assigns the value 'X' to the fourth element (remember, all arrays begin at 0) of array
s
, and then prints that element.
Precedence Summary
Table 2-8 lists the precedence of all operators defined by C. Note that all operators, except the unary operators and ?, associate from left to right. The unary operators
(*, &, ) and ? associate from right to left.
C++ defines a few additional operators, which are discussed at length in Part Two.
Expressions
Operators, constants, and variables are the constituents of expressions. An expression in
C/C++ is any valid combination of these elements. Because most expressions tend to follow the general rules of algebra, they are often taken for granted. However, a few aspects of expressions relate specifically to C and C++.
Order of Evaluation
Neither C nor C++ specifies the order in which the subexpressions of an expression are evaluated. This leaves the compiler free to rearrange an expression to produce more optimal code. However, it also means that your code should never rely upon the order in which subexpressions are evaluated. For example, the expression x = f1() + f2(); does not ensure that f1( ) will be called before f2( ).
C h a p t e r 2 : E x p r e s s i o n s
53
Highest ( ) [ ]
−> .
! ~ ++ – – (type) * & sizeof
* / %
+
−
<< >>
&
^
< <= > >=
== !=
|
&&
||
Highest
Lowest
,
?:
= +=
− = *= /= etc.
Table 2-8.
The Precedence of C Operators
Type Conversion in Expressions
When constants and variables of different types are mixed in an expression, they are all converted to the same type. The compiler converts all operands up to the type of the largest operand, which is called type promotion. First, all char and short int values are automatically elevated to int. (This process is called integral promotion.) Once this step has been completed, all other conversions are done operation by operation, as described in the following type conversion algorithm:
IF an operand is a long double
THEN the second is converted to long double
ELSE IF an operand is a double
THEN the second is converted to double
54
C + + : T h e C o m p l e t e R e f e r e n c e
ELSE IF an operand is a float
THEN the second is converted to float
ELSE IF an operand is an unsigned long
THEN the second is converted to unsigned long
ELSE IF an operand is long
THEN the second is converted to long
ELSE IF an operand is unsigned int
THEN the second is converted to unsigned int
There is one additional special case: If one operand is long and the other is
unsigned int
, and if the value of the unsigned int cannot be represented by a long, both operands are converted to unsigned long.
Once these conversion rules have been applied, each pair of operands is of the same type and the result of each operation is the same as the type of both operands.
For example, consider the type conversions that occur in Figure 2-2. First, the character ch is converted to an integer. Then the outcome of ch/i is converted to a
double
because f*d is double. The outcome of f+i is float, because f is a float. The final result is double.
Casts
You can force an expression to be of a specific type by using a cast. The general form of a cast is
(type) expression
char ch; int i; float f; double d; result=(ch/i) int
+ (f*d) – double
(f+i); float int double float
Figure 2-2.
A type conversion example double
C h a p t e r 2 : E x p r e s s i o n s
55 where type is a valid data type. For example, to make sure that the expression x/2 evaluates to type float, write
(float) x/2
Casts are technically operators. As an operator, a cast is unary and has the same precedence as any other unary operator.
Although casts are not usually used a great deal in programming, they can be very helpful when needed. For example, suppose you wish to use an integer for loop control, yet to perform computation on it requires a fractional part, as in the following program:
#include <stdio.h> int main(void) /* print i and i/2 with fractions */
{ int i; for(i=1; i<=100; ++i) printf("%d / 2 is: %f\n", i, (float) i /2);
} return 0;
Without the cast (float), only an integer division would have been performed. The cast ensures that the fractional part of the answer is displayed.
C++ adds four more casting operators, such as const_cast and static_cast. These operators are discussed in Part Two.
Spacing and Parentheses
You can add tabs and spaces to expressions to make them easier to read. For example, the following two expressions are the same: x=10/y~(127/x); x = 10 / y ~(127/x);
Redundant or additional parentheses do not cause errors or slow down the execution of an expression. You should use parentheses to clarify the exact order of evaluation,
56
C + + : T h e C o m p l e t e R e f e r e n c e both for yourself and for others. For example, which of the following two expressions is easier to read?
x = y/3-34*temp+127; x = (y/3) - (34*temp) + 127;
Compound Assignments
There is a variation on the assignment statement, called compound assignment, that simplifies the coding of a certain type of assignment operation. For example, x = x+10; can be written as x += 10;
The operator += tells the compiler to assign to x the value of x plus 10.
Compound assignment operators exist for all the binary operators (those that require two operands). In general, statements like:
var = var operator expression
can be rewritten as
var operator = expression
For another example, x = x-100; is the same as x -= 100;
Compound assignment is widely used in professionally written C/C++ programs; you should become familiar with it. Compound assignment is also commonly referred to as shorthand assignment because it is more compact.
Chapter 3
Statements
Copyright © 2003 by The McGraw-Hill Companies. Click here for terms of use.
57
डाउनलोड
विज्ञापन