Pages

Tuesday 28 June 2011

C Operators



In a C program, data items (variables and constants) can be arithmetically manipulated using operators. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation on data stored in variables. The variables that are operated are termed as operands.
C operators can be classified into a number of categories. They include:
1.                                   Arithmetic operators
2.                               Relational operators
3.        Logical operators
4.                                   Assignment operators
5.                                 Increment and decrement operators
6.                             Conditional operators
7.                                bit-wise operators
8.                                   Special operators

Arithmetic Operators
Arithmetic operators work on numeric type of operands. C provides all the basic arithmetic operators.
There are five arithmetic operators in C.
___________ Operator____________ Purpose___________________
+                                    Addition
-                                    Subtraction
*                                    Multiplication
/                                     Division
                 %                                   Remainder after integer division
The division operator (/) requires the second operand to be non-zero, though the operands need not be integers.   When an integer is divided by another integer, the ,Result is also an integer. In such instances the division is termed as integer division. Consider the following:

int ;x;
x= 10;

What do you expect the value of x/4 to be? If you guessed 2.5, you are wrong. The result is of course 2.5 however, since it is integer division, 2.5 will be truncated to 2. In case you wish to get the correct value you make this division a float type as x/4.0.
The % operator is known as modulus operator. It produces the remainder after the division of two operands. The second operand must be non-zero.
Rest all other operators work in their normal arithmetic way. Normal BODMAS rules are also applicable to these arithmetic operators.
Relational operator
Relational operator is used to compare two operands to see whether they are equal to each other, unequal, or one is greater or lesser than the other.
While the operands can be variables, constants or expressions, the result is always a numeric value equivalent to either true or false. As mentioned earlier a non-zero result indicates true and zero indicates false. C language provides six relational operators.
= =                                      equal to
! =                                       not equal to
<                                         less than
<=                                       less than or equal to
>                                         greater than
>                                      greater than or equal to
A simple relation contains only one relational expression and takes the following form:
<ae-l> <relational operator> <ae-2>                  
<ae-l> and <ae-2>' are arithmetic expressions, which may be constants, variables or combination of these. The value of the relational operator is either 1 or 0. If the relation is true, result is 1 otherwise it is 0.
e.g.:             Expressions                 Result
    6.3 < = 15                        True
2.5 < -2                          False
-10> =0                          False
                               10 <8+3                        True  
Logical Operators                                                                             
More than one relational expression can be combined to form a single compound relational expression using logical operators. Logical operators are used to combine two or more relational expressions. C provides three logical operators. A compound relation behaves identically producing either true or false value.
Operator           Meaning               Result
&&                  Logical AND          True if both the operands are true
Logical OR            True if both the operands are true
!                      Logical NOT          True if the operand is false and vice versa
e.g.:      1.   (age > 50 && weight < 80) will evaluate to true if age is more than 50 and also weight is less than 80. Otherwise it will evaluate to false.
2.             ( a < 0   ch = = 'a') will evaluate to true if a is less than 0 while ch is equal to 'a', false otherwise.
3.             (.! (a < 0)) will evaluate to true if a is greater than or equal to 0, false otherwise.
Assignment Operators
Assignment operators are used to store the result of an expression to a variable. The most commonly used assignment operator is (=). Be careful not to mistake assignment operator (=) for mathematical equality operator which is indicated by the same symbol.
An expression with assignment operator is of the following form:
 <identifier>  =   <expression>;
When this statement is executed, <expression> is evaluated and the value is stored in the <identifier>. Note that data type of both the sides should be either the same or compatible to each other.
Let us consider the following program that illustrates usage of assignment operator in C language.
#include <stdio.h>
main(   )
{
int  i ;
i   =   5;
printf   ("%d",   i);
i  =  i   +   10;
printf   ("n%d",   i);
}
Output will be:     5
15
In this program the current value stored in variable i is 5. Thus, while executing i = i+10;, the right hand side will be evaluated to give a value 15. This value will then be assigned to the left hand side. As a result, the current value of I after execution of this statement will become 15.


Increment and Decrement Operators
C has two very useful operators ++ and — called increment and decrement operators respectively. These are generally not found in other languages. These operators are unary operators as they require only one operand. The operands must be a variable
name and not a constant.                                                                 
The increment operator ('++) adds one to the current value of the operand and stores the result back into the operand, while the decrement operator (—) subtracts one from the operand and stores the decremented value back into the operand.     
There are two different forms of increment and decrement operators. When they are used before the operand, it is termed as prefix, while when used after the operand, they are termed as postfix operators.
e.g.:    int i   = 5;
i ++;
++ i;
-- i;
-- i;

When used in an isolated C statement, both prefix and postfix operators have the same effect, but when they are used in expressions, each of them has a different effect.
In expressions, postfix operator uses the current value and then increments/decrements while in the prefix form the value is incremented/decremented first and then used in the expression. Consider the following examples:
e.g:   b = a   ++;
 this is postfix increment expression. This statement is equivalent to:
{b =  a;
a = a+1;}
e.g.       b  =   -   -   a;
this is prefix decrement expression. This statement is equivalent to:
{   a=   a-1; b =   a;   }
Consider the following C program that illustrates the usage of postfix and prefix increment operators.
#include<stdio. h>
main( ) {
int a = 10; b = 0;
a++ ;
printf ("\n a = %d", a);
b = ++a;
printf ("\n a = % d, b = % d",a, b) ;
b = a++;
printf ("\n a = % d, b 1 % d", a, b);
     }
output:   a =11
a = 12 b = 12 a = 13 b = 12



Bitwise Operators
You know that a numeric value is stored in a variable in binary form.Bitwise operators are used for manipulation of data at bit level. These operators are used for testing the bits, or shifting them right or left. Bitwise operators are applicable to integer data types only. A list of different bit wise operators available in C language and their corresponding meaning is presented below:
Some Bit wise Operators
__________ Operator__________ Meaning______________________
&                             Bitwise Logical AND
|                              Bitwise Logical OR
Bitwise Logical XOR
<<                            Left shift
> >                           Right shift
~                             One's complement
 (Bit-wise OR)                     :binary operator takes two operands of int
type and performs bit-wise OR operation. With assumption that int size is 8-bits:
int a = 5;       [binary : 0000 0101]
int b = 9;       [binary: 0000 1001]
a l b yields      [binary: 0000 1101]
& (Bit-wise AND)                  :binary operator takes two operands of int
type and performs bit-wise AND operation. With same assumption on int size as above:
int a = 5;       [binary : 0000 0101]
int b = 9;       [binary: 0000 1001]
a & b yields     [binary: 0000 0001 ]
 ^ (Bit-wise Logical XOR)       :XOR gives 1 if only one of the operand is 1
             else 0. With same assumption on int size as
above:
int a = 5;        [binary: 0000 0101]
int b = 9;        [binary 10000 1001 ]
a ^ b yields        [binary: 0000 1100]   
<<(Shift left)                         :This  operator  shifts  the bits  towards left
padding the space with 0 by given integer times.
int a = 5;        [binary: 0000 0101]
a «3 yields       [binary : 0010 1000]       
          
» (Shift right)                           :This operator shifts the bits towards right
padding the space with 0.
                                   int a = 5;         [binary : 0000 0101]
a » 3 yields       [binary: 0000 0000]
~ (one's complement operator)      :It is a uniary operator that causes the bits of
its operand to be inverted so that 1 becomes 0 and vice-veria. The opearator must always precede the operand and must be integer type of all sizes. Assuming that int type is of 1 byte size:
inr a = 5;        [binary: 0000 0101]
~a;                 [binary: 1111 1010]


c programmming online,basic of c language,structure of c programming,c language statements,Compiling a C Program,Pseudo Codes in c,basis data type in c ,variables in C Program,constants in C Program,c operators in C Program,special operators in C ,Decision Making,Branching & Looping in C Program,if statement in C Program,for loop in C Program

No comments:

Post a Comment