c/language/operator arithmetic

Arithmetic operators apply standard mathematical operations to their operands.

Overflows
Unsigned integer arithmetic is always performed $modulo 2n$ where n is the number of bits in that particular integer. E.g. for, adding one to uint_max gives , and subtracting one from gives uint_max.

When signed integer arithmetic operation overflows (the result does not fit in the result type), the behavior is undefined: it may wrap around according to the rules of the representation (typically 2's complement), it may trap on some platforms or due to compiler options (e.g. in GCC and Clang), or may be completely optimized out by the compiler.

Floating-point environment
If is set to, all floating-point arithmetic operators obey the current floating-point rounding direction and report floating-point arithmetic errors as specified in  unless part of a  (in which case floating-point exceptions are not raised and the rounding mode is to nearest)

Floating-point contraction
Unless is set to, all floating-point arithmetic may be performed as if the intermediate results have infinite range and precision, that is optimizations that omit rounding errors and floating-point exceptions that would be observed if the expression was evaluated exactly as written. For example, allows the implementation of with a single fused multiply-add CPU instruction or optimization of  as.

Unrelated to contracting, intermediate results of floating-point arithmetic may have range and precision that is different from the one indicated by its type, see flt_eval_method

Unary arithmetic
The unary arithmetic operator expressions have the form

@1@ unary plus (promotion) @2@ unary minus (negation) where

Both unary plus and unary minus first apply to their operand, and then
 * unary plus returns the value after promotion
 * unary minus returns the negative of the value after promotion (except that the negative of a NaN is another NaN)

The type of the expression is the type after promotion, and the is non-lvalue.

Additive operators
The binary additive arithmetic operator expressions have the form

@1@ addition: and  must be one of the following
 * both have, including complex and imaginary
 * one is a pointer to complete object type, the other has integer type

@2@ subtraction: and  must be one of the following
 * both have, including complex and imaginary
 * has pointer to complete object type, has integer type
 * both are pointers to complete objects of types, ignoring qualifiers

Arithmetic addition and subtraction
If both operands have, then
 * first, are performed
 * then, the values of the operands after conversions are added or subtracted following the usual rules of mathematics (for subtraction, is subtracted from ), except that
 * if one operand is NaN, the result is NaN
 * infinity minus infinity is NaN and fe_invalid is raised
 * infinity plus the negative infinity is NaN and fe_invalid is raised

Complex and imaginary addition and subtraction are defined as follows (note the result type is imaginary if both operands are imaginary and complex if one operand is real and the other imaginary, as specified by the usual arithmetic conversions):

Pointer arithmetic

 * If the pointer points at an element of an array with index, then
 * and are pointers that point at an element of the same array with index
 * is a pointer that points at an element of the same array with index

The behavior is defined only if both the original pointer and the result pointer are pointing at elements of the same array or one past the end of that array. Note that executing p-1 when p points at the first element of an array is undefined behavior and may fail on some platforms.


 * If the pointer points at an element of an array with index  (or one past the end) and  points at an element of the same array with index  (or one past the end), then
 * has the value equal to and the type ptrdiff_t (which is a signed integer type, typically half as large as the size of the largest object that can be declared)

The behavior is defined only if the result fits in ptrdiff_t.

For the purpose of pointer arithmetic, a pointer to an object that is not an element of any array is treated as a pointer to the first element of an array of size 1.

Multiplicative operators
The binary multiplicative arithmetic operator expressions have the form

@1@ multiplication. and must have @2@ division. and must have @3@ remainder. and must have


 * first, are performed. Then...

Multiplication
The binary operator * performs multiplication of its operands (after usual arithmetic conversions) following the usual arithmetic definitions, except that
 * if one operand is a NaN, the result is a NaN
 * multiplication of infinity by zero gives NaN and fe_invalid is raised
 * multiplication of infinity by a nonzero gives infinity (even for complex arguments)

Because in C, any with at least one infinite part is an infinity even if its other part is a NaN, the usual arithmetic rules do not apply to complex-complex multiplication. Other combinations of floating operands follow the following table:

Besides infinity handling, complex multiplication is not allowed to overflow intermediate results, except when is set to, in which case the value may be calculated as if by $(x+iy)×(u+iv) = (xu-yv)+i(yu+xv)$, as the programmer assumes the responsibility of limiting the range of the operands and dealing with the infinities.

Despite disallowing undue overflow, complex multiplication may raise spurious floating-point exceptions (otherwise it is prohibitively difficult to implement non-overflowing versions)

Division
The binary operator divides the first operand by the second (after usual arithmetic conversions) following the usual arithmetic definitions, except that
 * when the type after usual arithmetic conversions is an integer type, the result is the algebraic quotient (not a fraction),
 * if one operand is a NaN, the result is a NaN
 * if the first operand is a complex infinity and the second operand is finite, then the result of the operator is a complex infinity
 * if the first operand is finite and the second operand is a complex infinity, then the result of the operator is a zero.

Because in C, any with at least one infinite part as an infinity even if its other part is a NaN, the usual arithmetic rules do not apply to complex-complex division. Other combinations of floating operands follow the following table:

Besides infinity handling, complex division is not allowed to overflow intermediate results, except when is set to, in which case the value may be calculated as if by $(x+iy)/(u+iv) = [(xu+yv)+i(yu-xv)]/(u2 +v2 )$, as the programmer assumes the responsibility of limiting the range of the operands and dealing with the infinities.

Despite disallowing undue overflow, complex division may raise spurious floating-point exceptions (otherwise it is prohibitively difficult to implement non-overflowing versions)

If the second operand is zero, the behavior is undefined, except that if the IEEE floating-point arithmetic is supported, and the floating-point division is taking place, then
 * Dividing a non-zero number by ±0.0 gives the correctly-signed infinity and fe_divbyzero is raised
 * Dividing 0.0 by 0.0 gives NaN and fe_invalid is raised

Remainder
The binary operator % yields the remainder of the division of the first operand by the second (after usual arithmetic conversions).

The sign of the remainder is defined in such a way that if the quotient is representable in the result type, then.

If the second operand is zero, the behavior is undefined.

If the quotient is not representable in the result type, the behavior of both  and  is undefined (that means  is undefined on 2's complement systems)

Note: the remainder operator does not work on floating-point types, the library function fmod provides that functionality.

Bitwise logic
The bitwise arithmetic operator expressions have the form

@1@ bitwise NOT @2@ bitwise AND @3@ bitwise OR @4@ bitwise XOR where

First, operators, , and perform  on both operands and the operator  performs  on its only operand.

Then, the corresponding binary logic operators are applied bitwise; that is, each bit of the result is set or cleared according to the logic operation (NOT, AND, OR, or XOR), applied to the corresponding bits of the operands.

Note: bitwise operators are commonly used to manipulate bit sets and bit masks.

Note: for unsigned types (after promotion), the expression is equivalent to the maximum value representable by the result type minus the original value of.

Shift operators
The bitwise shift operator expressions have the form

@1@ left shift of by  bits @2@ right shift of by  bits where

First, are performed, individually, on each operand (Note: this is unlike other binary arithmetic operators, which all perform usual arithmetic conversions). The type of the result is the type of after promotion.

The behavior is undefined if is negative or is greater or equal the number of bits in the promoted.

For unsigned, the value of is the value of $LHS * 2RHS$, reduced modulo maximum value of the return type plus 1 (that is, bitwise left shift is performed and the bits that get shifted out of the destination type are discarded). For signed with nonnegative values, the value of  is $LHS * 2RHS$ if it is representable in the promoted type of, otherwise the behavior is undefined.

For unsigned and for signed  with nonnegative values, the value of  is the integer part of $LHS / 2RHS$. For negative, the value of is implementation-defined where in most implementations, this performs arithmetic right shift (so that the result remains negative). Thus in most implementations, right shifting a signed fills the new higher-order bits with the original sign bit (i.e. with 0 if it was non-negative and 1 if it was negative).