Tuesday 1 October 2013

Operators in C

Data Type                                                                                                                         if else


Operators:- Operators are used for the logical and mathematical operations
Ex:- add , sum , divide , multiply , modulus etc.

Ex:-  a + b -c
 where + - are operators.
           a ,b, c are variables
   a+b-c are known as expression.

Types of C operator:-

  • Arithmetic Operator
  • Relational Operator
  • Bitwise Operator
  • Conditional Operator
  • Assignment Operator
  • Unary Operator

Arithmetic Operator:-Used for performing mathematical operations like add , sub , mul , divide etc.

Basic arithmetic operators

+                 addition
-                  subtraction
*                  multiply
\                    divide
%                 modulus

in modulus it display the remainder.

Relational operator:- used for displaying relations between two values.

Basic relational  operators

>                                greater than
<                                less than
>=                              greater than equal to
<=                              less than equal to
==                              equal to
!=                               not equal to

Bitwise Operator:- These operators perform operation bit by bit.

Basic Bitwise operator

      Bitwise Not                              ~
      Bitwise  AND                           &
      Bitwise   OR                             |
      Bitwise   XOR                          ^
      Bitwise   left shift                       <<
      Bitwise   Right shift                     >>

Conditional Operator:- It is same as the if else . if condition true executes first statement and if condition false executes second statement .

Ex:- int a=5,b=6;

if(a<b)? printf("a is less than b"): printf("a is greater than b");

output;-
T represents true
F represents false
 if condition true it prints first printf(_) else it go to second printf().
 if condition false it will execute the statement written after "colon :"


Assignment Operator:-it is used to assigned the value to the variables

Basic assignment operaotrs

a+= b                a=a+b
a-=b                  a= a-b
a*=b                  a= a*b
a\=b                   a= a\b
a%=b                 a=a%b
a&=b                 a=a&b
a|=b                   a= a|b
a^=b                  a= a^b
a>>=b               a=a>>b
a<<=b               a=a<<b

Unary Operator:- Two types of unary operator

increment   ++
decrement     --

increment\ decrement operators are also of two types

1)  pre:-   ++ variable name or  --variable name
  for this we first we increment  or decrement the value than use it
2)post :-   variablename ++   or variable name --
fir this first we use the value then change it .the changes will reflect in next usage of variables.

Ex:
int i=0;
i++;
printf("%d",i);
i--;
printf("%d",i);

output:- 1  0

Preferences of Operators.

higher preference :- * \  %
lower preferences:-  + - 





No comments :

Post a Comment