/    /  Operators

Operators

 

Operators are the symbols that operate on operands (value or a variable).

The C language has a wide variety of operators to carry out various operations:

  1. Arithmetic
  2. Relational
  3. Logical
  4. Assignment
  5. Bitwise
  6. Conditional
  7. Increment and decrement

Operators

Arithmetic operators:

These are used to perform mathematical calculations.

 

Example:

 

a+b

 

where a, b are operands and, + is an arithmetic operator.

 

Relational operators:

 

These operators check the relation between 2 operands and return value 1 if it is true otherwise it returns 0. These are utilized in decision making and loops in the C program.

 

Example:

 

a>b

 

where > is a relational operator.

 

Operators


Logical operators:

 

These combine expressions containing relational operators.

 

Operators

Bitwise operators:

 

They perform bit-level operations on each bit of data.

 

Operators

Example:

 

Operators

 

Conditional operator:

 

These are utilized for decision making in C programming .it takes 3 operands and consists of 2 symbols (? And :). It performs different instructions according to the test condition, be it true or false.

 

Syntax:

 

conditional expression ? expression1 : expression2;

 

If the test condition is true, the first expression is returned, and if false, the second expression is returned.

 

Example:

 

C=(a>b)?a:b;
C=(c>0)?10:10;

 

Comma operator:

 

A set of expressions separated by a comma is a valid constant in the C language.

 

int i, j; // i and j are declared by the statements 
i=(j=10, j+20);

 

These expressions are evaluated from left to right, first the value 10 is assigned to j and then expression j+20 is evaluated so the final value of i will be 30.

 

Sizeof operator:

 

It is a unary operator that is used in finding the size of data types, constants, arrays, structures, etc.

 

Example:

 

#include <stdio.h> void main()
{
int a; 
float b;
printf(“sizeof int=%d bytes \n”,sizeof(a)); 
printf(“sizeof float=%d bytes \n”,sizeof(b));
}

 

Increment and decrement operators:

 

In C, ‘++’ is called an increment operator and ‘- -‘ is called a decrement operator.

 

They are classified into 2 types:

  1. Pre-increment and post-increment.
  2. Pre-decrement and post-decrement

 

Pre-increment:

 

First the variable is incremented by 1 and only after that any operation is performed on the variable.

 

Syntax:

 

++variable;

 

Example:

 

++i or ++a; 
int a, i=2;
a=++i;

 

In the above example, first i is incremented to 1, i becomes 3 and after that i will be assigned to a.

 

Post-increment:

 

First the operation is performed on a variable and only after that variable is incremented by 1.

 

Syntax:

 

variable++;

 

Example:

 

i ++ or a++; 
int a, i=2;
a=i++;

 

In the above example, first i is assigned to a and after that i will be incremented by 1.

 

Pre-decrement:

 

First the variable is decremented by 1 and only after that any operation is performed on the variable.

 

Syntax:

 

--variable;

 

Example:

 

--i or --a; 
int a, i=2;
a=--i;

 

In the above example, first ‘i’ is decremented to 1, and only after that ‘i’ will be assigned to a.

 

Post-decrement:

 

First the operation is performed on a variable and only after that variable is decremented by 1.

 

Syntax:

 

variable--;

 

Example:

 

i-- or a--; 
int a, i=2;
a=i--;

 

In the above example, first ‘i’ is assigned to a, and only after that ‘i’ will be decremented by 1.