Tuesday 20 May 2014

OPERATORS IN C

Operators are the symbol which operates on value or a variable. For example: + is a operator to perform addition.
                      C language supports a rich set of built-in operators. An operator is a symbol that tells the compiler to perform certain mathematical or logical manipulations. Operators are used in program to manipulate data and variables. These operators can be classified as:

Operators in C programming
Arithmetic Operators
Increment and Decrement Operators
Assignment Operators
Relational Operators
Logical Operators
Conditional Operators
Bitwise Operators
Special Operators

Arithmetic operators:

C supports all the basic arithmetic operators. The following table show s all the basic arithmetic operators.

Example of working of arithmetic operators:

#include <stdio.h>
int main(){
    int a=9,b=4,c;
    c=a+b;
    printf("a+b=%d\n",c);
    c=a-b;
    printf("a-b=%d\n",c);
    c=a*b;
    printf("a*b=%d\n",c);
    c=a/b;
    printf("a/b=%d\n",c);
    c=a%b;
    printf("Remainder when a divided by b=%d\n",c);
    return 0;
}
Output of this code :

a+b=13
a-b=5
a*b=36
a/b=2
Remainder when a divided by b=1 
Here, the operators +, - and * performed normally as you expected. In normal calculation, 9/4 equals to 2.25. But, the output is 2 in this program. It is because, a and b are both integers. So, the output is also integer and the compiler neglects the term after decimal point and shows answer 2 instead of 2.25. And, finally a%b is 1,i.e. ,when a=9 is divided by b=4, remainder is 1.

Note: % operator can only be used with integers.

In C, ++ and -- are called increment and decrement operators respectively. Both of these operators are unary operators, i.e, used on single operand. ++ adds 1 to operand and -- subtracts 1 to operand respectively. For example:
Let a=5 and b=10
a++;  //a becomes 6
a--;  //a becomes 5
++a;  //a becomes 6
--a;  //a becomes 5 

Difference between ++ and -- operator as postfix and prefix:

When i++ is used as prefix(like: ++var), ++var will increment the value of var and then return it but, if ++ is used as postfix(like: var++), operator will return the value of operand first and then only increment it. This can be demonstrated by an example:

#include <stdio.h>
int main(){
    int c=2,d=2;
    printf("%d\n",c++); /*this statement displays 2;after then, only c incremented by 1 to 3.*/
    printf("%d",++c);   /*this statement increments 1 to c 
                         after then, only c is displayed.*/ 
    return 0;
Output: 2, 4.  

Assignment Operators

The most common assignment operator is =. This operator assigns the value in right side to the left side. For example:
var=5  //5 is assigned to var
a=c;   //value of c is assigned to a
5=c;   // Error! 5 is a constant.
 

Relational Operators

Relational operators checks relationship between two operands. If the relation is true, it returns value 1 and if the relation is false, it returns value 0. For example:
a>b
Here, > is a relational operator. If a is greater than b, a>b returns 1 if not then, it returns 0.
Relational operators are used in decision making and loops in C programming.

  
Logical Operators

Logical operators are used to combine expressions containing relation operators. In C, there are 3 logical operators:


Ex: 1. If c=5 and d=2 then,((c==5) && (d>5)) returns false.Becoz here (d>5) is false in the given example .So,one statement c==5 is true and another d>5 is false then whole expression is false.
2. If c=5 and d=2 then, ((c==5) || (d>5)) returns true.
3.If c=5 then, !(c==5) returns false.

Conditional Operators

Conditional operator takes three operands and consists of two symbols ? and :  It is also called ternary operator Conditional operators are used for decision making in C. For example:
expr1 ? expr2 : expr3
c=(c>0)?10:-10;
if expr1 condition is true then the value of expr2 otherwise the value of expr3.If c is greater than 0, value of c will be 10 but, if c is less than 0, value of c will be -10.

Bitwise Operators

A bitwise operator works on each bit of data. Bitwise operators are used in bit level programming.


Now lets see truth table for bitwise  &,  | and  ^



Bitwise operator is advance topic in programming . Later we learn more about bitwise operator in C programming.

Special Operators


Later we learn these operator in Pointer Chapter....

0 comments :

Post a Comment