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.
== (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.
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
intand adouble, theintis promoted to adoublebefore the equality check occurs. - Pointer Comparison: Pointers can be compared using
==if they point to compatible types. Additionally, a pointer can be compared to avoid *(void pointer) or a null pointer constant (such as0orNULL).
Operand Constraints
The== operator cannot be applied to all data types:
- Aggregate Types: You cannot use
==to directly comparestructoruniontypes. The compiler will emit an error. The standard and safest approach is member-by-member comparison. Using memory comparison functions likememcmpon 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
operand1andoperand2are executed is unsequenced and unspecified in C.
(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.3typically evaluates to0because the internal binary representations are not perfectly exact. - Zeroes: Positive zero and negative zero evaluate as equal (
+0.0 == -0.0yields1). - NaN: Not-a-Number (NaN) values are never equal to anything, including themselves. Using the standard
<math.h>macro,NAN == NANyields0.
Master C with Deep Grasping Methodology!Learn More





