Operators in C

Operators in C

Operators are symbols that can perform different operations on operands.

Types of Operators:
  • Arithmetic Operators
    For Example: int num1=5, num2=2;
    OperatorNameExampleResult
    +Additionnum1+num27
    -Subtractionnum1-num23
    *Multiplicationnum1*num210
    /Divisionnum1/num22
    %Modulo (Remainder)num1%num21
  • Relational Operators
    For Example: int num1=5, num2=2;
    OperatorNameExampleResult
    <Less thannum1<num20
    >Greater thannum1>num21
    <=Less than or equal tonum1<=num20
    >=Greater than or equal tonum1>=num21
    ==Equal tonum1==num20
    !=Not equal tonum1!=num21
  • Logical Operators
    For Example: int num1=5, num2=2;
    OperatorNameExampleResult
    &&Logical AND(num1>0) && (num2>0)1
    ||Logical OR(num1>0) || (num2<0)1
    !Logical NOT!(num1>0)0
  • Bit-wise Operators
    For Example: int num1=5, num2=2;
    OperatorNameExampleResult
    &Bitwise ANDnum1&num20
    |Bitwise ORnum1|num27
    ^Bitwise XORnum1^num27
    <<Left Shiftnum1<<110
    >>Right Shiftnum1>>12
    ~Bitwise NOT~num1-6
  • Assignment Operators
    For Example: int num1=5, num2=2;
    OperatorExampleResult
    (Value of num1)
    =num1=55
    +=num1+=num27
    -=num1-=num23
    *=num1*=num210
    /=num1/=num22
    %=num1%=num21
  • Increment and Decrement Operators
    For Example: int num1=5;
    OperatorExampleResult
    ++num1++6
    --num1--4

    Increment and decrement operators are used to increase or decrease the value of a variable by 1. These operators can be used in both prefix and postfix forms.

    Pre-increment or pre-decrement means increment or decrement the value of the variable before using it in an expression.

    For Example:
    #include <stdio.h>
    int main() {
        int num1 = 5, num2 = 2;
        int preIncrement = ++num1;
        int preDecrement = --num2;
        printf("num1 value: %d, Pre-incremented value: %d\n", num1, preIncrement);
        printf("num2 value: %d, Pre-decremented value: %d\n", num2, preDecrement);
        return 0;
    }
    Output:
    num1 value: 6, Pre-incremented value: 6
    num2 value: 1, Pre-decremented value: 1

    Post-increment or post-decrement means use the current value of the variable in an expression and then increment or decrement it.

    For Example:
    #include <stdio.h>
    int main() {
        int num1 = 5, num2 = 2;
        int postIncrement = num1++;
        int postDecrement = num2--;
        printf("num1 value: %d, Post-incremented value: %d\n", num1, postIncrement);
        printf("num2 value: %d, Post-decremented value: %d\n", num2, postDecrement);
        return 0;
    }
    Output:
    num1 value: 6, Post-incremented value: 5
    num2 value: 1, Post-decremented value: 2
  • Ternary Operator

    Ternary operator is a conditional operator that evaluates a condition and returns one of two values based on the result.

    Syntax:
    condition ? value_if_true : value_if_false;
    For Example: int num1=5, num2=2;
    OperatorExampleResult
    ?:(num1>num2)?num1:num25
  • Miscellaneous Operators

    These operators are used for various purposes in C programming.

    OperatorDescription
    ,Comma operator
    sizeofReturns the size of a variable or data type

Common Interview Questions

  1. Find sum of two numbers without using '+' operator?
  2. Find largest number without using relational operators?
  3. How to check given number is even or odd without using '%' operator?
  4. How to double a given integer without using '*' or '+' operators?