Tuesday, September 24, 2013

OPERATORS AND EXPRESSIONS


OPERATORS AND EXPRESSIONS

Operators are the symbol which operates on value or a variable.

For example: + is a operator to perform addition.

C programming language has wide range of operators to perform various operations.

For better understanding of operators, these operators can be classified as:


Arithmetic Operators
Increment and Decrement Operators
Assignment Operators
Relational Operators
Logical Operators
Conditional Operators
Bitwise Operators
Special Operators

Arithmetic Operators


+ addition or unary plus
- subtraction or  unary minus
* multiplication
/ division
% remainder after division( modulo division)



/* Program to demonstrate the working of arithmetic operators in C.  */

#include <stdio.h>
int main(){
    int a=6,b=2,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
a+b=8
a-b=4
a*b=24
a/b=3

Increment and decrement operators

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 then, only c incremented by 1 to 3.
    printf("%d",++c);   //this statement increments 1 to c 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.
Operator Example Same As
= a=b 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

Relational Operator

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.



== Equal to 7==3 returns false (0)
> Greater than 7>3 returns true (1)
< Less than 7<3 returns false (0)
!= Not equal to 7!=3 returns true(1)
>= Greater than or equal to 7>=3 returns true (1)
<= Less than or equal to 7<=3 return false (0)

Logical Operators

Logical operators are used to combine expressions containing relation operators.

In C, there are 3 logical operators:


&& Logial AND If c=5 and d=2 then,((c==5) && (d>5)) returns false.
|| Logical OR If c=5 and d=2 then, ((c==5) || (d>5)) returns true.
! Logical NOT If c=5 then, !(c==5) returns false..

Bitwise Operators

A bitwise operator works on each bit of data.

Bitwise operators are used in bit level programming.


& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right



Comma Operator

Comma operators are used to link related expressions together. For example:
int a,c=5,d;

The sizeof operator
It is a unary operator which is used in finding the size of data type, constant, arrays, structure etc. For example:

#include <stdio.h>
int main(){
    int a;
    float b;
    double c;
    char d;
    printf("Size of int=%d bytes\n",sizeof(a));
    printf("Size of float=%d bytes\n",sizeof(b));
    printf("Size of double=%d bytes\n",sizeof(c));
    printf("Size of char=%d byte\n",sizeof(d));
    return 0;
}
Output
Size of int=4 bytes
Size of float=4 bytes
Size of double=8 bytes
Size of char=1 byte

Conditional operators (?:)

Conditional operators are used in decision making in C programming, i.e, executes different statements according to test condition whether it is either true or false.

Syntax of conditional operators
conditional_expression?expression1:expression2

If the test condition is true, expression1 is returned and if false expression2 is returned.

Example of conditional operator
#include <stdio.h>
int main(){
   char feb;
   int days;
   printf("Enter l if the year is leap year otherwise enter 0: ");
   scanf("%c",&feb);
   days=(feb=='l')?29:28;
   /*If test condition (feb=='l') is true, days will be equal to 29. */
   /*If test condition (feb=='l') is false, days will be equal to 28. */
   printf("Number of days in February = %d",days);
   return 0;
}
Output
Enter l if the year is leap year otherwise enter n: l
Number of days in February = 29

No comments:

Post a Comment