Operators in C

Operators in C


Introduction

In C programming, operators are used to perform operations on variables and values.
They are essential for performing calculations, comparisons, and logical decisions in programs.

👉 In simple terms:
Operators are symbols that perform operations on operands (values).


What are Operators?

An operator is a symbol that tells the compiler to perform a specific operation.

👉 Example:

int a = 10 + 5;

Here:

  • + → operator
  • 10 and 5 → operands

Types of Operators in C

C provides several types of operators:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Increment & Decrement Operators
  6. Bitwise Operators
  7. Conditional (Ternary) Operator
  8. Special Operators

1. Arithmetic Operators

Used for mathematical calculations:

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b

Example

#include <stdio.h> int main() { int a = 10, b = 3; printf("Addition = %d\n", a + b); printf("Subtraction = %d\n", a - b); printf("Multiplication = %d\n", a * b); printf("Division = %d\n", a / b); printf("Modulus = %d\n", a % b); return 0; }

Output

Addition = 13 Subtraction = 7 Multiplication = 30 Division = 3 Modulus = 1

2. Relational Operators

Used to compare values:

Operator Meaning Example
== Equal to a == b
!= Not equal a != b
> Greater than a > b
< Less than a < b
>= Greater or equal a >= b
<= Less or equal a <= b

Example

#include <stdio.h>
int main() {
    int a = 5, b = 10;

    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);

    return 0;
}

Output

a == b = 0
a != b = 1
a > b = 0
a < b = 1

3. Logical Operators

Used for decision making:

Operator Meaning Example
&& Logical AND a > 0 && b > 0
|| Logical OR a > 0 || b > 0
! Logical NOT !(a > b)

Example

#include <stdio.h>
int main() {
    int a = 1, b = 0;

    printf("AND = %d\n", a && b);
    printf("OR = %d\n", a || b);
    printf("NOT = %d\n", !a);

    return 0;
}

Output

AND = 0
OR = 1
NOT = 0

4. Assignment Operators

Used to assign values:

Operator Description Example
= Assign a = 10
+= Add and assign a += 5
-= Subtract and assign a -= 5
*= Multiply and assign a *= 2
/= Divide and assign a /= 2

Example

#include <stdio.h>
int main() {
    int a = 10;

    a += 5;
    printf("a += 5 → %d\n", a);

    a -= 3;
    printf("a -= 3 → %d\n", a);

    a *= 2;
    printf("a *= 2 → %d\n", a);

    a /= 4;
    printf("a /= 4 → %d\n", a);

    return 0;
}

Output

a += 5 → 15
a -= 3 → 12
a *= 2 → 24
a /= 4 → 6

5. Increment & Decrement Operators

Operator Description Example
++ Increment (increase by 1) a++ or ++a
-- Decrement (decrease by 1) a-- or --a

Example 1

int x = 5;
x++; // x becomes 6
x--; // x becomes 5

Example 2

#include <stdio.h>
int main() {
    int a = 5;

    printf("a++ = %d\n", a++);
    printf("After a++ → %d\n", a);

    printf("++a = %d\n", ++a);

    return 0;
}

Output

a++ = 5
After a++ → 6
++a = 7

6. Bitwise Operators

Used for bit-level operations:

Operator Description Example
& Bitwise AND a & b
| Bitwise OR a | b
^ Bitwise XOR a ^ b
~ Bitwise NOT ~a
<< Left Shift a << 1
>> Right Shift a >> 1

Example

#include <stdio.h>
int main() {
    int a = 5, 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 = %d\n", ~a);
    printf("a << 1 = %d\n", a << 1);
    printf("a >> 1 = %d\n", a >> 1);

    return 0;
}

Output

a & b = 1
a | b = 7
a ^ b = 6
~a = -6
a << 1 = 10
a >> 1 = 2

7. Conditional (Ternary) Operator

Used as a shortcut for if-else:

condition ? expression1 : expression2;

Example

#include <stdio.h> int main() { int a = 10, b = 20; int max = (a > b) ? a : b; printf("Max = %d", max); return 0; }

Output

Max = 20

8. Special Operators

Operator Description Example
sizeof Returns size of variable sizeof(int)
& Address of operator &a
* Pointer operator *ptr

Example

#include <stdio.h>
int main() {
    int a = 10;
    int *ptr = &a;

    printf("Size of int = %lu\n", sizeof(a));
    printf("Address of a = %p\n", &a);
    printf("Value using pointer = %d\n", *ptr);

    return 0;
}

Output

Size of int = 4
Address of a = (memory address)
Value using pointer = 10

Operator Precedence

Operators follow priority rules:

Example:

int result = 10 + 5 * 2;

Output:

20

✔ Multiplication happens before addition


Common Mistakes

  • ❌ Confusing = and ==
  • ❌ Division with integers
  • ❌ Incorrect use of logical operators
  • ❌ Forgetting precedence rules

Pro Tips

  • ✔ Use parentheses to control operations
  • ✔ Practice each operator type
  • ✔ Understand precedence clearly
  • ✔ Write clean expressions

Conclusion

Operators are essential in C programming. They allow you to perform calculations, comparisons, and logical decisions effectively.

Mastering operators is a key step toward becoming a strong programmer.

👉 This article is part of Dharani Tech Edu Hub — where learning programming is made simple and practical.

Comments

  1. Great work. It will helpful for me. Thankyou

    ReplyDelete
    Replies
    1. Thank you, YOKESH! 😊
      Happy to hear that it helped you. Your support means a lot! 🙌

      Delete

Post a Comment

Popular posts from this blog

Introduction to C Programming

Input & Output in C