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.
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#
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);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 == 0tests for even numbers;% 10gives the last digit.
Increment and decrement#
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);6
7 5 7x++ (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#
int balance = 1000;
balance += 500;
balance -= 200;
balance *= 2;
balance /= 4;
balance %= 300;
printf("%d\n", balance);50balance += 500 means balance = balance + 500.
Relational operators#
Comparisons produce 1 (true) or 0 (false), of type int:
int age = 20;
printf("%d %d %d\n", age > 18, age == 21, age != 21);1 0 1Logical operators#
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);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#
int stock = 0;
printf("%s\n", stock > 0 ? "In stock" : "Sold out");
int bigger = 7 > 3 ? 7 : 3;
printf("%d\n", bigger);Sold out
7sizeof 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:
()[]->.and postfix++--- Unary
!-++--*(dereference)&(address-of)sizeofand casts */%+-<<>><<=>>===!=&, then^, then|&&, then||?:- Assignment:
=+=-=...
printf("%d\n", 2 + 3 * 4);
printf("%d\n", (2 + 3) * 4);
printf("%d\n", 10 - 4 - 3);14
20
3Use 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
#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;
}1 h 2 min 5 s
oddKey takeaway#
Finished this lesson?