Стандарт языка Си С99 TC (1113411), страница 20
Текст из файла (страница 20)
The operands of the % operator shallhave integer type.Semantics3The usual arithmetic conversions are performed on the operands.4The result of the binary * operator is the product of the operands.5The result of the / operator is the quotient from the division of the first operand by thesecond; the result of the % operator is the remainder. In both operations, if the value ofthe second operand is zero, the behavior is undefined.6When integers are divided, the result of the / operator is the algebraic quotient with anyfractional part discarded.90) If the quotient a/b is representable, the expression(a/b)*b + a%b shall equal a.6.5.6 Additive operatorsSyntax1additive-expression:multiplicative-expressionadditive-expression + multiplicative-expressionadditive-expression - multiplicative-expressionConstraints2For addition, either both operands shall have arithmetic type, or one operand shall be apointer to an object type and the other shall have integer type.
(Incrementing isequivalent to adding 1.)3For subtraction, one of the following shall hold:— both operands have arithmetic type;90) This is often called ‘‘truncation toward zero’’.82Language§6.5.6WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3— both operands are pointers to qualified or unqualified versions of compatible objecttypes; or— the left operand is a pointer to an object type and the right operand has integer type.(Decrementing is equivalent to subtracting 1.)Semantics4If both operands have arithmetic type, the usual arithmetic conversions are performed onthem.5The result of the binary + operator is the sum of the operands.6The result of the binary - operator is the difference resulting from the subtraction of thesecond operand from the first.7For the purposes of these operators, a pointer to an object that is not an element of anarray behaves the same as a pointer to the first element of an array of length one with thetype of the object as its element type.8When an expression that has integer type is added to or subtracted from a pointer, theresult has the type of the pointer operand.
If the pointer operand points to an element ofan array object, and the array is large enough, the result points to an element offset fromthe original element such that the difference of the subscripts of the resulting and originalarray elements equals the integer expression.
In other words, if the expression P points tothe i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and(P)-N (where N has the value n) point to, respectively, the i+n-th and i−n-th elements ofthe array object, provided they exist.
Moreover, if the expression P points to the lastelement of an array object, the expression (P)+1 points one past the last element of thearray object, and if the expression Q points one past the last element of an array object,the expression (Q)-1 points to the last element of the array object. If both the pointeroperand and the result point to elements of the same array object, or one past the lastelement of the array object, the evaluation shall not produce an overflow; otherwise, thebehavior is undefined. If the result points one past the last element of the array object, itshall not be used as the operand of a unary * operator that is evaluated.9When two pointers are subtracted, both shall point to elements of the same array object,or one past the last element of the array object; the result is the difference of thesubscripts of the two array elements.
The size of the result is implementation-defined,and its type (a signed integer type) is ptrdiff_t defined in the <stddef.h> header.If the result is not representable in an object of that type, the behavior is undefined. Inother words, if the expressions P and Q point to, respectively, the i-th and j-th elements ofan array object, the expression (P)-(Q) has the value i−j provided the value fits in anobject of type ptrdiff_t. Moreover, if the expression P points either to an element ofan array object or one past the last element of an array object, and the expression Q pointsto the last element of the same array object, the expression ((Q)+1)-(P) has the same§6.5.6Language83ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N1256value as ((Q)-(P))+1 and as -((P)-((Q)+1)), and has the value zero if theexpression P points one past the last element of the array object, even though theexpression (Q)+1 does not point to an element of the array object.91)10EXAMPLEPointer arithmetic is well defined with pointers to variable length array types.{int n = 4, m = 3;int a[n][m];int (*p)[m] = a;p += 1;(*p)[2] = 99;n = p - a;////////p == &a[0]p == &a[1]a[1][2] == 99n == 1}11If array a in the above example were declared to be an array of known constant size, and pointer p weredeclared to be a pointer to an array of the same known constant size (pointing to a), the results would bethe same.Forward references: array declarators (6.7.5.2), common definitions <stddef.h>(7.17).6.5.7 Bitwise shift operatorsSyntax1shift-expression:additive-expressionshift-expression << additive-expressionshift-expression >> additive-expressionConstraints2Each of the operands shall have integer type.Semantics3The integer promotions are performed on each of the operands.
The type of the result isthat of the promoted left operand. If the value of the right operand is negative or isgreater than or equal to the width of the promoted left operand, the behavior is undefined.91) Another way to approach pointer arithmetic is first to convert the pointer(s) to character pointer(s): Inthis scheme the integer expression added to or subtracted from the converted pointer is first multipliedby the size of the object originally pointed to, and the resulting pointer is converted back to theoriginal type. For pointer subtraction, the result of the difference between the character pointers issimilarly divided by the size of the object originally pointed to.When viewed in this way, an implementation need only provide one extra byte (which may overlapanother object in the program) just after the end of the object in order to satisfy the ‘‘one past the lastelement’’ requirements.84Language§6.5.7WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC34The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled withzeros.
If E1 has an unsigned type, the value of the result is E1 × 2E2 , reduced moduloone more than the maximum value representable in the result type. If E1 has a signedtype and nonnegative value, and E1 × 2E2 is representable in the result type, then that isthe resulting value; otherwise, the behavior is undefined.5The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned typeor if E1 has a signed type and a nonnegative value, the value of the result is the integralpart of the quotient of E1 / 2E2 . If E1 has a signed type and a negative value, theresulting value is implementation-defined.6.5.8 Relational operatorsSyntax1relational-expression:shift-expressionrelational-expressionrelational-expressionrelational-expressionrelational-expression<><=>=shift-expressionshift-expressionshift-expressionshift-expressionConstraints2One of the following shall hold:— both operands have real type;— both operands are pointers to qualified or unqualified versions of compatible objecttypes; or— both operands are pointers to qualified or unqualified versions of compatibleincomplete types.Semantics3If both of the operands have arithmetic type, the usual arithmetic conversions areperformed.4For the purposes of these operators, a pointer to an object that is not an element of anarray behaves the same as a pointer to the first element of an array of length one with thetype of the object as its element type.5When two pointers are compared, the result depends on the relative locations in theaddress space of the objects pointed to.
If two pointers to object or incomplete types bothpoint to the same object, or both point one past the last element of the same array object,they compare equal. If the objects pointed to are members of the same aggregate object,pointers to structure members declared later compare greater than pointers to membersdeclared earlier in the structure, and pointers to array elements with larger subscript§6.5.8Language85ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N1256values compare greater than pointers to elements of the same array with lower subscriptvalues. All pointers to members of the same union object compare equal.
If theexpression P points to an element of an array object and the expression Q points to thelast element of the same array object, the pointer expression Q+1 compares greater thanP. In all other cases, the behavior is undefined.6Each of the operators < (less than), > (greater than), <= (less than or equal to), and >=(greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false.92)The result has type int.6.5.9 Equality operatorsSyntax1equality-expression:relational-expressionequality-expression == relational-expressionequality-expression != relational-expressionConstraints2One of the following shall hold:— both operands have arithmetic type;— both operands are pointers to qualified or unqualified versions of compatible types;— one operand is a pointer to an object or incomplete type and the other is a pointer to aqualified or unqualified version of void; or— one operand is a pointer and the other is a null pointer constant.Semantics3The == (equal to) and != (not equal to) operators are analogous to the relationaloperators except for their lower precedence.93) Each of the operators yields 1 if thespecified relation is true and 0 if it is false.