Skip to study material
← Question index

QUESTION 54 · Operators

Classify the operators in C with examples.

5 marks

Answer in brief

An operator is a symbol that tells C to perform an operation on one or more values. The values it works on are called operands.

Types of operators

  1. Arithmetic: +, -, *, / and % perform numeric calculations.
  2. Relational: <, <=, >, >=, == and != compare values.
  3. Logical: &&, || and ! combine or reverse conditions.
  4. Bitwise: &, |, ^, ~, << and >> work with the bits of integers.
  5. Assignment: = stores a value; += and similar forms combine calculation and assignment.
  6. Increment and decrement: ++ adds one and -- subtracts one.
  7. Conditional: ?: selects one of two expressions based on a condition.

Classification by number of operands

  1. Unary: Works on one operand, such as -x or !ready.
  2. Binary: Works on two operands, such as a + b.
  3. Ternary: Works with three operands. The conditional operator is written condition ? value1 : value2.

Example

In total = price * quantity;, * multiplies price and quantity, and = stores the result in total. In age >= 18 && has_id, >= compares age with 18 and && combines the two conditions.

Important points

Do not confuse = with ==: one assigns and the other compares. Likewise & is bitwise AND, while && is logical AND. Parentheses can make grouping clear. Choose types and input values that make the operation valid.

Reference table

↔ Scroll horizontally on a small screen
Operator families
FamilyOperatorsExample
Arithmetic+ − * / %a + b
Relational and equality< <= > >= == !=score >= 40
Logical&& || !valid && ready
Bitwise& | ^ ~ << >>flags & mask
Conditional?:a > b ? a : b
Increment/decrement++ --++count
Assignment= += -= *= /= %= and bitwise compound formstotal += value
Other useful operatorssizeof, casts, comma, address and indirectionsizeof(int)

Key points

  • An operand is the value or expression an operator acts on.
  • Operator purpose and operand count are two different classifications.