United States |
|
|
||
6.4 Unary OperatorsUnary expressions are formed by combining a unary operator with a single operand. All unary operators are of equal precedence and have right-to-left associativity. The unary operators are:
6.4.1 Unary Plus and MinusConsider the following expression:
This is the negative of the operand. The operand must have an arithmetic type, and integer promotion is applied. The additive inverse of an unsigned quantity is computed by subtracting the quantity from the largest value of the unsigned type plus one. The unary plus operator returns the value of an expression:
Neither the unary plus nor unary minus operators produce lvalues.
Consider the following expression:
The result is the logical (Boolean) negation of the expression. If the
value of the expression is 0, the negated result is 1; if the value of
the expression is not 0, the negated result is 0. The type of the
result is
int
. The expression must have a scalar type.
C has two unary operators for incrementing and decrementing scalar objects. The increment operator ++ adds 1 to its operand; the decrement operator -- subtracts 1. Both ++ and -- can be used either as prefix operators (before the variable: ++n ) or postfix operators (after the variable: n++ ). In both cases, the effect is to increment n . The expression ++n increments n before its value is used, while n++ increments n after its value is used. Section 6.3.4 describes the postfix increment and decrement operators. This section describes the prefix form. Consider the following expression:
After evaluating this expression, the result is the incremented rvalue, not the corresponding lvalue. For this reason, expressions that use the increment and decrement operators in this manner cannot appear by themselves on the left side of an assignment expression where an lvalue is needed. If declared as an integer or floating-point number, the operand is increased or decreased by 1 (or 1.0). The results of the following C statements are equivalent:
The following example shows the difference between the postfix and prefix forms of the increment operator:
If the operand is a pointer, the address is incremented by the size of the addressed object as determined by its data type, not by the integer value 1. For example:
Consider the following expression:
The prefix operator -- is similar to the prefix operator ++ except that the value of the operand is decremented. When using the increment and decrement operators, do not depend on the order of evaluation of expressions. Consider the following ambiguous expression:
It is unspecified whether the value of j in x[j] is evaluated before or after j is incremented. To avoid ambiguity, increment the variable in a separate statement, as in the following example:
6.4.4 Address Operator and IndirectionConsider the following expression:
This expression results in the address of the lvalue. The lvalue can be a function designator or any lvalue that designates an object, including an unqualified array identifier. The lvalue cannot be a register variable or a bit field. Consider the following expression:
When an expression resolves to an address, the value stored at that address can be accessed by using the dereferencing operator (*). If the operand of * is a function name or function pointer, then the result is a function designator. If the operand of * is a pointer to an object, then the result is an lvalue designating the object. If an invalid value (0, for example) is assigned to the pointer, then the * operation is undefined.
The dereferencing operator * always produces an lvalue. The address
operator & never produces an lvalue.
Consider the following expression:
The result is the bitwise negation (one's complement) of the evaluated
expression. Each 1-bit is converted into a 0-bit and vice versa. The
expression must have an integer type. The compiler performs the usual
arithmetic conversions (see Section 6.11.1).
The cast operator forces the conversion of its scalar operand to a specified scalar data type, or to void . The operator consists of a type-name, in parentheses, that precedes an expression, as follows:
The value of the expression is converted to the named data type, as if the expression were assigned to a variable of that type. The expression's type and value are not themselves changed; the value is converted to the cast type for the duration of the cast operation. The type-name has the following syntax: type-name:
In simple cases, type-specifier is the keyword for a data type, such as char or double , and abstract-declarator is empty. For example:
The type-specifier can also be an enum specifier, or a typedef name. The type-specifier can be a structure or union only if the abstract-declarator is a pointer. That is, the type-name can be a pointer to a structure or union, but cannot be a structure or union because structures and unions are not scalar types. For example:
The abstract-declarator in a cast operation is a declarator without an identifier. Abstract declarators have the following syntax: abstract-declarator:
The abstract-declarator cannot be empty in the following form:
Abstract declarators can include the brackets and parentheses that indicate arrays and functions. However, cast operations cannot force the conversion of any expression to an array, function, structure, or union. The brackets and parentheses are used in operations such as the following example, which casts the identifier P1 to pointer to array of int:
This kind of cast operation does not change the contents of P1 ; it only causes the compiler to treat the value of P1 as a pointer to such an array. For example, casting pointers this way can change the scaling that occurs when an integer is added to a pointer:
Cast operators can be used in the following conversions that involve
pointers:
6.4.7 The sizeof OperatorConsider the syntax of the following expressions:
type-name cannot be an incomplete type, function type, or a bit field. The sizeof operator produces a compile-time integer constant value. expression is inspected only to deduce its type; it is not fully evaluated. For example, sizeof(i++) is equivalent to sizeof(i) . The result of the sizeof operation is the size, in bytes, of the operand. In the first case, the result of sizeof is the size determined by the type of the expression. In the second case, the result is the size of an object of the named type. The expression should be enclosed in parentheses if it contains operators, because the precedence of sizeof is higher than that of most operators. The syntax of type-name is the same as that for the cast operator. For example:
The type of the
sizeof
operator's result,
size_t
, is an unsigned integer type.
In Compaq C,
size_t
is
unsigned int
.
The __typeof__ operator is another way to refer to the type of an expression. This feature is provided for compatiblity with the gcc compiler. The syntax of this operator keyword looks like sizeof , but the construct acts semantically like a type-name defined with typedef .
There are two ways of writing the argument to __typeof__ : with an expression or with a type. The following is an example with an expression. This example assumes that x is an array of int s; the type described is int :
The following is an example with a type-name as the argument. The type described is that of pointers to int :
A __typeof__ construct can be used anywhere a typedef name can be used. For example, you can use it in a declaration, in a cast, or inside a sizeof or __typeof__ operator:
The last example (the nested __typeof__ operators) is equivalent to the following traditional C declaration:
To see the meaning of the declaration using __typeof__ , and why it might be a useful way to write it that way, let's rewrite it with these macros:
Now the declaration can be rewritten this way:
Thus,
array (pointer (char), 4)
is the type of arrays of 4 pointers to
char
.
The _Pragma operator destringizes its string literal argument, effectively allowing #pragma directives to be produced by macro expansion. When specified using this operator, the tokens of the pragma, which appear together within a single string literal in this form, are not macro expanded, even if they have _m suffix. But macro expansion can be accomplished if desired by using the stringization operator ( # ) to form the string (see Section 8.1.3). The _Pragma operator has the following syntax:
A _Pragma operator expression is processed as follows: The string literal is destringized by deleting the L prefix, if present, deleting the leading and trailing double-quotes, replacing each escape sequence \" by a double-quote, and replacing each escape sequence \\ by a single backslash. The resulting sequence of characters is processed through translation phase 3 to produce preprocessing tokens that are executed as if they were the pp-tokens in a pragma directive. The original four preprocessing tokens in the unary operator expression are removed. Example: A directive of the form:
can also be expressed as:
The latter form is processed in the same way, whether it appears literally as shown, or results from macro replacement, as in:
6.5 Binary OperatorsThe binary operators are categorized as follows:
The following sections describe these binary operators.
The multiplicative operators are *, /, and %. Operands must have arithmetic type. Operands are converted, if necessary, according to the usual arithmetic conversion rules (see Section 6.11.1). The * operator performs multiplication. The / operator performs division. When integers are divided, truncation is toward zero. If either operand is negative, the result is truncated toward zero (the largest integer of lesser magnitude than the algebraic quotient). The % operator divides the first operand by the second and yields the remainder. Both operands must be integral. When both operands are unsigned or positive, the result is positive. If either operand is negative, the sign of the result is the same as the sign of the left operand. The following statement is true if b is not zero:
The Compaq C compiler, with the check option enabled, issues warnings for these undefined behaviors:
| |
privacy statement and legal notices |