• Aucun résultat trouvé

Unary Operators

Dans le document III CP·6 (Page 61-64)

Syntax:

unary- expreJJzon:

poJtjix-expreJJion ++ unary- expreJJion -- unary-expreJJzon

unary-operator caJt-expreJJion sizeof unary-expreJJion sizeof ( type-name ) unary-operator: one of

I;

*

+

Prefix Increment and Decrement Operators Constraints:

The operand of the prefix increment or decrement operator has qualified or unqualified scalar type and is a modifiable lvalue.

Semantics:

The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. The expression ++E is equivalent to (E+=1).

See the discussions of additive operators and compound assignment later in this section for information on constraints, types, side effects, conversions, and the effects of operations on pointers.

The prefix -- operator is analogous to the prefix ++ operator, except that the value of the operand is decremented.

HA17-00 Prefix Increment and Decrement Operators

4-7

Expressions

Address and Indirection Operators Constraints:

The operand of the unary 1 operator is either a function designator or an lvalue that designates an object that is not a bit-field and is not declared with the register storage-class specifier.

The operand of the unary

*

operator has pointer type.

Semantics:

The result of the unary 1 (address-of) operator is a pointer to the object or function designated by its operand. If the operand has type "type", the result has type "pointer to type".

The unary

*

operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object. If the operand has type "pointer to type", the result has type "type". If an invalid value has been assigned to the pointer, the behavior of the unary * operator cannot be predicted.8

Unary Arithmetic Operators Constraints:

The operand of the unary + or - operator has arithmetic type; of the - operator, integral type; and of the ! operator, scalar type.

Semantics:

The result of the unary + operator is the value of its operand. The integral promotion is performed on the operand, and the result has the promoted type.

The result of the unary - operator is the negative of its operand. The integral promotion is performed on the operand, and the result has the promoted type.

The result of the - operator is the bitwise complement of its operand (that is, each bit in the result is set if and only if the corresponding bit in the converted operand is not set).

8 It is always true that if E is a function designator or an lvalue that is a valid operand of the unary t operator, *tE is a function designator or an lvalue equal to E.

If *P is an lvalue and T is the name of an object pointer type, the cast expression *(T)P is an lvalue that has a type compatible with that to which T points.

Among the invalid values for dereferencing a pointer by the unary

*

operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object that has automatic storage duration when execution of the block with which the object is associated has

terminated.

4-8 U nary Arithmetic Operators HA17-00

Expressions The integral promotion is performed on the operand, and the result has the promoted type.

The expression -E is equivalent to (ULONG_MAX-E) ifE is promoted to type unsigned long, and to (UINT_MAX-E) if E is promoted to type unsigned into (The constants ULONG_MAX and UINT_MAX are defined in, the header <limits.h>.)

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, or 1 if the value of its operand compares equal to O. The result has type int. The expression ! E is equivalent to (O==E).

sizeof Operator Constraints:

The sizeof operator may not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an lvalue that designates a bit-field object.

Semantics:

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand, which is not itself evaluated. The result is an integer constant.

When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. When applied to an operand that has array type, the result is the total number of bytes in the array.9When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.

The value of the result depends upon the argument type, and its type (an unsigned integral type) is size_t defined in the <stddef .h> header.

Examples:

A principal use of the sizeof operator is in communication with routines such as storage allocators and I/O systems. A storage-allocation function might accept a size (in bytes) of an object to allocate and return a pointer to void. For example:

extern void *alloc(size_t);

double *dp

=

alloc(sizeof *dp);

The alloc function ensures that its return value is aligned suitably for conversion to a pointer to double.

Another use of the sizeof operator is to compute the number of elements in an array:

sizeof array / sizeof array[O]

9 When applied to a parameter declared to have array (or function) type, the sizeof operator yields the size of the pointer obtained by converting as in Section 3, Data Conversion; see Section 7, External Data Definitions.

HA17-00 sizeof Operator 4-9

Expressions

Dans le document III CP·6 (Page 61-64)