C Menu


Operators




It is a special kind of symbols which perform a perticular task. Depends on number of operands, this operators are classified into 3 types.

When we are evaluating any expression what input data we are passing it is called operand, which symbol we are using it is called operator.

  1. Unary Operator
  2. Binary Operator
  3. Ternary Operator

When we are working with unary operator it required only one operand. Binary operator takes 2 operands and ternary operator takes 3 operands

Operators are classified into

  1. Arithmetic operators (Binary Operator)
  2. Relational operators (Binary Operator)
  3. Logical operators (Binary Operator)
  4. Assignment operators (Binary Operator)
  5. Increment and Decrement operators (Unary Operator)
  6. Bitwise operators (Binary Operator)
  7. Conditional operators (Ternary Operator)
  8. Special operators

1. Arithmetic operators

Arithmetic operators are used to perform arithmetic operations between two operands.

Operator Description
+ (addition) Add two operands
- (subtraction) Subtract right operand from the left
*(multiplication) Multiply two operands
/ (divide) Divide left operand by the right one (always results into float)
%( reminder) Modulus - remainder of the division of left operand by the right
Example:
// Working of arithmetic operators
#include < stdio.h >
int main()
{
    int a = 10,b = 5, 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("a % b = %d \n",c);
    
    return 0;
}
Output:
a + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0

2. Relational operators

A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0

Operator Description
== (Equal to) Equal to - True if both operands are equal.
> (Greater than) Greater that - True if left operand is greater than the right
< (Less than) Less that - True if left operand is less than the right
!= (Not equal to) Not equal to - True if operands are not equal.
>= (Greater than or equal) Greater than or equal to - True if left operand is greater than or equal to the right
<= (Less than or equal) Less than or equal to - True if left operand is less than or equal to the right
Example:
 // Working of relational operators
#include < stdio.h >
int 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);

    return 0;
}
Output:

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 
	

3. Logical operators

The logical operators are used primarily in the expression evaluation to make a decision.

Operator Description
&& (logical and) Returns True if both statements are true
|| (logical or) Returns True if one of the statements is true
! (logical not) Reverse the result, returns False if the result is true
Example:
// Working of logical operators
#include < stdio.h >
int main()
{
	int a= 10, b = 20, c= 30;				
	printf ("(a>b) && (a<c) is %d", (a>b) && (a<c));
	printf ("(a>b) || (a<c) is %d", (a>b) || (a<c));
	printf ("!(a>b) is %d", !(a>b));
	return 0;
}
Output:
(a>b) && (a<c) is 0
(a>b) || (a<c) is 1
!(a>b) is 1		
		

4. Assignment operators

The assignment operators are used to assign the value of the right expression to the left operand.

Operator Description
= (Assigns to) Assigns values from right side operands to left side operand.
+= (Assignment after Addition) It adds right operand to the left operand and assign the result to left operand.
-= (Assignment after Subtraction) It subtracts right operand from the left operand and assign the result to left operand.
*= (Assignment after Multiplication) It multiplies right operand with the left operand and assign the result to left operand.
/= (Assignment after Division) It divides left operand with the right operand and assign the result to left operand.
%= (Assignment after Modulus) It takes modulus using two operands and assign the result to left operand.
Example:
// Working of Increment and Decrement operators
#include < stdio.h >
int main()
{
	 int a = 5, c;

    c = a;      
    printf("c = %d\n", c);
    c += a;     //it is equal to c=c+a
    printf("c = %d\n", c);
    c -= a;     //it is equal to c=c-a
    printf("c = %d\n", c);
    c *= a;     //it is equal to c=c*a
    printf("c = %d\n", c);
    c /= a;     //it is equal to c=c/a
    printf("c = %d\n", c);
    c %= a;     //it is equal to c=c%a
    printf("c = %d\n", c);

	return(0)
}
Output:
c = 5 
c = 10 
c = 5 
c = 25 
c = 5 
c = 0

5. Increment and Decrement operators

Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.

Operator Description
++ (Increment) Increment - It increments the value of operand by one.
-- (Decrement) Decrement - It decrements the value of operand by one.

we have 2 types of Increment and Decrement operator

  • pre increment and pre decrement
  • post increment and post decrement
Example:
// Working of Increment and Decrement operators
#include < stdio.h >
int main()
{
	int a=5,b=8,c=4,d=2;
	int a1,b1,c1,d1;
	a1=a++; //post increment, first value in a assigns to a1 then a increments  
	b1=++b; //pre increment, first b value increment than assigns to b1 
	c1=c--;  //post decrement, first value in c assigns to c1 then a decrement 
	d1=--d;  //pre decrement, first d value decrement than assigns to d1
	printf("a1 = %d, a = %d",a1,a)
	printf("\n b1 = %d, b = %d",b1,b)
	printf("\n c1 = %d, c = %d",c1,c)
	printf("\n d1 = %d, d = %d",d1,d)

	return(0)
}
Output:
a1 = 5, a = 6
b1 = 9, b = 9
c1 = 4, c = 3
d1 = 1, d = 1

6. Bitwise operators

The bitwise operators perform bit by bit operation on the values of the two operands.

Operator Description
& (binary and) Sets each bit to 1 if both bits are 1
| (binary or) Sets each bit to 1 if one of two bits is 1
^ (binary xor) Sets each bit to 1 if only one of two bits is 1
~ (negation) Inverts all the bits
<< (left shift) Shift left by pushing zeros in from the right and let the leftmost bits fall off
>> (right shift) Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
Example:
// Working of logical operators
#include < stdio.h >
int main()
{
	a = 9     //equal to 1001
	b = 12    //equal to 1100

	print ("a & b = ",a & b)
	print ("a | b = ",a | b)
	print("a ^ b = ",a ^ b)
	print("~a = ",~a)
	print("a << 2 = ",a << 2)
	print("a >> 2 = ",a >> 2)

	return(0)
}
Output:
a & b = 8
a | b = 13
a ^ b = 5
~a = -10
a << 2 = 36
a >> 2 = 2

7. Conditional operators

The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'.

Syntax
Expression1? expression2: expression3;
Example:
// Working of conditional operator
#include 
int main()
{
	int a=5,b=10;
	a>b?printf("a is Greater"):printf("b is Greater");

	return(0)
}
Output:
b is Greater

8. Special operators

The sizeof operator
The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).

Example:
// Working of conditional operator
#include < stdio.h >
int main()
{
	int a;
    float b;
    double c;
    char d;
    printf("Size of int=%lu bytes\n",sizeof(a));
    printf("Size of float=%lu bytes\n",sizeof(b));
    printf("Size of double=%lu bytes\n",sizeof(c));
    printf("Size of char=%lu byte\n",sizeof(d));

	return(0)
}
Output:
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

Next Topic :Expression Evaluation