Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Addition operator +

The binary + operator adds numbers or moves a pointer by an integer number of elements. Learn its type conversions, pointer-arithmetic limits, and overflow risks.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

a + b

The built-in binary + operator in a + b computes the sum of numeric values; it does not itself modify its operands. The usual arithmetic conversions determine a common type for the operands and result: for example, 7 + 0.5 has type double and value 7.5. Small integer types undergo promotions, so adding two short values produces an int result.

Additive operators group left-to-right: a + b + c means (a + b) + c. Multiplication has higher precedence, so a + b * c means a + (b * c). Grouping does not determine operand evaluation order. Overflow of a signed integer result causes undefined behavior. Arithmetic with an unsigned integer result operates modulo 2^N, where N is that type's width; mixing signed and unsigned types requires care with conversions.

For a pointer p to an array element and an integer n, both p + n and n + p move the pointer by n elements, not bytes. The pointee type of p must be a complete object type and must be similar to the array element type. If p points to an element of an extern array whose element type is still incomplete at the point of addition, p + 1 is ill-formed even though the pointee and element types are identical. The result must point to an element of the same array or the position one past its end; that final position must not be dereferenced. Moving outside this range or using a pointee type that is not similar to the element type causes undefined behavior. For example, adding 1 to an unsigned int* that points to an element of an int[] array causes undefined behavior even if the next element is in bounds. Adding two pointers is not allowed.

Unary +a is a separate operator, not addition of two values. User-defined types can define operator+, whose behavior depends on the selected overload; the rules above describe the built-in operator. The example demonstrates an integer sum, a double sum, pointer movement, and unchanged values of a and b. The referenced source is an evolving working draft, not a frozen C++23 text.

Examples

#include <iostream>

int main() {
    int a = 7;
    int b = 5;
    int values[] = {10, 20, 30};
    int* p = values;

    std::cout << a + b << '\n';
    std::cout << a + 0.5 << '\n';
    std::cout << *(p + 1) << '\n';
    std::cout << a << ' ' << b << '\n';
}

Result

12
7.5
20
7 5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources