TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
+ operator in C is a polymorphic token that functions either as a binary addition operator or a unary plus operator, depending on its arity. It operates on arithmetic types (integer and floating-point) and pointer types, triggering specific type conversions and memory scaling rules defined by the C Standard.
Binary Addition Operator
When used with two operands,+ computes the sum of its operands.
+ operator is strictly dictated by the types of its operands:
1. Arithmetic Addition
If both operands possess arithmetic types, the compiler applies the usual arithmetic conversions before evaluating the expression. This process determines a common real type for the operation, evaluated in the following sequence:
- Floating-Point Hierarchy: Floating-point types are evaluated before integer ranks. If either operand is a floating-point type, the hierarchy is
long double>double>float. The operand with the lesser type (or any integer type) is implicitly converted to the greater floating-point type. - Integer Promotions: If neither operand is a floating-point type, integer promotions are applied to both operands. Types smaller than
int(e.g.,char,short) are promoted tointorunsigned int. - Integer Conversion Ranks and Signedness: After promotions, if the integer types still differ, the compiler resolves the common type based on integer conversion rank and signedness:
- If both operands have the same signedness (both signed or both unsigned), the operand with the lower integer conversion rank is converted to the type with the higher rank.
- If the unsigned operand has a rank greater than or equal to the signed operand, the signed operand is converted to the unsigned operand’s type.
- If the signed operand has a strictly greater rank and can represent all possible values of the unsigned operand’s type, the unsigned operand is converted to the signed operand’s type.
- If the signed operand has a strictly greater rank but cannot represent all values of the unsigned operand’s type, both operands are converted to the unsigned type corresponding to the signed operand’s type.
+ operator performs pointer arithmetic.
- The integer operand is implicitly multiplied by the size (in bytes) of the type the pointer points to (
sizeof(*pointer)). - The result is a new pointer of the same type, offset by the scaled integer value.
- Constraint: It is a constraint violation to add two pointers together. The
+operator only supportspointer + integerorinteger + pointer. - Constraint: Pointer arithmetic is invalid on pointers to incomplete types (e.g.,
void*) or function pointers, as their size is not defined by the standard.
Unary Plus Operator
When used with a single operand,+ yields the value of its operand after applying type promotions.
- The operand must have an arithmetic type.
- The compiler applies integer promotions to the operand. For example, applying unary
+to auint8_twill result in anint(assumingintcan represent all values ofuint8_ton the target architecture). - Unlike the unary
-operator, unary+does not alter the sign or mathematical value of the operand; its primary mechanical effect is enforcing integer promotion.
Undefined Behavior (UB)
The+ operator can invoke undefined behavior under specific conditions:
- Signed Integer Overflow: If the result of an addition between signed integers cannot be represented in the resulting type, the behavior is undefined. (Note: Unsigned integer addition does not overflow; it wraps around modulo , where is the number of bits in the result type).
- Pointer Bounds Violation: When performing pointer arithmetic, if the resulting pointer points outside the bounds of the array object the original pointer points to (or further than one element past the end of that array), the behavior is undefined.
- Floating-Point Exceptions: Depending on the floating-point environment (
<fenv.h>), adding floating-point numbers that result in overflow, underflow, or NaN (Not a Number) may raise hardware exceptions.
Master C with Deep Grasping Methodology!Learn More





