Operators in C
An operator is a symbol that tells C to do something with values: add them, compare them, or join conditions. +, > and && are all operators.
This lesson gives you the big picture: the 8 types of operators in C, with one example each. The next lessons explain each type in detail.
What You Will Learn
- What an operator and an operand are.
- The 8 types of operators in C.
- Unary, binary and ternary operators.
- What an expression is.
What are Operators in C?
Exam definition: An operator is a symbol that performs an operation on one or more values called operands. A combination of operators and operands that gives a value is called an expression.
In a + b: + is the operator, a and b are operands, and a + b is an expression.
The 8 types of operators in C
| # | Type | Operators | Example |
|---|---|---|---|
| 1 | Arithmetic | + - * / % | 7 % 3 → 1 |
| 2 | Relational | == != > < >= <= | 5 > 3 → 1 (true) |
| 3 | Logical | && || ! | (5 > 3) && (2 > 1) → 1 |
| 4 | Assignment | = += -= *= /= %= | x += 5 |
| 5 | Increment / Decrement | ++ -- | i++ |
| 6 | Bitwise | & | ^ ~ << >> | 5 & 3 → 1 |
| 7 | Conditional (ternary) | ? : | a > b ? a : b |
| 8 | Special | sizeof, comma ,, & (address), * (pointer), . and -> (member) | sizeof(int) → 4 |
Memory trick: "A R L A I B C S" = "All Rich Ladies Are In Big Car Shows": Arithmetic, Relational, Logical, Assignment, Increment, Bitwise, Conditional, Special.
Unary, binary and ternary operators
Operators are also grouped by how many operands they need:
| Group | Operands | Examples |
|---|---|---|
| Unary | 1 | ++i, -x, !flag, sizeof(x) |
| Binary | 2 | a + b, a > b, a && b |
| Ternary | 3 | a > b ? a : b (the only one) |
Uni = one, bi = two, ter = three, like unicycle, bicycle, tricycle.
Syntax of an Expression
operand operator operand /* binary: a + b */
operator operand /* unary: -a, !a */
condition ? value1 : value2 /* ternary */
Example 1: One Operator from Each Type
#include <stdio.h>
int main(void)
{
int a = 7, b = 3;
printf("Arithmetic : a %% b = %d\n", a % b);
printf("Relational : a > b = %d\n", a > b);
printf("Logical : a>0 && b>0 = %d\n", a > 0 && b > 0);
a += 1;
printf("Assignment : a += 1 -> %d\n", a);
b++;
printf("Increment : b++ -> %d\n", b);
printf("Bitwise : 5 & 3 = %d\n", 5 & 3);
printf("Ternary : bigger = %d\n", a > b ? a : b);
printf("Special : sizeof(int) = %zu\n", sizeof(int));
return 0;
}
Output
Arithmetic : a % b = 1 Relational : a > b = 1 Logical : a>0 && b>0 = 1 Assignment : a += 1 -> 8 Increment : b++ -> 4 Bitwise : 5 & 3 = 1 Ternary : bigger = 8 Special : sizeof(int) = 4
Code Explanation
a % b: remainder of 7 ÷ 3 = 1. (%%inside printf prints one % sign.)a > b: 7 > 3 is true, and in C true = 1, false = 0.a > 0 && b > 0: both conditions are true, so the result is 1.a += 1: same asa = a + 1, so a becomes 8.b++: increases b by 1, so b becomes 4.5 & 3: bitwise AND of 101 and 011 in binary = 001 = 1.a > b ? a : b: if a is bigger, give a, else b. 8 > 4, so 8.sizeof(int): the size of an int in bytes, 4.
How C Evaluates an Expression
- C calculates expressions using precedence (which operator first) and associativity (left-to-right or right-to-left when two have the same precedence).
- Example:
2 + 3 * 4is 14, not 20, because*has higher precedence than+. - Use brackets
( )to make the order clear:(2 + 3) * 4= 20. - You will learn the full table in the operator precedence lesson.
Example 2: Operators in a Real Calculation
#include <stdio.h>
int main(void)
{
int total_minutes = 135;
int hours = total_minutes / 60; /* arithmetic: division */
int minutes = total_minutes % 60; /* arithmetic: remainder */
int is_long = total_minutes > 120; /* relational: 1 or 0 */
printf("%d minutes = %d hours %d minutes\n", total_minutes, hours, minutes);
printf("Is it a long movie? %s\n", is_long ? "Yes" : "No"); /* ternary */
return 0;
}
Output
135 minutes = 2 hours 15 minutes Is it a long movie? Yes
Important Points to Remember
- An operator works on operands; together they make an expression.
- C has 8 types of operators.
- By operands: unary (1), binary (2), ternary (3).
- The only ternary operator is
? :. - In C, true = 1 and false = 0.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
if (x = 5) | Assigns 5; the condition is always true | Use == to compare |
5.5 % 2 | Error: % needs integers | Use fmod() from math.h for decimals |
Writing & instead of && in conditions | Bitwise AND, not logical AND; may give surprises | Use && for conditions |
2 + 3 * 4 expecting 20 | Gives 14 | Use brackets: (2 + 3) * 4 |
Types of Operators Practice Questions
- What is an operator? What is an operand?
- Name the 8 types of operators in C.
- What is the difference between unary, binary and ternary operators?
- Which is the only ternary operator in C?
- What is the value of
10 > 20in C?
Show answers
- An operator is a symbol that performs an operation; operands are the values it works on.
- Arithmetic, relational, logical, assignment, increment/decrement, bitwise, conditional, special.
- Unary works on 1 operand, binary on 2, ternary on 3.
? :(the conditional operator).- 0 (false).
Coding Exercise
Read a number of seconds (e.g. 3725) and print it as hours, minutes and seconds using / and %.
Show solution
#include <stdio.h>
int main(void)
{
int total, h, m, s;
printf("Enter seconds: ");
scanf("%d", &total);
h = total / 3600;
m = (total % 3600) / 60;
s = total % 60;
printf("%d h %d m %d s\n", h, m, s);
return 0;
}
Enter seconds: 3725 1 h 2 m 5 s
Quick Quiz: Types of Operators in C (MCQ)
-
In
x + y,xandyare:A) Operators B) Operands C) Keywords D) Constants only
-
Which is a ternary operator?
A)
++B)&&C)? :D)sizeof -
!is a:A) Unary operator B) Binary operator C) Ternary operator D) Assignment operator
-
sizeofbelongs to which type?A) Arithmetic B) Logical C) Special D) Bitwise
-
Value of
17 % 5:A) 3 B) 2 C) 3.4 D) 12
Show answers
- B: operands.
- C:
? : - A: unary.
- C: special.
- B: 2.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Operator | Symbol that performs an operation |
|---|---|
| Operand | Value the operator works on |
| Expression | Operators + operands that give a value |
| 8 types | Arithmetic, Relational, Logical, Assignment, Increment, Bitwise, Conditional, Special |
| By operands | Unary (1), Binary (2), Ternary (3) |
| Only ternary | ? : |
| True / false | 1 / 0 |
Frequently asked questions
- What is the difference between an operator and an operand?
- The operator is the symbol (like +); operands are the values it works on.
- Which operator works on three operands?
- The conditional (ternary) operator
? :. - Is sizeof a function or an operator?
- An operator (a unary special operator), even though it looks like a function.
- What do relational and logical operators return?
- 1 for true and 0 for false.
Remember
- 8 types: A-R-L-A-I-B-C-S.
- Uni-bi-ter = unicycle, bicycle, tricycle.
Next Topic
Next lesson: Arithmetic Operators in C. You will learn the five maths operators and the tricky rules of / and %.