Skip to content
StudyFame

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

#TypeOperatorsExample
1Arithmetic+ - * / %7 % 3 → 1
2Relational== != > < >= <=5 > 3 → 1 (true)
3Logical&& || !(5 > 3) && (2 > 1) → 1
4Assignment= += -= *= /= %=x += 5
5Increment / Decrement++ --i++
6Bitwise& | ^ ~ << >>5 & 3 → 1
7Conditional (ternary)? :a > b ? a : b
8Specialsizeof, 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:

GroupOperandsExamples
Unary1++i, -x, !flag, sizeof(x)
Binary2a + b, a > b, a && b
Ternary3a > 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

  1. a % b: remainder of 7 ÷ 3 = 1. (%% inside printf prints one % sign.)
  2. a > b: 7 > 3 is true, and in C true = 1, false = 0.
  3. a > 0 && b > 0: both conditions are true, so the result is 1.
  4. a += 1: same as a = a + 1, so a becomes 8.
  5. b++: increases b by 1, so b becomes 4.
  6. 5 & 3: bitwise AND of 101 and 011 in binary = 001 = 1.
  7. a > b ? a : b: if a is bigger, give a, else b. 8 > 4, so 8.
  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 * 4 is 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

MistakeWhat happensFix
if (x = 5)Assigns 5; the condition is always trueUse == to compare
5.5 % 2Error: % needs integersUse fmod() from math.h for decimals
Writing & instead of && in conditionsBitwise AND, not logical AND; may give surprisesUse && for conditions
2 + 3 * 4 expecting 20Gives 14Use brackets: (2 + 3) * 4

Types of Operators Practice Questions

  1. What is an operator? What is an operand?
  2. Name the 8 types of operators in C.
  3. What is the difference between unary, binary and ternary operators?
  4. Which is the only ternary operator in C?
  5. What is the value of 10 > 20 in C?
Show answers
  1. An operator is a symbol that performs an operation; operands are the values it works on.
  2. Arithmetic, relational, logical, assignment, increment/decrement, bitwise, conditional, special.
  3. Unary works on 1 operand, binary on 2, ternary on 3.
  4. ? : (the conditional operator).
  5. 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)

  1. In x + y, x and y are:

    A) Operators   B) Operands   C) Keywords   D) Constants only

  2. Which is a ternary operator?

    A) ++   B) &&   C) ? :   D) sizeof

  3. ! is a:

    A) Unary operator   B) Binary operator   C) Ternary operator   D) Assignment operator

  4. sizeof belongs to which type?

    A) Arithmetic   B) Logical   C) Special   D) Bitwise

  5. Value of 17 % 5:

    A) 3   B) 2   C) 3.4   D) 12

Show answers
  1. B: operands.
  2. C: ? :
  3. A: unary.
  4. C: special.
  5. B: 2.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

OperatorSymbol that performs an operation
OperandValue the operator works on
ExpressionOperators + operands that give a value
8 typesArithmetic, Relational, Logical, Assignment, Increment, Bitwise, Conditional, Special
By operandsUnary (1), Binary (2), Ternary (3)
Only ternary? :
True / false1 / 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 %.