Skip to content
Laviyx Learn

Try

C course menu

Operators in C: Arithmetic, Relational, Logical, Assignment and More

Learn C operators with examples: arithmetic operators and the modulus operator, increment and decrement (prefix vs postfix), relational and logical operators with short-circuiting, assignment operators, the ternary operator, sizeof, the comma operator, and precedence.

Beginner6 min readUpdated
On this page

Operators combine values into new values. C's operators are also used, with small differences, by C++, Java, JavaScript and many other languages, so learning them here pays off everywhere.

Arithmetic#

C
int a = 17, b = 5;
printf("%d %d %d %d %d\n", a + b, a - b, a * b, a / b, a % b);
printf("%d\n", -17 % 5);
Output
22 12 85 3 2
-2
  • / with two integers is integer division (Type Conversion).
  • % gives the remainder and only works on integers. With a negative left operand the result is negative (since C99).
  • n % 2 == 0 tests for even numbers; % 10 gives the last digit.

Increment and decrement#

C
int count = 5;
count++;
printf("%d\n", count);

int x = 5;
int y = x++;      /* y gets the old value, then x becomes 6 */
int z = ++x;      /* x becomes 7 first, then z gets 7 */
printf("%d %d %d\n", x, y, z);
Output
6
7 5 7

x++ (postfix) gives the old value; ++x (prefix) gives the new value. Never use the same variable twice in one expression with ++, as in i = i++ + ++i;: that is undefined behaviour.

Assignment operators#

C
int balance = 1000;
balance += 500;
balance -= 200;
balance *= 2;
balance /= 4;
balance %= 300;
printf("%d\n", balance);
Output
50

balance += 500 means balance = balance + 500.

Relational operators#

Comparisons produce 1 (true) or 0 (false), of type int:

C
int age = 20;
printf("%d %d %d\n", age > 18, age == 21, age != 21);
Output
1 0 1
OperatorMeaning
==, !=Equal, not equal
<, >, <=, >=Less, greater, and so on

Logical operators#

OperatorTrue when
&&Both sides are true (non-zero)
||At least one side is true
!The operand is false (zero)
C
int age = 25, has_ticket = 1;
printf("%d\n", age >= 18 && has_ticket);
printf("%d\n", age < 12 || age > 60);
printf("%d\n", !has_ticket);
Output
1
0
0

&& and || short-circuit: the right side is not evaluated if the left side already decides the result. This makes checks like if (p != NULL && p->value > 0) safe.

The ternary operator#

C
int stock = 0;
printf("%s\n", stock > 0 ? "In stock" : "Sold out");
int bigger = 7 > 3 ? 7 : 3;
printf("%d\n", bigger);
Output
Sold out
7

sizeof and the comma operator#

sizeof gives a size in bytes (Data Types). The comma operator evaluates left then right and gives the right value; you will mostly see it in for loop headers: for (i = 0, j = n - 1; i < j; i++, j--).

Precedence#

When an expression has several operators, precedence decides the order. Roughly from highest to lowest:

  1. () [] -> . and postfix ++ --
  2. Unary ! - ++ -- * (dereference) & (address-of) sizeof and casts
  3. * / %
  4. + -
  5. << >>
  6. < <= > >=
  7. == !=
  8. &, then ^, then |
  9. &&, then ||
  10. ?:
  11. Assignment: = += -= ...
C
printf("%d\n", 2 + 3 * 4);
printf("%d\n", (2 + 3) * 4);
printf("%d\n", 10 - 4 - 3);
Output
14
20
3

Use parentheses whenever the order is not obvious. Bitwise operators are covered in Bitwise Operators.

Practice#

Practice exercise

Given a number of seconds, 3725, print it as hours, minutes and seconds (1 h 2 min 5 s) using / and %, and print whether the number of seconds is even.

Show solution
C
#include <stdio.h>

int main(void) {
    int total = 3725;
    int hours = total / 3600;
    int minutes = total % 3600 / 60;
    int seconds = total % 60;
    printf("%d h %d min %d s\n", hours, minutes, seconds);
    printf("%s\n", total % 2 == 0 ? "even" : "odd");
    return 0;
}
Output
1 h 2 min 5 s
odd

Key takeaway#

Finished this lesson?