|
|
|
|
|
|
|
or the operand can be the name of a data type, enclosed in parentheses: |
|
|
|
|
|
|
|
|
You could find out the sizes of various data types on your machine by using code like this: |
|
|
|
|
|
|
|
|
cout < "Size of a short is " < sizeof(short)<< " bytes." < endl;
cout < "Size of an int is " < sizeof(int) << " bytes." < endl;
cout < "size of a long is " < sizeof(long) <<" bytes." < endl;
.
.
. |
|
|
|
|
|
|
|
|
The last operator in our operator table is the ?: operator, sometimes called the conditional operator. It is a ternary (three-operand) operator with the following syntax: |
|
|
|
|
|
|
|
|
Expression1 ? Expression2 : Expression3 |
|
|
|
|
|
|
|
|
|
Here's how it works. First, the computer evaluates Expression1. If it is nonzero (true), then the value of the entire expression is Expression2; otherwise, the value of the entire expression is Expression3. (Only one of Expression2 and Expression3 is evaluated.) A classic example of its use is to set a variable max equal to the larger of two variables a and b. Using an If statement, we would do it this way: |
|
|
|
|
|
|
|
|
if (a > b)
max = a;
else
max = b; |
|
|
|
|
|
|
|
|
With the ?: operator, we can use the following assignment statement: |
|
|
|
|
|