Search Knowledge

© 2026 LIBREUNI PROJECT

C Programming Mastery / Language Fundamentals

Operators, Expressions, and Side Effects

The Mechanics of Expressions

In C, an Expression is a sequence of operators and operands that specifies a computation. Understanding the nuances of how these expressions are evaluated is the difference between writing robust code and creating subtle, non-deterministic bugs.

Arithmetic Operators and Pitfalls

Common arithmetic operators (+, -, *, /, %) behave as expected for floating-point and unsigned integers. However, signed integer arithmetic carries risks:

  • Integer Division: Truncates toward zero. (-5) / 2 results in -2.
  • The Modulo Operator (%): Requires integer operands. The identity (a/b)*b + a%b == a always holds in C.

Bitwise Operators: The Hardware Interface

C is the language of choice for drivers and embedded systems because of its direct support for bit-level manipulation.

OperatorDescriptionCommon Use Case
&Bitwise ANDMasking bits (clearing specific bits).
|Bitwise ORSetting bits.
^Bitwise XORToggling bits or simple swaps.
~Bitwise NOTOne’s complement (inverting all bits).
<<Left ShiftMultiply by power of 2.
>>Right ShiftDivide by power of 2 (Behavior for signed is implementation-defined).
Runtime Environment

Interactive Lab

1#include <stdio.h>
2 
3int main() {
4 unsigned char flag = 0x01; // 0000 0001
5 flag = flag << 3; // 0000 1000 (Value: 8)
6 printf("Flag value: %u\n", flag);
7
8 if (flag & 8) {
9 printf("Bit 3 is set!\n");
10 }
11 return 0;
12}
System Console

Waiting for signal...

Logical Operators and Short-Circuiting

Logical AND (&&) and OR (||) are guaranteed to evaluate from left-to-right. They use Short-Circuit Evaluation:

  • In A && B, if A is false, B is never evaluated.
  • In A || B, if A is true, B is never evaluated.

This property is frequently used to guard against null pointer dereferences:

if (ptr != NULL && ptr->value > 10) { ... }

Precedence, Associativity, and Sequence Points

Precedence determines which operator is applied first in an expression like a + b * c. Associativity determines the direction of evaluation for operators of the same precedence (e.g., a - b - c is (a - b) - c).

The Sequence Point Rule

A “Sequence Point” is a point in time where all “side effects” (like variable assignments) from previous evaluations are guaranteed to be complete.

  • The end of a statement (;) is a sequence point.
  • The &&, ||, and , operators are sequence points.

CRITICAL RULE: Between two sequence points, an object’s value shall be modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored. Failure to follow this results in Undefined Behavior.

Interactive Lab

Sequence Point Violation

int i = 5;
// Is this code valid or undefined behavior?
i = i++ + 1;
// Answer: 

The Ternary Operator

The ?: operator is C’s only ternary operator. It is an expression, not a statement, meaning it returns a value and can be used on the right-hand side of an assignment.

int max = (a > b) ? a : b;