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 == (equality) operator is a binary operator that compares two operands to determine if their evaluated values are equal. It yields an int result of 1 if the operands are equal, and 0 if they are not.
operand1 == operand2

Return Type

Regardless of the types of the operands, the result of the == operator is always of type int. C does not return a boolean type (bool) from equality operators, even if <stdbool.h> is included.

Type Promotion and Conversions

When the operands are of different types, the compiler applies implicit conversions before performing the comparison:
  • Usual Arithmetic Conversions: If both operands are arithmetic types (integer or floating-point), they are promoted to a common type. For example, if comparing an int and a double, the int is promoted to a double before the equality check occurs.
  • Pointer Comparison: Pointers can be compared using == if they point to compatible types. Additionally, a pointer can be compared to a void * (void pointer) or a null pointer constant (such as 0 or NULL).

Operand Constraints

The == operator cannot be applied to all data types:
  • Aggregate Types: You cannot use == to directly compare struct or union types. The compiler will emit an error. The standard and safest approach is member-by-member comparison. Using memory comparison functions like memcmp on structs is a known anti-pattern; structs often contain hidden padding bytes with indeterminate values, which can yield false negatives unless the entire struct memory was explicitly zeroed out prior to initialization.
  • Arrays: When == is used with array identifiers, the arrays undergo array-to-pointer decay. The operator compares the memory addresses of the first elements of the arrays, not the contents of the arrays themselves.

Precedence and Associativity

  • Precedence: The == operator has lower precedence than relational operators (<, <=, >, >=) but higher precedence than bitwise operators (&, ^, |) and logical operators (&&, ||).
  • Associativity: It has left-to-right associativity. This dictates the grouping of the operands, not the evaluation order of the operands themselves. The order in which operand1 and operand2 are executed is unsequenced and unspecified in C.
Because of left-to-right associativity, chained equality expressions group sequentially:
a == b == c
This expression is parsed as (a == b) == c. The subexpression (a == b) evaluates to either 1 or 0, and that resulting int is then compared to c.

Floating-Point Mechanics

When evaluating IEEE 754 floating-point numbers, the == operator adheres to specific hardware-level rules:
  • Precision Loss: Using == to check for exact equality between floating-point numbers is inherently unsafe due to precision loss and rounding errors. For example, 0.1 + 0.2 == 0.3 typically evaluates to 0 because the internal binary representations are not perfectly exact.
  • Zeroes: Positive zero and negative zero evaluate as equal (+0.0 == -0.0 yields 1).
  • NaN: Not-a-Number (NaN) values are never equal to anything, including themselves. Using the standard <math.h> macro, NAN == NAN yields 0.
Master C with Deep Grasping Methodology!Learn More