ProgrammingMiddle C Developer

What are the features of working with comparison and assignment operations in C? What are their priorities, typical pitfalls, and mistakes when using them in expressions?

Pass interviews with Hintsage AI assistant

Answer.

In the C language, comparison operators (==, !=, <, >, <=, >=) and the assignment operator (=) differ in both their semantics and priorities. Historically, errors between these operations have led to bugs in software: for example, mixing = and == often became a cause of hard-to-trace errors.

Problem: The main difficulty arises from the low priority of the assignment operator (=) compared to comparison operators. Additionally, assignment returns a value (rvalue), which allows for writing expressions like while(x = y), which sometimes leads to undesirable or non-obvious consequences.

Solution: It is necessary to clearly distinguish between == and =, understand their priorities in the chain of expressions, and use parentheses and linters to track such errors. In complex expressions, always leave parentheses for clarity.

Example code:

int a = 5, b = 3; if (a = b) { // mistake: assignment, not comparison printf("a == b\n"); }

Correct:

int a = 5, b = 3; if (a == b) { printf("a == b\n"); }

Key features:

  • The assignment operator (=) returns the assigned value and can be used in complex expressions.
  • Comparison operators always return 0 (false) or 1 (true).
  • The priority of the assignment operator is lower than that of comparison operators.

Trick questions.

What is the difference between '==' and '=' in C, and what happens if they are confused in conditions?

== is the comparison operator, = is the assignment operator. If = is used instead of ==, the variable receives the assigned value, and the condition checks this value as a boolean. This is a common cause of bugs.

Can you write chains of assignments, such as a = b = c = 0? What happens then?

Yes, in C, the assignment operator works from right to left. First, 0 will be assigned to c, then this value is assigned to b, then to a. All variables will receive 0.

Example code:

int a, b, c; a = b = c = 0;

Why is the expression 'if (a = 0)' not the same as 'if (a == 0)'?

In the expression if (a = 0), 0 is assigned to a. The condition is always false (since the result of the assignment is 0), and not "equality check". You need to write if (a == 0).

Typical mistakes and anti-patterns

  • Using = instead of == in conditions.
  • Complex nested assignments without parentheses and comments.
  • Expecting a bool result from the assignment operator.

Real-life example

Negative case

A programmer writes a while loop (x = data[i]) and expects the condition to trigger when x is zero. In fact, the loop only ends when data[i] is equal to 0 in value, rather than when x matches data[i].

Pros:

  • Allows for concise assignment and checking chains in one line.

Cons:

  • Hard-to-localize errors, especially if just one = is missed.
  • The error has syntactic correctness but not the intended semantics.

Positive case

Strict separation of expressions, explicit comparison and assignment. Use of linters to check the code.

Pros:

  • The code is clear and reliable.
  • Easier to maintain and support large projects.

Cons:

  • May require slightly more code and fewer "trick moves".