⬅ Previous Next ➡

Operators & Expressions

Operators & Expressions in C
  • Operators are symbols used to perform operations on operands (variables/values).
  • An Expression is a combination of operands and operators that produces a result.
  • C supports many operator types: arithmetic, relational, logical, assignment, unary, bitwise, conditional, and more.

1) Arithmetic Operators

  • + Addition, - Subtraction, * Multiplication, / Division, % Modulus (remainder)
  • Note: % works only with integers.
#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);   // int division
    printf("a%%b=%d\n", a % b);
    return 0;
}

2) Relational (Comparison) Operators

  • Return 1 (true) or 0 (false).
  • ==, !=, >, <, >=, <=
#include <stdio.h>

int main() {
    int x = 5, y = 8;
    printf("x==y: %d\n", x == y);
    printf("x!=y: %d\n", x != y);
    printf("x<y : %d\n", x < y);
    printf("x>y : %d\n", x > y);
    return 0;
}

3) Logical Operators

  • Used with conditions: && (AND), || (OR), ! (NOT)
  • Short-circuit: in &&, if first is false, second not checked; in ||, if first is true, second not checked.
#include <stdio.h>

int main() {
    int age = 20;
    int hasId = 1;

    printf("Eligible: %d\n", (age >= 18) && (hasId == 1));
    printf("OR demo: %d\n", (age < 18) || (hasId == 1));
    printf("NOT demo: %d\n", !(hasId == 1));
    return 0;
}

4) Assignment Operators

  • =, +=, -=, *=, /=, %=
#include <stdio.h>

int main() {
    int n = 10;
    n += 5;  // 15
    n -= 2;  // 13
    n *= 2;  // 26
    n /= 2;  // 13
    n %= 5;  // 3
    printf("n=%d\n", n);
    return 0;
}

5) Unary Operators

  • ++ increment, -- decrement, + unary plus, - unary minus, ! logical NOT
  • Pre: ++a changes then uses, Post: a++ uses then changes.
#include <stdio.h>

int main() {
    int a = 5;
    printf("++a = %d\n", ++a); // 6
    printf("a++ = %d\n", a++); // 6 (then becomes 7)
    printf("a   = %d\n", a);   // 7
    return 0;
}

6) Bitwise Operators

  • Works on bits: &, |, ^, ~, <<, >>
#include <stdio.h>

int main() {
    int a = 5;  // 0101
    int b = 3;  // 0011

    printf("a&b  = %d\n", a & b);  // 1
    printf("a|b  = %d\n", a | b);  // 7
    printf("a^b  = %d\n", a ^ b);  // 6
    printf("a<<1 = %d\n", a > 1); // 2
    return 0;
}

7) Conditional (Ternary) Operator

  • Syntax: condition ? value1 : value2
#include <stdio.h>

int main() {
    int marks = 72;
    char *result = (marks >= 40) ? "Pass" : "Fail";
    printf("Result: %s\n", result);
    return 0;
}

8) Operator Precedence & Associativity

  • Precedence: decides which operator executes first.
  • Associativity: decides direction when precedence is same (left-to-right or right-to-left).
  • Common order (high to low):
    • ()
    • Unary (++ -- + - ! ~)
    • * / %
    • + -
    • << >>
    • < <= > >=
    • == !=
    • &, ^, |
    • &&, ||
    • ?:
    • =, +=, -= ... (right-to-left)
  • Use () to avoid confusion.
#include <stdio.h>

int main() {
    int a = 10, b = 5, c = 2;

    int ans1 = a + b * c;     // * first => 10 + (5*2) = 20
    int ans2 = (a + b) * c;   // () first => (10+5)*2 = 30

    printf("ans1=%d\n", ans1);
    printf("ans2=%d\n", ans2);
    return 0;
}

9) Type Conversion & Casting

  • Implicit Type Conversion (automatic): smaller type → larger type (int → float).
  • Explicit Type Casting (manual): larger type → smaller type using (type).
#include <stdio.h>

int main() {
    int a = 10;
    float b = a; // implicit (int -> float)
    printf("b=%.2f\n", b);

    float x = 12.75f;
    int y = (int)x; // explicit cast (float -> int)
    printf("y=%d\n", y);

    // Example: avoid int division
    int p = 5, q = 2;
    float div = (float)p / q;
    printf("division=%.2f\n", div);

    return 0;
}
⬅ Previous Next ➡