#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Operators

Operators in C: Types of Operators

What is an Operator?

An operator is a symbol that tells the compiler to perform a specific mathematical, relational, or logical operation.

Types of Operators in C

  • Arithmetic operators — + - * / %
  • Relational operators — == != > < >= <=
  • Logical operators — && || !
  • Bitwise operators — & | ^ ~ << >>
  • Assignment operators — = += -= *= /=
  • Increment/decrement operators — ++ --
  • Conditional (ternary) operator — ?:
  • Special operators — sizeof, comma operator, pointer operators * &

Understanding operator precedence and associativity is essential for writing correct expressions.

Operators in Action: One Program, Many Results

Seeing operators work on real values clears up most confusion. Notice especially how integer division and the modulo operator behave.

#include <stdio.h>

int main(void) {
    int a = 17, b = 5;

    printf("%d\n", a / b);        /* 3  : integer division drops the fraction */
    printf("%d\n", a % b);        /* 2  : remainder */
    printf("%.2f\n", a / 2.0);    /* 8.50 : one double operand gives a double result */
    printf("%d\n", a > b);        /* 1  : relational results are 1 (true) or 0 (false) */
    printf("%d\n", a & b);        /* 1  : 10001 AND 00101 = 00001 */
    printf("%d\n", b << 1);       /* 10 : shifting left by one doubles the value */

    int max = (a > b) ? a : b;    /* ternary picks the larger value */
    printf("%d\n", max);          /* 17 */
    return 0;
}

Pre-increment vs Post-increment

Both ++x and x++ add one to x. The difference is the value the expression gives back. Pre-increment changes x first and returns the new value. Post-increment returns the old value and then changes x.

int x = 5;
int y = x++;   /* y = 5, x = 6 */
int z = ++x;   /* x = 7, z = 7 */

On a line by itself, such as the update part of a for loop, the two forms behave the same. The difference only shows up when the result is used inside a larger expression.

How Precedence Decides the Answer

When an expression mixes operators, C evaluates higher-precedence operators first. Operators at the same level follow associativity, which is left to right for most groups and right to left for unary, ternary and assignment operators.

Level (high to low)Operators
Postfix() [] -> . and postfix ++ --
Unary! ~ ++ -- sizeof, unary - * &, casts
Multiplicative* / %
Additive+ -
Shift<< >>
Relational and equality< <= > >=, then == !=
Bitwise&, then ^, then |
Logical&&, then ||
Conditional, assignment, comma?:, = += -= and friends, then ,

So 2 + 3 * 4 is 14, not 20. A well-known trap is if (flags & MASK == 0): equality binds tighter than bitwise AND, so it is read as flags & (MASK == 0). Add parentheses whenever bitwise and comparison operators meet.

Mistakes Worth Avoiding

  • Using = instead of == in a condition. if (x = 0) assigns zero and the condition is always false. Compilers warn about this when warnings are enabled.
  • Modifying a variable twice in one expression. Code like i = i++ + ++i; is undefined behaviour; different compilers can give different results.
  • Forgetting short-circuit evaluation. In a && b, b is never evaluated if a is false. This is useful for guards like p != NULL && p->value > 0, but surprising if b had a side effect you relied on.
  • Applying % to floating-point values. It only works on integers; use fmod() from math.h for doubles.

Practical habit: compile with warnings on (for example gcc -Wall -Wextra) and use parentheses generously. Clear code beats clever code.

Ready to master real-world C Programming development?

Learn C Programming hands-on with mentor-led, live sessions.

Explore Course