Operator in C Language

An operator is a symbol which operates on a variable or value. There are types of operators like arithmetic, logical, conditional, relational, bitwise, assignment operators etc. Some special types of operators are also present in C like sizeof(), Pointer operator, Reference operator etc.

There are Three types of Operator :

  • Unary Operators
  • Binary Operators
  • Ternary Operators

Unary Operators

  • increment(++)
  • decrement(--)
  • sizeof()

increment

It is used to increment the value of the variable by 1.

The increment can be done in two ways:

1.prefix increment

In this method, the operator preceeds the operand (e.g.,++a). The value of operand will be altered before it is used.

int a = 1;
int b = ++a; //b=2

2.postfix increment

In this method, the operator follows the operand (e.g.,a++). The value of operand will be altered after it is used.

int a = 1;
int b = a++; // b = 1
int c = a; // c = 2

Decrement

It is used to decrement the value of the variable by 1.

The decrement can be done in two ways:

1.prefix decrement

In this method, the operator preceeds the operand (e.g.,--a). The value of operand will be altered before it is used.

int a = 1;
int b = --a; //b=0

2.postfix decrement

In this method, the operator follows the operand (e.g.,a--). The value of operand will be altered after it is used.

int a = 1;
int b = a--; // b = 1
int c = a; // c = 0

sizeof()

This operator returns the size of its operand, in bytes. the sizeof operator always precedes its operand. the operand is an expression, or it may be a cast.

#include< stdio.h >
void main()
{
float n = 0;
printf("Size of n = ",sizeof(n););
}

Output:

Size of n = 4

Binary Operators

  • Arithmatic Operator
  • Relational Operator
  • logical Operator
  • Bitwise Operator

Arithmetic Operators

Arithmetic operators are the operators used to perform the arithmetic operation like addition, subtraction, multiplication, division, and modulo operation.

Symbol Meaning Example
+ Addition a=12 and b=12 then a+b=24
- Subtraction a=12 and b=10 then a-b=2
* Multiplication a=5 and b=5 then a*b=25
/ Division a=12 and b=3 then a/b=4
% Modulo a=23and b=10 then a%b=3


Relational Operators

These operators are very helpful for making decisions. Depending upon the condition, it returns either 0 or 1. When the condition with these operators is true, 1 is returned. If the condition is false. it returns 0.

Symbol Meaning Example
>= Greater than or equal To a=12 and b=14 then a>=b give false(0)
<= less than or equal To a=10 and b=10 then a<=b give true(1)
== equal To a=5 and b=5 then a==b give true(1)
!= not equal To a=12 and b=13 then a!=b give true(1)


Logical Operators

These operators are generally used along with relation operators.Like relational operators,output of these operators is either True(1) or false (0).

Symbol Meaning Example
&& Logical AND (2>3 && 4>3) Evaluates to false && true Which then evaluates to False(0).
|| Loical OR (2>3 || 4>3) Evaluates to false || true Which then evaluates to False(1).
! Logcal NOT ! (2>3) Evaluates to !(false) Which then evaluates to True(1).

Bitwise Operators

The bitwise operators are the bit manipulation operators. They can manipulate individual bits.

Symbol Meaning Example
~ Bitwise Complement if a=01011 then ~a gives 10100
& Bitwise AND if a=1001 and b=1111 then a&b gives 1001
| Bitwise OR if a=1001 and b=1111 then a|b gives 1111/td>
^ Bitwise XOR if a=1001 and b=1111 then a^b gives 0110

Ternary Operators

?:

The ternary operators is an operators that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comprison, and the third is the result upon a false comparison.it is also called as Conditional operator.

Syntax

expression-1 ? expression-2 : expression-3

Example

a>b ? printf("A is less"); : printf("b is less");