Operators

Operators

Operator is a symbol or letter used to indicate a specific operation on variables in a program.
Operator act upon the data items called as operands.
Example: A+B ;
Here A and B are operand variable and + is operator , And the function of it to add the values stored in the variable A and B.

Rules for formation of an expression

  • A signed or unsigned constant or variable is an expression.
  • Two expression connected by an operator is also an expression .
  • Two operator should not occur in continuation .
Types of Operators
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Unary operators
6. Conditional or Ternary operators
7. Bit-wise operators
8. Comma operators

Arithmetic operators
Arithmetic operators are used to perform various  arithmetic calculations like addition, subtraction,  multiplication and division.

Arithmetic Operators:

Symbol Operation Example
+ Addition X+Y
Subtraction X-Y
* Multiplication X*Y
/ Division X/Y
% Modulus or remainder X%Y

Program to demonstrate Arithmetic Operators .

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, sum, sub, div, rem, mul;
clrscr();
printf(“enter value of a and b”);
scanf(“%d%d”,&a,&b);
sum=a+b;
sub=a-b;
mul=a*b;
div=a/b;
rem=a%b;
printf(“addition is =%d”,sum);
printf(“subtraction is =%d”,sub);
printf(“multiplication is =%d”,mul);
printf(“remainder is =%d”,rem);
printf(“division is =%d”,div);
}

2 Relational operators
A Relational operator is used to compare two values of the operands and the result of such an operation is always logical .
Relational operator are used in decision making statements.
Relational operators:

Operator Operation Example
> Greater than x>y
>= Greater than or equal to x>=y
< Less than X<y
<= Less then or equal to X<=y
== Equal to X==y
!= Not equal to X!=y

3 Logical Operators
Logical operators are used to connect relational expressions or logical expressions.
The result of such operations is always logical mean either true or false.

Operator Operation Example
! Logical NOT !x
&& Logical AND X&&y
|| Logical OR X||y

4 Assignment operators
Assignment operator is an operator in which the variable on the left side of the equal sign is set equal to the value of the operand on the right side of the equal sign.
The most commonly used assignment operation is =.
Identifier=expression.
Where identifier generally represents a variable and expression represents a constant.
Example:
a+=10;    a=a+10;
Note : if the two operands in an assignment expression are of different data types, then the value on the right will be automatically converted to the type of the identifier on the left.
5 unary operators
Unary operator are the operators that act upon a single operand to produce a new value.
Unary operator usually precede their single operands.

Operator Operation  Example
Unary minus -7.5, -(x+y)
++ Pre increment ++i
++ Post increment i++
Pre decrement –i
Post decrement i–
& Address of &a
* Value at address *a
! Not or negation !a
sizeof Size of data type Int i ; sizeof(i)

6 Conditional or Ternary Operators
Conditional operator (? : ) is the condensed from of an if then else statement of c.
The general from of conditional operator is :
exp 1 ? exp 2 :exp 3
where exp 1 , exp 2 and exp 3 are the expressions.
e.g  z=(x > y ) ? X:y;

 Program to find the greatest of two numbers using conditional operator.
#include<stdio.h>
#include<conio.h>
void main()
{
Int a, b, big;
Clrscr();
printf(“\n enter two numbers”);
scanf(“%d%d”, &a, &b);
big=(a > b)? a:b;
printf(“the greater number =%d, big”);
getch();
}

Bitwise Operators
Bitwise operators are the operators which operate at bit level and allows the user to manipulate individual bits.
Bitwise operator used in low level means machine level programming.

Operator Operation
<< Left shift of bits
>> Right shift of bits
One’s complement
& Bitwise AND
^ Bitwise XOR
| Bitwise OR