Operators In C

Operators In C

An operator is a symbol that is used for performing certain operations like arithmetical, logical, relational, assignment, etc.

Operators In C

Types of Operators

  • Arithmetic Operators
  • Relational Operators
  • Assignment Operators
  • Bitwise Operators
  • Ternary or Conditional Operators
  • Logical Operator
  • Misc Operator


Let's look each one by one with example


Arithmetic Operators


OperatorOperation
+addition
-subtraction
*multiplication
/division
%remainder after division


Code 


#include<stdio.h>

int main()

{

  int a = 2,b = 2, r;

  r = a+b;

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

  r = a-b;

  printf("Subtraction: a-b = %d \n",r);

  r = a*b;

  printf("Multiplication: a*b = %d \n",r);

  r = a/b;

  printf("Division: a/b = %d \n",r);

  r = a%b;

  printf("Remainder: %d \n",r);

  r = ++a;

  printf("Increment the value of a by 1: %d \n",r);

  r = --b;

  printf("Decremented the value of b by 1: %d \n",r);

  return 0;

}


Output

Addition: a+b = 4 
Subtraction: a-b = 0 
Multiplication: a*b = 4 
Division: a/b = 1 
Remainder: 0 
Increment the value of a by 1: 3 
Decremented the value of b by 1: 1 

Relational Operators



Relational operator checks the relation between two operands, and return 1(true) or 0(false) based on the operator.


OperatorOperationExample
==Equal to2 == 3 is evaluated to 0
>Greater than2 > 1 is evaluated to 1
<Less than2 < 1 is evaluated to 0
!=Not equal to2 != 1 is evaluated to 1
>=Greater than or equal to2 >= 2 is evaluated to 1
<=Less than or equal to2 <= 1 is evaluated to 0

Code


#include <stdio.h>
int main()
{
  int a = 2, b = 2;


  printf("%d == %d is %d \n", a, b, a == b);
  printf("%d > %d is %d \n", a, b, a > b);
  printf("%d < %d is %d \n", a, b, a < b);
  printf("%d != %d is %d \n", a, b, a != b);
  printf("%d >= %d is %d \n", a, b, a >= b);
  printf("%d <= %d is %d \n", a, b, a <= b);


  return 0;
}


Output


2 == 2 is 1 
2 > 2 is 0 
2 < 2 is 0 
2 != 2 is 0 
2 >= 2 is 1 
2 <= 2 is 1 

Assignment Operators


Assignement operator assign value to a variable.


OperatorExample
=a=b or b=a
+=a += b or a = a+b
-=a -=b or a = a-b
*=a *= b or a = a*b
/=a /= b or a = a/b
%=a %= b or a = a%b

Code 

#include <stdio.h>
int main()
{
  int a = 2, result;
  result = a;
  printf("Value of result = %d\n", result);
  result += a;
  printf("Value of result = %d\n", result); 
  result -= a; 
  printf("Value of result = %d\n", result);
  result *= a;  
  printf("Value of result = %d\n", result);
  result /= a;
  printf("Value of result = %d\n", result);
  return 0;
}


Output

Value of result = 2
Value of result = 4
Value of result = 2
Value of result = 4
Value of result = 2


Bitwise Operators



In C language we get operators for bit operation between two variables.


OperatorKnown As
<<Binary Left Shift Operator
>>Binary Right Shift Operator
~Binary Ones Complement Operator
&Binary AND Operator
^Binary XOR Operator
|Binary OR Operator

Code

#include <stdio.h>
int main() {
int a = 2, b = 3, result = 0;         
result = a & b;     
printf("AND Operator of a and b is %d\n", result );
result = a ^ b;
printf("XOR Operator of a and b is %d\n", result );
result = ~a;
printf("Ones Complement Operator of a is %d\n", result );
result = a << 2;
printf("Left Shift Operator of a is %d\n", result );
result = a >> 2;
printf("Right Shift Operator of a is %d\n", result );
return 0;
}


Output


AND Operator of a and b is 2
XOR Operator of a and b is 1
Ones Complement Operator of a is -3
Left Shift Operator of a is 8
Right Shift Operator of a is 0

Ternary or Conditional Operators


It is used in decision making statements.


expression? statement1: statement2; 


It is a boolean expression, it can be either true or false.
If the expression returns true then statement1 will get executed.
If the expression returns false then statement2 will get executed.


Code


#include <stdio.h>  
int main()  
{  
int num=2;
(num>1)? (printf("It is greater than number 1!")) : (printf("It is less than number 1!"));
return 0;  
}


Output


It is greater than number 1!


Logical Operators



It is used in decision making. It returns 0(false) or 1(true).


OperatorWhat it does
&& (Logical AND)True only if all conditions satisfy.
|| (Logical OR)True only if either one condition satisfies.
! (Logical Not)True only if the operand is 0.

Code

#include <stdio.h>  
  
int main ()  
{  
    int n = 2;  
    printf (" %d \n", (n == 2 && n >= 1));
    printf (" %d \n", (n >= 3 && n >= 0));  
    printf (" %d \n", (n == 2 && n >= 2));  
    printf (" %d \n", (n >= 1 && n <= 2));  
      
    return 0;  


Output


 1 
 0 
 1
 1


Misc Operators



It includes operators like sizeof(), reference operator(&), pointer operator(*), etc


OperatorExampleWhat it does
sizeofsizeof(a)Returns the size occupied by the data type of the operand. 
&&aRefers to the memory address where operand is stored.
**aA pointer.


Code


#include<stdio.h>
int main()
{
int num = 10, *pointer;
pointer = &num;
printf("Memory address: %d\n",pointer);
return 0;
}


Output


Memory address: 6422216
Load comments