C Operators

Welcome to The Coding College! In this tutorial, we’ll explore operators in C programming, essential tools for performing operations on variables and values. Operators are the building blocks of logic and computation in any C program, making them a must-know for every developer.

What Are Operators in C?

An operator in C is a symbol that tells the compiler to perform specific operations. These operations can range from arithmetic to logical, relational, or bitwise manipulations.

For example:

int a = 10, b = 5;  
int sum = a + b;  // '+' is an operator  

Types of Operators in C

C supports various operators, broadly classified as:

  1. Arithmetic Operators
  2. Relational (Comparison) Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Unary Operators
  7. Ternary Operators
  8. Special Operators

1. Arithmetic Operators

These operators perform basic mathematical operations.

OperatorDescriptionExampleOutput
+Additiona + bSum of a and b
-Subtractiona - bDifference of a and b
*Multiplicationa * bProduct of a and b
/Divisiona / bQuotient of a and b
%Modulus (remainder)a % bRemainder when a is divided by b

Example:

#include <stdio.h>

int main() {
    int a = 10, b = 3;

    printf("Addition: %d\n", a + b);
    printf("Modulus: %d\n", a % b);

    return 0;
}

Output:

Addition: 13  
Modulus: 1  

2. Relational (Comparison) Operators

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

OperatorDescriptionExampleOutput
==Equal toa == b1 (true) or 0 (false)
!=Not equal toa != b1 (true) or 0 (false)
>Greater thana > b1 or 0
<Less thana < b1 or 0
>=Greater than or equal toa >= b1 or 0
<=Less than or equal toa <= b1 or 0

3. Logical Operators

These operators combine two or more conditions.

OperatorDescriptionExampleOutput
&&Logical AND(a > b) && (a > 0)1 if both are true
``Logical OR
!Logical NOT!(a > b)Reverses the condition

4. Bitwise Operators

Perform bit-level operations.

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left Shifta << 2
>>Right Shifta >> 2

5. Assignment Operators

Used to assign values to variables.

OperatorDescriptionExample
=Assigna = 10
+=Add and assigna += 5
-=Subtract and assigna -= 5
*=Multiply and assigna *= 5
/=Divide and assigna /= 5
%=Modulus and assigna %= 5

6. Unary Operators

Operate on a single operand.

OperatorDescriptionExample
+Unary plus+a
-Unary minus-a
++Increment++a or a++
--Decrement--a or a--

7. Ternary Operator

The only ternary operator in C:

(condition) ? expression1 : expression2

Example:

#include <stdio.h>

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

    int max = (a > b) ? a : b;
    printf("Maximum: %d\n", max);

    return 0;
}

Output:

Maximum: 10

8. Special Operators

  1. Comma Operator (,): Used to separate multiple expressions.
  2. Pointer Operators (*, &): Used in pointer operations.
  3. Sizeof Operator: Returns the size of a variable or data type.

Examples of Operator Usage in C

Arithmetic Operators Example:

#include <stdio.h>

int main() {
    int a = 15, b = 4;

    printf("Sum: %d\n", a + b);
    printf("Difference: %d\n", a - b);
    printf("Product: %d\n", a * b);
    printf("Quotient: %d\n", a / b);
    printf("Remainder: %d\n", a % b);

    return 0;
}

Output:

Sum: 19  
Difference: 11  
Product: 60  
Quotient: 3  
Remainder: 3  

Conclusion

Operators in C are essential for performing operations, comparisons, and logical decisions in your programs. Whether it’s basic arithmetic or complex bit-level manipulation, mastering operators is key to becoming a proficient C programmer.

Leave a Comment