Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The > (greater than) operator is a binary relational operator that evaluates whether the value of its left operand is strictly greater than the value of its right operand. It yields an int result of 1 if the condition is true, and 0 if the condition is false.
operand1 > operand2

Technical Mechanics

  • Return Type: The result of the > operator is always of type int, regardless of the types of the operands. C does not natively return a boolean type from relational operators.
  • Associativity: Left-to-right. An expression like a > b > c is parsed as (a > b) > c. Because the result of (a > b) is either 0 or 1, the subsequent comparison is evaluated as 0 > c or 1 > c.
  • Precedence: The > operator has lower precedence than arithmetic operators (like +, -, *, /) but higher precedence than equality operators (==, !=) and logical operators (&&, ||).

Operand Rules and Type Promotion

The operator accepts operands of real type (integer or floating-point) or pointer type, governed by strict rules defined by the C Standard: 1. Arithmetic Operands When both operands have arithmetic types, the compiler applies the usual arithmetic conversions before performing the comparison to establish a common real type.
  • Integer Promotions: Operands of lower integer conversion ranks (e.g., char, short) are first promoted to int if int can represent all values of the original type. Otherwise, they are promoted to unsigned int (e.g., an unsigned short on a system where short and int are both 16-bit).
  • Floating-Point Hierarchy: Floating-point conversions are handled by a separate hierarchy, distinct from integer conversion ranks. If either operand is a floating-point type, the operand with the lower type is converted to the higher type (e.g., comparing an int and a double causes the int to be converted to double).
  • Integer Conversion Rank and Mixed Signs: If both operands are integers of different types, integer conversion ranks dictate the promotion.
    • If the operands have the same rank but different signedness, the signed operand is converted to unsigned.
    • If the unsigned operand has a lower rank, and the signed operand’s type can represent all values of the unsigned type, the unsigned operand is converted to the signed type.
    • If the higher-rank signed type cannot represent all values of the lower-rank unsigned type (e.g., a 32-bit long and a 32-bit unsigned int), both operands are converted to the unsigned version of the higher-rank type.
  • Negative to Unsigned Conversion: When a signed integer is converted to an unsigned integer during these promotions, a negative value is mathematically converted to a large positive unsigned value (e.g., -1 becomes UINT_MAX). This can lead to unexpected comparison results if not carefully managed.
2. Pointer Operands Pointers can be compared using > if both point to compatible object types.
  • The C standard abstracts memory and does not mandate or assume virtual memory. Pointer comparison is strictly defined in terms of array subscripts and object layouts.
  • Comparing pointers using > is strictly valid only if both pointers point to elements within the same array object (or one past the last element), or to members of the same aggregate object. In C, only arrays and structures are aggregate types.
  • For arrays, a pointer to an element with a higher subscript compares greater than a pointer to an element with a lower subscript.
  • For structures, pointers to members declared later in the struct compare greater than pointers to members declared earlier.
  • Comparing pointers to distinct, unrelated objects results in undefined behavior.

Syntax Visualization

int x = 10;
int y = 5;
double z = 15.5;

// Standard integer comparison
int res1 = x > y;       // Evaluates to 1 (int)

// Mixed-type comparison (x is converted to double)
int res2 = z > x;       // Evaluates to 1 (int)

// Associativity evaluation: (x > y) > 0  ->  (1) > 0
int res3 = x > y > 0;   // Evaluates to 1 (int)

// Pointer comparison (valid within the same array)
int arr[5];
int *p1 = &arr[0];
int *p2 = &arr[3];
int res4 = p2 > p1;     // Evaluates to 1 (int)

// Pointer comparison (valid within the same aggregate object)
struct Data {
    int first_member;
    int second_member;
} obj;
int *ptr1 = &obj.first_member;
int *ptr2 = &obj.second_member;
int res5 = ptr2 > ptr1; // Evaluates to 1 (int)
Master C with Deep Grasping Methodology!Learn More