What is Operator in C-Programming Language?

Operators in c
Operators in c


An operator is a symbol that operates on a variable or value. It is used for performing certain operations like arithmetical, logical, relational, etc.

When a programmer wants to perform some types of mathematical operations then they have to use operators. The purpose of operators is mainly for mathematical and logical calculations.

Types of Operators:

In C-Programming Language there are various types of operators available to perform various operations.

Following are the list of operators present in c-programming language:-

1.Arithmetic operator : - (+,-,*,/,%).

The purpose of these operators is to perform mathematical operations like addition, subtraction, multiplication, division, modulus, etc.

2.Increment/Decrement Operators :- (++,--).

These are mainly used for incrementing and decrementing the value of an integer. '++' will increase the value by adding 1, and '--' will decrease the value by subtracting 1.

3.Assignment Operators :- (=,+=,-+,*=,/=,%=).

The purpose of these operators is to assign value to a variable. The most used operator is '='.

4.Relational Operators :- (==,<,>,>=,<=,!=).

These are mainly used for checking relationships between operands. With the help of this operators we can check whether one operand is equal to, less or greater then other operands or not.
It returns 1, when the relation is true and 0 when the relation is false.
It is also known as comparison operators.

5.Logical Operators :-(&&,||,!).

Logical operators is mainly used for decision making. A logical operator returns either 1 or 0 depends on condition is true or false.

6.Conditional Operator :- (?).

Also known as ternary operator. The main purpose of conditional operator is in decision making statements.

Syntax: 

Expression1 ? statement1 : statement2;

if the expression1 is true then statement1 will get executed, else statement2 will get executed.

7.Bitwise Operators :- (<<,>>,~,&,^,|).

These are special operators for bit operations between two variables.

8.Misc Operators :- (sizeof,&,*,?:).

It includes operators like sizeof(), reference operator(&), Poiner(*), condition(?).

Precedence :

Operator precedence determines which operator is evaluated first when an expression has more then one operators.

Example :

    a = 40 - 2 * 10

Here, 'a' will be assigned 20 because it is evaluated as 40-(2*10).

Because multiplication(*) has higher precedence then subtraction(-).

Associativity :

Associativity is used when there are two or more operators of same precedence is present in one expression.

For example:
Multiplication (*) and division (1) arithmetic. operators have some precedence.
So
a = 5 + 2 / 10 
This expression would be evaluated as (5+2)/10 because, the associativity is left to right for these operators.

Precedence and associativity table :


Precedence Associativity
*, /, % Left to right
+, - Left to right
==, != Left to right
<<, >> Left to right
<, >, <=, >= Left to right
& Left to right
=, *=, +=, -=, /=, %= Right to left
&=, ^=, |=, <<=, >>= Right to left
, Left to right
?: Right to left
|| Left to right
&& Left to right
| Left to right
(), [], ->, . Left to right