Operators can be classified as:
Arithmetic operator may be
| 
Unary Operator | 
Binary Operator | 
| 
 It is used
  with one value or variable | 
It is used with two variables | 
| 
Eg. -5, +a | 
Eg. a+b, 5-6 | 
| 
Unary operator are+ , - | 
Binary operator are+, -, *, /  and % | 
| 
Operator | 
Meaning of
   Operator | 
| 
+ | 
addition or unary plus | 
| 
- | 
subtraction or  unary minus | 
| 
* | 
Multiplication | 
| 
/ | 
Division | 
| 
% | 
remainder after division( modulo division) | 
Example
of working of arithmetic operators
/* Program to demonstrate the working of arithmetic operators in C.*/
#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);
return0;
}
}
 
output:
a+b=13
a-b=5
a*b=36
a/b=2
a%b=1
2)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.
| 
Operator | 
Meaning of
   Operator | 
Example | 
| 
== | 
Equal to | 
5==3 returns
   false (0) | 
| 
>  | 
Greater than | 
5>3 returns
   true (1) | 
| 
<  | 
Less than | 
5<3 returns
   false (0) | 
| 
!= | 
Not equal to | 
5!=3 returns
   true(1) | 
| 
>= | 
Greater than or
   equal to | 
5>=3 returns
   true (1) | 
| 
<= | 
Less than or
   equal to | 
5<=3 return
   false (0) | 
3)Logical Operators: Logical operators are used to combine expressions
containing relation operators. In C, there are 3 logical operators:
| 
Operator | 
Meaning of Operator | 
Example | 
Explanation | 
| 
&& | 
Logical AND  | 
((x>5) && (y<0)) | 
Returns true if x greater than 5 and y less than 0. | 
| 
|| | 
Logical OR | 
((x>5)  ||
  (y<0)) | 
Returns true if x greater than 5 or y less than 0. | 
| 
! | 
Logical NOT | 
X !=0 | 
Returns true if x 
  is not equal to 0. | 
4)Bitewise
Operators: A bitwise operator works on each bit of data.
Bitwise operators are used in bit level programming.
| 
Operators | 
Meaning of operators | 
| 
& | 
Bitwise AND | 
| 
| | 
Bitwise OR | 
| 
^ | 
Bitwise exclusive OR | 
| 
~ | 
Bitwise complement | 
| 
<<  | 
Shift left | 
| 
>>  | 
Shift right | 
=. 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 | 
Shorthand | 
Assignment | 
| 
+= | 
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 | 
 
6) Shorthand Operators: The mathematical operation (+, -, *, /, and %) merge with assignment operator (=) is called shorthand operators. The mathematical operators come before assignment operator at the expression.
 
7) Comma
Operator: Comma operators are used to link expressions together. Comma
operator is represented by “,”. This operator is used to evaluate left to right
hand side. For example
x = (2, 4, 6,121);The value of x becomes 121 by replacing all previous values of x.
8) 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.
return0;
}
 
Output
2
4
 
In the pre-increment, value is first incremented and then used inside the expression
 
               b=++y
If the value of y is 5 then the value of b is 6 because the values of y get modified before using it in the expression.
 
 
In the post-increment, value is first used in the expression then incremented.
               b = y++
If the value of y is 5 then the value of variable b is also 5 because old value is used.
 
In the pre-decrement value is first decremented and then used inside the expression
 
b = - - var
 
In the post-decrement old value is first used in a expression and then old value will be decrement by 1.
 
b = var - -;
 
 
Example:
#include <stdio.h>
#include<conio.h>
  main( )
  {
int a, b, x = 10, y =10; 
a=x --;
b=-- y;
 
printf("value of a : %d", a);
printf("\nvalue of b : %d", b);
getch( );
}
output :
 
value of a : 10
value of  b : 9
 
9)Ternary Operator (? :)The two token ? and : are used to represent ternary operator.
The syntax of ternary operator is 
(condition)?(statement if condition is true):(statement if condition is false)
Example
(a % 2==0)? (printf (“even number”)): (printf(“odd number”));
 
Expression: An expression is a combination of variables, constants and operators written according to the syntax of C language. The expression without semicolon is called an expression.
If we place semicolon at the last of the expression convert into a statement.
 
| Algebraic Expression | C Expression  | Name of Expression | 
| ab-c | a*b-c | Arithmetic | 
| a=b&c | a=b&c | Logical | 
| i=ptr/100 | i=p*t*r/100 | Arithmetic | 
 
Type Casting
It is the way to convert a variable from one data type to another data type. In general, fundamental data types can be converted.
There are 2 types of type casting: 
1) Implicit type casting: This is performed by the compiler automatically. The conversion of data is performed either compilation or run time.
               
/*Example of implicit type casting*/
#include <stdio.h>
#include<conio.h>
main ( ) 
{
               int p,t,r,i;
               printf(“\n Enter principal, time and rate:”);
               scanf(“%d%d%d”,&p,&t,&r);
               i=p*t*r/100.00; // type casting
               printf(%d”,i); 
getch( );
}
 
Output: Enter principal, time and rate: 2000 1 1
20
 
2) Explicit Type casting: It is explicitly defined within a program (instead of being done by the compiler for implicit type conversion).
 
Example:
#include<stdio.h>
#include <conio.h>
               main( ) {
               float x =  2.345;
               int i;
               i= int (x);
printf(“from float x = % f  i=%d “, x, i);
getch ( );
} 
 
Output: from float x = 2.345000 i=2 
 
Example 2: 
               double da = 3.3;
               double da = 3.3;
               double da = 3.4;
int result = (int)da + (int)db +(int)dc ; // result == 9
                               
 
No comments:
Post a Comment