Operators


What is operator?


An operator is a symbol used to perform certain mathematical or logical operations. It is used in programs to manipulate data and variables.

Types of Operators in C
  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Assignment operators
  • Increment and decrement operators
  • Conditional operators
  • Bitwise operators
  • Special operators

Arithmetic Operators


Arithmetic operators in C

If a & b are two integer variable are called Operands.
Examples: a-b, a+b, a*b, a/b, a%b

  • if a & b are integer than after Arithmetic operation ,the result is also integer.
  • if a & b are float or double than after Arithmetic operation ,the result is also float or double.
  • The % modulo operator cannot used on floating point data.

Relational Operators

Relational Operators are used to compare and check the relationship between two operands.
Example: comparing the age of two persons( 10<20 is true, 20<10 is false, 20!=20 is false ).



Relational operators in C

Logical Operators

C has the following three logical operators.


Logical operators in C

EXAMPLES: if (11>10 && 15>16) is false, if (11>10 || 15>16) is true.

Assignment Operators

Assignment operators are used to assign the result of an expression to a variable (Right operands to its left operands).
Equal (‘=’) Operator is used as assignment operator.
Example: x=5+4;
The result of expression “5+4” i.e 9 will be assigned to variable x .


Assignment operators in C


Output:

Enter the value of a 2
Enter the value of n 100
2
416


Conditional Operators

Conditional operators (? :) are ternary operators that takes three expressions as operand as shown below:
Syntax: exp1 ? exp2 : exp3
It evaluates exp2 when exp1 is True otherwise exp3 is evaluated .




Output:

a = 16 b = 10 c = 5
a/b = 1
a%b = 5
a *=b = 176
0
1
0




Increment and Decrement operators

Increment & Decrement are Unary Operators that add or subtract, to or from its operand.
Examples: ++a, a++, a--, --a



Output:

y=6 a=6



Output:

y=5 a=6



Output:

y=4 a=4



Output:

y=5 a=4


Bitwise Operators

For manipulation of data at bit level a special type of operators are used called Bitwise Operators.
We used it for shifting the bits right or left.
Note:Bitwise operator may not be applied to float or double. It takes only Integer as operand.




Output:

x is 122
y is 61
x is 61
x is 122
y1 is 244


Special Operators

C support some special type of operators such as comma operator , sizeof operator , pointer operator (*) .
Example:
< m=sizeof (sum); // sizeof operator
int a,b; //comma operator


Precedence of operators


Precedence determines the order in which operators are evaluated in the given expression in absense of paraentheses.
Note: Paraentheses is used to override the operator precedence.
x=9-4+3*2-1
Here we use precedence operator rule as priority is given in table.


Precedence Table

C operators are listed in order of precedence (highest to lowest). Their associativity indicates in what order operators of equal precedence in an expression are applied .


Precedence operators in C


Key points
  • When ++ or -- is used as postfix like y=a++.
    Then first, the value of ‘a’ is assign to y, after that ‘a’ is incremented.
  • When ++ or -- is used as prefix like y= ++a.
    Then first,the value of ‘a’ is incremented after that the resultant value of ‘a’ is assigned to y.


Do You Know?