liveBook · Manning

Comparison

As explained in Modern C, Third Edition

A comparison operator is a C operator that evaluates a relational condition between two operands and produces a Boolean result.

A comparison operator evaluates a relational condition between two operands and produces a Boolean result. C provides ==, !=, <, >, <=, and >=; the last two include equality. section 4.0 section 4.4.1

Use comparisons as values as well as conditions:

  • Comparison operators return false or true, represented numerically as 0 and 1. section 4.4.1
  • Use them directly in control statements such as if conditions. section 4.4.1 section 3.1
  • Use their results in arithmetic or array indexing when a numeric truth value is useful. section 4.4.1
  • Use not_eq as an alternative spelling for != only with <iso646.h>; the feature is rarely used. section 4.4.1

Combining comparison results

size_t c = (a < b) + (a == b) + (a > b); size_t d = (a <= b) + (a >= b) - 1; Each comparison contributes 0 or 1, so these expressions encode relationships between a and b numerically. section 4.4.1

size_t c = (a < b) + (a == b) + (a > b);
size_t d = (a <= b) + (a >= b) - 1;

Inspect a compact listing that combines strict and inclusive comparisons as arithmetic values. section 4.4.1

How Modern C, Third Edition covers it

How deep it goes
Defines six operators and their 0-or-1 results, with worked arithmetic examples but no deeper evaluation mechanics.
Examples
Uses relational arithmetic with a and b and classifies array elements by comparing them with 1.0.
What it assumes
Assumes basic C expressions and control statements while establishing comparison syntax and Boolean result values.
What you can do after
Apply comparisons in conditions, arithmetic expressions, and Boolean-indexed arrays.
How it connects
Connects comparison operators to Boolean values, control statements, arithmetic operators, array indexing, and the bool type.

Also explained in

Compare how 8 books explain comparison →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.