Operators in C

An operator is a symbol that tells the compiler to perform a specific operation on one or more operands. Operators are the building blocks of expressions and computations in C. For example, in 5 + 3, the symbol + is the operator and 5 and 3 are the operands.

C has a rich collection of operators organized into different categories based on their purpose.

Types of Operators in C

  1. Arithmetic Operators
  2. Relational (Comparison) Operators
  3. Logical Operators
  4. Assignment Operators
  5. Increment and Decrement Operators
  6. Bitwise Operators
  7. Conditional (Ternary) Operator
  8. sizeof Operator
  9. Comma Operator

1. Arithmetic Operators

Used to perform basic mathematical operations like addition, subtraction, multiplication, and division.

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33 (integer division)
%Modulus (Remainder)10 % 31

#include <stdio.h>

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

    printf("Addition       : %d\n", a + b);  // 13
    printf("Subtraction    : %d\n", a - b);  // 7
    printf("Multiplication : %d\n", a * b);  // 30
    printf("Division       : %d\n", a / b);  // 3
    printf("Modulus        : %d\n", a % b);  // 1

    return 0;
}

Note: When both operands are integers, division gives an integer result. Use float to get decimal results.


float result = (float)10 / 3;
printf("%.2f", result);  // Output: 3.33

2. Relational (Comparison) Operators

Used to compare two values. The result is either 1 (true) or 0 (false).

OperatorMeaningExampleResult
==Equal to5 == 51 (true)
!=Not equal to5 != 31 (true)
>Greater than7 > 41 (true)
<Less than3 < 81 (true)
>=Greater than or equal to5 >= 51 (true)
<=Less than or equal to3 <= 20 (false)

#include <stdio.h>

int main()
{
    int x = 8, y = 5;

    printf("%d == %d : %d\n", x, y, x == y);  // 0
    printf("%d != %d : %d\n", x, y, x != y);  // 1
    printf("%d >  %d : %d\n", x, y, x > y);   // 1
    printf("%d <  %d : %d\n", x, y, x < y);   // 0

    return 0;
}

3. Logical Operators

Used to combine multiple conditions. Commonly used in if statements and loops.

OperatorNameDescriptionExample
&&Logical ANDTrue only if both conditions are true(5 > 3) && (8 > 6) → 1
||Logical ORTrue if at least one condition is true(5 > 3) || (2 > 8) → 1
!Logical NOTReverses the result!(5 > 3) → 0

#include <stdio.h>

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

    // AND: both must be true
    if (age >= 18 && hasID == 1)
        printf("Entry allowed\n");

    // OR: at least one must be true
    if (age >= 18 || hasID == 1)
        printf("May enter\n");

    // NOT: reverses the result
    if (!hasID)
        printf("No ID found\n");
    else
        printf("ID is present\n");

    return 0;
}

Output


Entry allowed
May enter
ID is present

4. Assignment Operators

Used to assign values to variables. The = operator assigns the value on the right to the variable on the left. Compound assignment operators combine an arithmetic operation with assignment.

OperatorExampleEquivalent To
=a = 5a = 5
+=a += 3a = a + 3
-=a -= 3a = a - 3
*=a *= 3a = a * 3
/=a /= 3a = a / 3
%=a %= 3a = a % 3

#include <stdio.h>

int main()
{
    int a = 10;

    a += 5;  printf("After += 5: %d\n", a);  // 15
    a -= 3;  printf("After -= 3: %d\n", a);  // 12
    a *= 2;  printf("After *= 2: %d\n", a);  // 24
    a /= 4;  printf("After /= 4: %d\n", a);  // 6
    a %= 4;  printf("After %%= 4: %d\n", a);  // 2

    return 0;
}

5. Increment and Decrement Operators

Used to increase or decrease the value of a variable by 1.

OperatorNameDescription
++aPre-incrementIncrement first, then use the value
a++Post-incrementUse the value first, then increment
--aPre-decrementDecrement first, then use the value
a--Post-decrementUse the value first, then decrement

#include <stdio.h>

int main()
{
    int a = 5;

    printf("a = %d\n", a);      // 5
    printf("a++ = %d\n", a++);  // 5 (uses 5, then a becomes 6)
    printf("a = %d\n", a);      // 6

    printf("++a = %d\n", ++a);  // 7 (increments first, then uses 7)
    printf("a = %d\n", a);      // 7

    return 0;
}

Output


a = 5
a++ = 5
a = 6
++a = 7
a = 7

6. Conditional (Ternary) Operator

The ternary operator is a shorthand for a simple if-else statement. It takes three operands.

Syntax


condition ? value_if_true : value_if_false;

#include <stdio.h>

int main()
{
    int marks = 75;
    char *result;

    result = (marks >= 50) ? "Pass" : "Fail";
    printf("Result: %s\n", result);

    return 0;
}

Output


Result: Pass

7. Bitwise Operators

Operate directly on the binary (bit-level) representation of a number. Used in low-level programming, embedded systems, and performance-critical code.

OperatorNameExample
&Bitwise AND5 & 3 = 1
|Bitwise OR5 | 3 = 7
^Bitwise XOR5 ^ 3 = 6
~Bitwise NOT~5 = -6
<<Left Shift5 << 1 = 10
>>Right Shift5 >> 1 = 2

8. sizeof Operator

Returns the size in bytes of a variable or data type.


#include <stdio.h>

int main()
{
    int num = 10;
    printf("Size of int    : %zu bytes\n", sizeof(int));
    printf("Size of num    : %zu bytes\n", sizeof(num));
    printf("Size of double : %zu bytes\n", sizeof(double));
    return 0;
}

Operator Precedence in C

When multiple operators appear in an expression, C evaluates them based on precedence (priority). Higher precedence operators are evaluated first. Operators at the same level are evaluated left-to-right (left associativity) or right-to-left (right associativity).

PrecedenceOperatorsAssociativity
1 (Highest)() [] . ->Left to Right
2++ -- ! ~ (unary)Right to Left
3* / %Left to Right
4+ -Left to Right
5<< >>Left to Right
6< <= > >=Left to Right
7== !=Left to Right
8& | ^Left to Right
9&& ||Left to Right
10?:Right to Left
11 (Lowest)= += -= etc.Right to Left

Precedence Example


int result = 2 + 3 * 4;
// * has higher precedence than +
// So: 2 + (3 * 4) = 2 + 12 = 14
printf("%d", result);  // Output: 14

int result2 = (2 + 3) * 4;
// Parentheses force evaluation first
// So: (5) * 4 = 20
printf("%d", result2);  // Output: 20

Summary

C provides a comprehensive set of operators for performing arithmetic, comparisons, logical decisions, assignments, and low-level bit manipulation. Understanding how each operator works and how operator precedence affects expression evaluation is crucial for writing correct and efficient C programs. Using parentheses whenever there is any doubt about the order of operations is always a good practice.

Leave a Comment

Your email address will not be published. Required fields are marked *