# Arithmetic operators

< c‎ | language

Arithmetic operators apply standard mathematical operations to their operands.

Operator Operator name Example Result
+ unary plus +a the value of a after promotions
- unary minus -a the negative of a
- subtraction a - b the subtraction of b from a
* product a * b the product of a and b
/ division a / b the division of a by b
% modulo a % b the remainder of a divided by b
~ bitwise NOT ~a the bitwise NOT of a
& bitwise AND a & b the bitwise AND of a and b
| bitwise OR a | b the bitwise OR of a and b
^ bitwise XOR a ^ b the bitwise XOR of a and b
<< bitwise left shift a << b a left shifted by b
>> bitwise right shift a >> b a right shifted by b

### Overflows

Unsigned integer arithmetic is always performed modulo 2n
where n is the number of bits in that particular integer. E.g. for unsigned int, adding one to UINT_MAX gives 0, and subtracting one from 0 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. `-ftrapv` in GCC and Clang), or may be completely optimized out by the compiler.

#### Floating-point environment

If #pragma STDC FENV_ACCESS is set to `ON`, all floating-point arithmetic operators obey the current floating-point rounding direction and report floating-point arithmetic errors as specified in math_errhandling unless part of a static initializer (in which case floating-point exceptions are not raised and the rounding mode is to nearest)

#### Floating-point contraction

Unless #pragma STDC FP_CONTRACT is set to `OFF`, 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 (x*y) + z with a single fused multiply-add CPU instruction or optimization of a = x*x*x*x; as tmp = x*x; a = tmp*tmp.

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

 `+` expression (1) `-` expression (2)
1) unary plus (promotion)
2) unary minus (negation)

where

 expression - expression of any arithmetic type

Both unary plus and unary minus first apply integral promotions 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 value category is non-lvalue.