🟢 Beginner  ·  Lesson 08

Operators in C

Types of Operators in C

An operator is a symbol that tells the compiler to perform a specific operation on one or more operands. C has a rich set of operators that can be categorized as:

  • Arithmetic Operators: + - * / %
  • Relational (Comparison) Operators: == != > < >= <=
  • Logical Operators: && || !
  • Assignment Operators: = += -= *= /= %=
  • Increment/Decrement: ++ --
  • Bitwise Operators: & | ^ ~ << >>
  • Ternary Operator: ? :
  • sizeof Operator: sizeof()

1. Arithmetic Operators

Used to perform basic mathematical operations.

OperatorNameExample (a=10, b=3)Result
+Additiona + b13
-Subtractiona - b7
*Multiplicationa * b30
/Divisiona / b3 (integer division!)
%Modulus (Remainder)a % b1
C Language
#include <stdio.h>
int main() {
    int a = 10, b = 3;
    printf("a + b = %d\n", a + b);
    printf("a - b = %d\n", a - b);
    printf("a * b = %d\n", a * b);
    printf("a / b = %d\n", a / b);  // Integer division
    printf("a %% b = %d\n", a % b); // %% prints single %
    return 0;
}
a + b = 13 a - b = 7 a * b = 30 a / b = 3 a % b = 1
⚠️ Integer Division

10 / 3 gives 3, not 3.33! When both operands are integers, C performs integer division (truncates the decimal). Use 10.0 / 3 or cast: (float)10 / 3 to get 3.333333.

2. Relational Operators

Used to compare two values. Always return 1 (true) or 0 (false).

OperatorMeaninga=5, b=3Result
==Equal toa == b0 (false)
!=Not equal toa != b1 (true)
>Greater thana > b1 (true)
<Less thana < b0 (false)
>=Greater than or equala >= 51 (true)
<=Less than or equala <= 30 (false)
💡 Common Mistake: = vs ==

= is assignment (sets a value). == is comparison (checks equality). Writing if (a = 5) instead of if (a == 5) is a classic bug!

3. Logical Operators

Used to combine multiple conditions. Return 1 (true) or 0 (false).

OperatorNameExampleResult
&&AND1 && 00 — both must be true
||OR1 || 01 — at least one true
!NOT!10 — reverses the value
C Language – Logical Operators Example
#include <stdio.h>
int main() {
    int marks = 85, attendance = 78;

    // Eligible if marks >= 80 AND attendance >= 75
    if (marks >= 80 && attendance >= 75) {
        printf("Eligible for scholarship\n");
    }
    // Fail if marks < 40 OR attendance < 75
    if (marks < 40 || attendance < 75) {
        printf("Not eligible\n");
    }
    return 0;
}
Eligible for scholarship

4. Assignment Operators

OperatorExampleEquivalent to
=a = 5a = 5
+=a += 3a = a + 3
-=a -= 2a = a - 2
*=a *= 4a = a * 4
/=a /= 2a = a / 2
%=a %= 3a = a % 3

5. Increment & Decrement Operators

++ increases a value by 1, -- decreases by 1. These can be pre (before variable) or post (after variable).

C Language
#include <stdio.h>
int main() {
    int a = 5;
    printf("a++  = %d\n", a++);  // Prints 5, THEN increments to 6
    printf("a    = %d\n", a);    // Now a is 6
    printf("++a  = %d\n", ++a);  // Increments to 7, THEN prints 7
    printf("a--  = %d\n", a--);  // Prints 7, THEN decrements to 6
    printf("--a  = %d\n", --a);  // Decrements to 5, THEN prints 5
    return 0;
}
a++ = 5 a = 6 ++a = 7 a-- = 7 --a = 5

6. Ternary Operator ( ? : )

A short form of if-else. Syntax: condition ? value_if_true : value_if_false

C Language
#include <stdio.h>
int main() {
    int marks = 75;
    char *result = (marks >= 40) ? "Pass" : "Fail";
    printf("Result: %s\n", result);

    int a = 10, b = 20;
    int max = (a > b) ? a : b;
    printf("Max: %d\n", max);
    return 0;
}
Result: Pass Max: 20

Operator Precedence (Priority)

When an expression has multiple operators, precedence determines which gets evaluated first. Higher precedence = evaluated first.

PriorityOperatorsAssociativity
1 (Highest)() [] . ->Left to Right
2++ -- ! ~ (cast) sizeofRight to Left
3* / %Left to Right
4+ -Left to Right
5<< >>Left to Right
6< <= > >=Left to Right
7== !=Left to Right
8–12Bitwise &, ^, |, &&, ||Left to Right
13?:Right to Left
14 (Lowest)= += -= *= /=Right to Left

Example: 2 + 3 * 43 * 4 = 12 first, then 2 + 12 = 14 (not 20!).

Use parentheses () to override precedence: (2 + 3) * 4 = 20.

Summary

  • Arithmetic: + - * / % — basic math operations
  • Relational: == != > < — compare values, return 0 or 1
  • Logical: && || ! — combine conditions
  • Assignment: = += -= — assign values
  • Increment/Decrement: ++ -- — increase/decrease by 1
  • Ternary: ? : — one-line if-else
  • Don't confuse = (assignment) with == (comparison)
🏋️ Practice Exercise

Write a program that takes two numbers and prints: their sum, difference, product, quotient, remainder, which is greater (using ternary), and whether their sum is even or odd (using %).

Frequently Asked Questions

What are operators in C?
Operators are symbols that perform operations on values and variables, such as + for addition or == for comparison. C groups them into arithmetic, relational, logical, assignment, bitwise and a few others, each for a different kind of task.
What are the types of operators in C?
The main categories are arithmetic (+ - * / %), relational (< > == !=), logical (&& || !), assignment (= += -=), bitwise (& | ^), and special ones like the increment ++, decrement -- and the ternary ?:.
What is the difference between = and == in C?
A single = is the assignment operator, which stores a value in a variable. A double == is the equality operator, which compares two values and gives true or false. Mixing them up is a very common bug, especially inside if conditions.
What is the modulus operator in C?
The modulus operator % gives the remainder of an integer division. For example 10 % 3 is 1, because 10 divided by 3 leaves a remainder of 1. It is often used to check even/odd numbers or to wrap values around a range.
What is operator precedence in C?
Operator precedence decides the order in which operators are evaluated in an expression, much like maths where multiplication happens before addition. When unsure, you use parentheses to force the order you want and make the expression clear.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.