Explore
ProGrad Programs
About Us

Edit
Reply




Edit

Operators in C | Part1

Published on 07 Mar 2020

Operators are symbols used in an expression to manipulate operands. Whereas, operands are data items contained in an expression.

Operators can be either

  • Unary — requires only one operand
  • Binary — requires two operands


Types of operators

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Increment/Decrement Operators
  • Bitwise Operators
  • Conditional Operator
  • Assignment operators
  • Comma operator


1) Arithmetic Operators:


These operators are used to perform arithmetic/mathematical operations on operands. The operators falling into this category are:

  • Addition: The ‘+’ operator adds two operands.


  • Subtraction: The ‘-' operator subtracts two operands.


  • Multiplication: The ‘*’ operator multiplies two operands. 


  • Division: The ‘/’ operator divides the first operand by the second. 


  • Modulus: The ‘%’ operator returns the remainder when the first operand is divided by the second. 


  • Increment: The ‘++’ operator is used to increment the value of an integer. When the operator is placed before the variable name (also called pre-increment operator i.e ++x), its value is incremented instantly. And when the operator is placed after the variable name (also called post-increment operator i.e x++), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. 


  • Decrement: The ‘–‘ operator is used to decrement the value of an integer. When placed before the variable name (also called pre-decrement operator i.e --x), its value is decremented instantly. And when the operator is placed after the variable name (also called post-decrement operator i.e x--), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement.


Example

int main()
{
    int a = 10, b = 4, res;
    res = a++;
    printf("a is %d and res is %d\n", a, res); 
    res = a--;
    printf("a is %d and res is %d\n", a, res);  
    res = ++a;
    printf("a is %d and res is %d\n", a, res);
    res = --a;
    printf("a is %d and res is %d\n",a,res); 
    return 0;
}

 

 Output:

a is 11 and res is 10
a is 10 and res is 11
a is 11 and res is 11
a is 10 and res is 10

 

2) Relational Operators:


These operators are used for comparisons of two values.


  • ‘==’ operator checks whether the two given operands are equal or not. If so, it returns true. Else, it returns false. 
  • ‘!=’ operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise, it returns false. 
  • ‘>’ operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise, it returns false. 
  • ‘<‘ operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise, it returns false.
  • ‘>=’ operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise, it returns false. 
  • ‘<=’ operator checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise, it returns false. 

 

3) Logical Operators:


Logical operators are used to manipulate and create logical statements.

  • Logical AND: The ‘&&’ operator returns true when both the conditions under consideration are satisfied. Else, it returns false. 
  • Logical OR: The ‘||’ operator returns true when one (or both) of the conditions under consideration is satisfied. Else, it returns false. 
  • Logical NOT: The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise, it returns false. 


Example: 

int main()
{
    int a=10, b=4, c = 10, d = 20;
    if (a>b && c==d)
{
        printf("a is greater than b AND c is equal to d\n");
}
    else
{
        printf("AND condition not satisfied\n");
}


    if (a>b || c==d)
{
        printf("a is greater than b OR c is equal to d\n");
}
    else 
{
        printf("Neither a is greater than b nor c is equal "" to d\n");
}

 

    if (!a)
{
        printf("a is zero\n");
}
    else 
{
       printf("a is not zero");
}
    return 0;
}

 

Click here to read about the remaining operators.

If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in