Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Addition operator (+)

Binary + adds numbers or moves a pointer by an integer number of array elements, rather than bytes.

Version: C23; repository compiler flag, production runtime not attested

Syntax

left + right
pointer + integer
integer + pointer

Binary + adds arithmetic values after the usual arithmetic conversions. Distinguish it from unary +.

Signed integer result overflow causes undefined behavior; unsigned arithmetic is modular.

In pointer + integer and integer + pointer, the pointer operand must be a pointer to a complete object type. Standard C therefore does not allow these operations on void *, function pointers, or pointers to other incomplete types. The offset is measured in elements, not bytes. The result must point to an element of the same array or one past its last element; a one-past pointer must not be dereferenced. For pointer arithmetic, an object that is not an array element is treated as the sole element of an array of length one. If x is such an object of type int, &x + 1 is valid and yields a one-past pointer. Adding two pointers is invalid.

In the example, 7 + 5 yields 12, and values + 2 points to the third element.

Examples

#include <stdio.h>

int main(void)
{
    int values[] = {10, 20, 30};
    int sum = 7 + 5;
    int *p = values + 2;

    printf("%d %d\n", sum, *p);
    return 0;
}

Result

12 30

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