⚡0 XP🔥0

Operators and Expressions

Lesson 3 of 13 ⏱ 6 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs C

Arithmetic, relational, logical, assignment, increment/decrement, bitwise and ternary operators, plus precedence rules.

An operator is a symbol that performs an operation on values (called operands). a + b is an expression: + is the operator, a and b are operands.

Arithmetic operators#

Operator Meaning Example (a = 17, b = 5) Result
+ addition a + b 22
- subtraction a - b 12
* multiplication a * b 85
/ division a / b 3 (integer division)
% modulus (remainder) a % b 2
▶ Try it YourselfC
Ctrl + Enter to run
Output
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
17.0 / 5 = 3.4
Common Mistake

% works only with integers. For decimals use fmod() from <math.h>. Dividing an integer by zero crashes the program.

Relational operators#

They compare two values and give 1 (true) or 0 (false): ==, !=, >, <, >=, <=.

▶ Try it YourselfC
Ctrl + Enter to run
Output
marks >= 40 : 1
marks == 100: 0
marks != 72 : 0
Common Mistake

= is assignment, == is comparison. Writing if (x = 5) assigns 5 to x and is always true - a classic bug. Compile with -Wall and GCC warns you.

Logical operators#

Operator Name True when
&& AND both conditions are true
|| OR at least one is true
! NOT the condition is false
▶ Try it YourselfC
Ctrl + Enter to run
Output
Can vote: 1
Teen or senior: 1
Not adult: 0

C uses short-circuit evaluation: in A && B, if A is false, B is never evaluated. In A || B, if A is true, B is skipped. This lets you write safe checks like if (n != 0 && total / n > 50).

Assignment and compound assignment#

x = 10;     // assign
x += 5;     // x = x + 5   → 15
x -= 3;     // x = x - 3   → 12
x *= 2;     // x = x * 2   → 24
x /= 4;     // x = x / 4   → 6
x %= 4;     // x = x % 4   → 2

Increment and decrement#

++ adds 1 and -- subtracts 1. The position matters when the value is used in the same expression:

▶ Try it YourselfC
Ctrl + Enter to run
Output
a = 6, b = 5
a = 7, c = 7
हिंदी में समझें

a++ (post) - पहले पुरानी value इस्तेमाल होती है, फिर a बढ़ता है। ++a (pre) - पहले a बढ़ता है, फिर नई value इस्तेमाल होती है। अकेली line में (a++;) दोनों का असर एक जैसा है। एक ही expression में एक variable को दो बार ++ न करें (a++ + a++) - इसका result undefined होता है।

The ternary (conditional) operator#

condition ? value1 : value2 - a compact if-else that produces a value.

▶ Try it YourselfC
Ctrl + Enter to run
Output
Bigger: 31
Result: PASS

Bitwise operators#

They work on the individual bits of integers - used in embedded systems, flags and interview puzzles.

Operator Meaning 6 op 3 (110, 011)
& AND 2 (010)
| OR 7 (111)
^ XOR 5 (101)
~ NOT ~6 is -7
<< left shift (× 2ⁿ) 6 << 1 = 12
>> right shift (÷ 2ⁿ) 6 >> 1 = 3
▶ Try it YourselfC
Ctrl + Enter to run
Output
x & y  = 2
x | y  = 7
x ^ y  = 5
x << 1 = 12
x >> 1 = 3
Is 6 odd? 0

Precedence - which operator runs first?#

From highest to lowest (most used ones):

  1. () brackets, ++ -- (postfix)
  2. ! ~ ++ -- (prefix), unary -, casts, sizeof
  3. * / %
  4. + -
  5. << >>
  6. < <= > >=
  7. == !=
  8. &, then ^, then |
  9. &&, then ||
  10. ?: ternary
  11. = += -= ... assignment
▶ Try it YourselfC
Ctrl + Enter to run
Output
14
20
10
3

Operators with the same precedence usually run left to right (20 / 4 * 2 is (20 / 4) * 2 = 10). When unsure, add brackets - they make code clearer for humans too.

Summary#

  • / between integers drops the fraction; % gives the remainder.
  • Comparisons and logical operators give 1 or 0; && and || short-circuit.
  • a++ uses then increments; ++a increments then uses.
  • ?: chooses between two values; bitwise operators act on bits.
  • Use brackets when precedence is not obvious.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Seconds to h:m:s

A video is 7384 seconds long. Using / and %, print 2 hours 3 minutes 4 seconds.

Expected output:
2 hours 3 minutes 4 seconds
2

Even or odd with ternary

Read an integer n with scanf and print n is even or n is odd using one printf and the ternary operator. Test input is 27.

Test input:
27
Expected output:
27 is odd

Quick Quiz

Q1.

What is the value of 17 % 5?

Q2.

int a = 5; int b = a++; - what are a and b now?

Q3.

In C, what does a relational expression like (3 > 2) evaluate to?

Q4.

What does x = (a > b) ? a : b; do?

Q5.

What is 2 + 3 * 4?