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 serves a dual purpose depending on its arity: as a unary operator, it performs arithmetic negation, and as a binary operator, it performs arithmetic subtraction, pointer decrement, or pointer difference operations.
Unary Minus Operator
The unary- operator computes the arithmetic negation of its operand.
- Operand Constraints: The operand must have an arithmetic type (integer or floating-point). It cannot be a pointer, array, or struct/union.
- Type Promotion: Integer promotions are applied to the operand before the operation. The type of the result is the type of the promoted operand.
- Precedence and Associativity: It evaluates at precedence level 2 (unary operators) with right-to-left associativity.
- Result and Modulo Arithmetic: The result is the negative of the promoted operand. For unsigned integer types, the behavior depends on integer promotion:
- If the narrow unsigned type promotes to a signed
int(e.g.,unsigned charorunsigned shorton systems whereintcan represent all their values), standard signed arithmetic applies (e.g.,-(int)5becomes-5). - If the promoted type is unsigned, the negation is well-defined and computed using modulo arithmetic by subtracting the promoted value from the maximum value of the promoted type plus one.
- If the narrow unsigned type promotes to a signed
Binary Minus Operator
The binary- operator computes the difference between two operands.
- Precedence and Associativity: It evaluates at precedence level 4 (additive operators) with left-to-right associativity.
- Operand Constraints and Behavior: The binary
-operator accepts three specific combinations of operand types: 1. Both operands have arithmetic types:- The usual arithmetic conversions are applied to both operands to determine a common real type.
- The result is the difference between the left and right operands, possessing the common type.
- Undefined Behavior: If the result cannot be represented in the resulting signed integer type (signed integer overflow), the behavior is undefined. 2. Left operand is a pointer, right operand is an integer:
- The left operand must be a pointer to a completely defined object type.
- The result is a pointer of the same type as the left operand.
- The memory address is calculated by decrementing the pointer’s address by the integer value multiplied by the
sizeofthe pointed-to type. - Undefined Behavior: The behavior is undefined if the resulting pointer does not point to an element within the same array object, or one past the last element of that array. 3. Both operands are pointers:
- Both operands must be pointers to qualified or unqualified versions of compatible complete object types. Subtracting pointers to incomplete types (such as
void*or forward-declared structs) is a constraint violation. - The result is the difference between the subscripts of the two array elements they point to.
- The type of the result is
ptrdiff_t, a signed integer type defined in<stddef.h>. - Undefined Behavior: The behavior is undefined unless both pointers point to elements of the same array object, or one past the last element of the array object. If the resulting difference cannot fit in a
ptrdiff_t, the behavior is also undefined.
Master C with Deep Grasping Methodology!Learn More





