C Operators

Operators in C programming Language

  • An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bit-wise etc.
  • An operator is a symbol that work on a value or a variable. For example: + is an operator to perform addition.
  • There are following types of operators to perform different types of operations in C language.

  1. Arithmetic Operators
  2. Assignment Operator
  3. Relational Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Ternary or Conditional Operators
  7. Increment or Decrements
  8. sizeof Operator


Arithmetic Operators

  • An arithmetic operator is used to performs mathematical operations like addition, subtraction, multiplication, division etc on numerical values (variables oand constants).
Operator        Meaning of Operator
  1.     +                          addition or unary plus
  2.      -                           subtraction or unary minus
  3.     *                           multiplication
  4.     /                           division
  5.     %                         remainder after division (modulo division)
// Working of arithmetic operators

#include <stdio.h>
int main()
{
    int a = 9,b = 4, c;
    c = a+b;
    printf("a+b = %d \n",c);
    c = a-b;
    printf("a-b = %d \n",c);
    c = a*b;
    printf("a*b = %d \n",c);
    c = a/b;
    printf("a/b = %d \n",c);
    c = a%b;
    printf("Remainder when a divided by b = %d \n",c);
    return 0;
}

Output 
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

Assignment Operator

  • An assignment operator is normally used to assign a value to a variable. 
  • The most commonly assignment operator used is "=" (Equal to) 



// Working of assignment operators
#include <stdio.h>
int main()
{
    int a = 5, c;
    c = a;      // c is 5
    printf("c = %d\n", c);
    c += a;     // c is 10
    printf("c = %d\n", c);
    c -= a;     // c is 5
    printf("c = %d\n", c);
    c *= a;     // c is 25
    printf("c = %d\n", c);
    c /= a;     // c is 5
    printf("c = %d\n", c);
    c %= a;     // c = 0
    printf("c = %d\n", c);
    return 0;
}
Output

c = 5
c = 10
c = 5
c = 25
c = 5
c = 0

Relational Operators

  • A relational operator tells the relationship between two operands. 
  • If the relation is true, it returns 1; if the relation is false, it returns 0.
  • Relational operators can be used anywhere in decision making and loops.
Operator        Meaning of Operator       Example
==                       Equal to                                    5 == 3 is evaluated to 0
>                          Greater than                            5 > 3 is evaluated to 1
<                          Less than                                  5 < 3 is evaluated to 0
!=                         Not equal to                             5 != 3 is evaluated to 1
>=                       Greater than or equal to        5 >= 3 is evaluated to 1
<=                       Less than or equal to              5 <= 3 is evaluated to 0

// Working of relational operators
#include <stdio.h>
#include <conio.h>
void main()
{
    int a = 5, b = 5, c = 10;
    printf("%d == %d is %d \n", a, b, a == b);
    printf("%d == %d is %d \n", a, c, a == c);
    printf("%d > %d is %d \n", a, b, a > b);
    printf("%d > %d is %d \n", a, c, a > c);
    printf("%d < %d is %d \n", a, b, a < b);
    printf("%d < %d is %d \n", a, c, a < c);
    printf("%d != %d is %d \n", a, b, a != b);
    printf("%d != %d is %d \n", a, c, a != c);
    printf("%d >= %d is %d \n", a, b, a >= b);
    printf("%d >= %d is %d \n", a, c, a >= c);
    printf("%d <= %d is %d \n", a, b, a <= b);
    printf("%d <= %d is %d \n", a, c, a <= c);
    getch();
}

Output will be
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1

5 <= 10 is 1

Bitwise Operators

  • The Bitwise operators in C are some of the Operators which are used to perform bit operations. 
  • With the help of BO all the decimal values will be converted into binary values (sequence of bits i.e., 0100, 1100, 1000, 1001 etc.). 
  • The bitwise operators will work on these bits, such as shifting them left to right or converting bit value from 0 to 1, etc.




// Working of bitwise operators
#include <stdio.h>

#include <conio.h>
void main()
{
 int a = 9, b = 65;
 printf(" Bitwise AND Operator a&b = %d \n", a & b);
 printf(" Bitwise OR Operator a|b = %d \n", a | b);
 printf(" Bitwise EXCLUSIVE OR Operator a^b = %d \n", a ^ b);
 printf(" Bitwise NOT Operator ~a = %d \n", a = ~a);
 printf(" LEFT SHIFT Operator a<<1 = %d \n", a << 1);
 printf(" RIGHT SHIFT Operator b>>1 = %d \n", b >> 1);
 getch();
}

Output:

Increment or Decrements Operators

  • In C programming there are two operators increment ++ and decrement -- which are used to change the value of an operand (it may be constant or variable) by 1.
  • Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. 
Note:- These two operators are unary operators, meaning they only operate on a single operand.

// Working of increment and decrement operators

#include <stdio.h>
int main()
{
    int a = 10, b = 100;
    float c = 10.5, d = 100.5;
    printf("++a = %d \n", ++a);
    printf("--b = %d \n", --b);
    printf("++c = %f \n", ++c);
    printf("--d = %f \n", --d);
    return 0;
}

Output
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000

Precedence of Operators in C

  • The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operators direction to be evaluated, it may be left to right or right to left.
  • Let's understand the precedence by the example given below:
  • 1.    int value=10+20*10;  
  • The value variable will contain 210 because * (multiplicative operator) is evaluated before + (additive operator).
  • The precedence and associativity of C operators is given below:


Types of Operators 
Description
Arithmetic_operatorsThese are used to perform mathematical calculations like addition, subtraction, multiplication, division, and modulus
Assignment_operatorsThese are used to assign the values for the variables in C programs.
Relational operators
These operators are used to compare the value of two variables.
Logical operators
These operators are used to perform logical operations on the given two variables.
Bit wise operators
These operators are used to perform bit operations on given two variables.
Conditional (ternary) operators
Conditional operators return one value if the condition is true and returns another value if the condition is false.
Increment/decrement operators
These operators are used to either increase or decrease the value of the variable by one.
Special operators
&, *, sizeof( ) and ternary operators.

Post a Comment

Previous Post Next Post