Friday, September 26, 2014

types of operators in "C"

 Describe the various types of operators in „C‟ language along with its priority.

An ex pr e s s I o n is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof.


1. ARITHMETIC OPERATORS:
The symbols of the arithmetic operators are:-
Example:
#include <stdio.h>
main()
{
int sum = 50; float modulus; modulus = sum % 10;
printf(“The %% of %d by 10 is %f\n”, sum, modulus);
}
PRE/POST INCREMENT/DECREMENT OPERATORS
PRE means do the operation first followed by any assignment operation. POST means do the operation after any assignment operation. Consider the following statements ++count; /* PRE Increment, means add one to count */ count++; /* P OST Increment, means add one to count */
Example:
#include <stdio.h>
main()
{
int count = 0, loop;
loop = ++count; /* same as count = count + 1; loop = count; */
printf(“loop = %d, count = %d\n”, loop, count);
loop = count++; /* same as loop = count; count = count + 1; */
printf(“loop = %d, count = %d\n”, loop, count);
}
If the operator precedes (is on the left hand side) of the variable, the operation is performed first, so the statement
loop = ++count;
really means increment count first, then assign the new value of count to loop.
2. THE RELATIONAL OPERATORS
These allow the comparison of two or more variables.
= = equal to
! = not equal
< less than
< = less than or equal to
> greater than
> = greater than or equal to
Example:
#include <stdio.h>
main() /* Program introduces the for statement, counts to ten */
{
int count;
for( count = 1; count <= 10; count = count + 1 )
printf(“%d “, count );
printf(“\n”);
}

3. LOGICAL OPERATORS (AND, NOT, OR, EOR) 
Combining more than one condition
These allow the testing of more than one condition as part of selection statements. The symbols are
LOGICAL AND &&
Logical and requires all conditions to evaluate as TRUE (non-zero).
LOGICAL OR ||
Logical or will be executed if any ONE of the conditions is TRUE (non-zero).
LOGICAL NOT !
logical not negates (changes from TRUE to FALSE, vsvs) a condition.
LOGICAL EOR ^
Logical eor will be excuted if either condition is TRUE, but NOT if they are all true.
The following program uses an if statement with logical AND to validate the users input to be in the range 1-10.
#include <stdio.h>
main()
{
int number;
int valid = 0;
while( valid == 0 ) {
printf(“Enter a number between 1 and 10 à”);
scanf(“%d”, &number);
if( (number < 1 ) || (number > 10) ){
printf(“Number is outside range 1-10. Please re-enter\n”);
valid = 0;
}
else
valid = 1;
}
printf(“The number is %d\n”, number );
} Example: NEGATION
#include <stdio.h>
main()
{
int flag = 0;
if( ! flag ) {
printf(“The flag is not set.\n”);
flag = ! flag;
}
printf(“The value of flag is %d\n”, flag);
}
Example:
Consider where a value is to be inputted from the user, and checked for validity to be within a certain range, lets say between the integer values 1 and 100.
#include <stdio.h>
main()
{
int number;
int valid = 0;
while( valid == 0 ) {
printf(“Enter a number between 1 and 100”);
scanf(“%d”, &number );
if( (number < 1) || (number > 100) ) printf(“Number is outside legal range\n”); else
valid = 1;
}
printf(“Number is %d\n”, number );
}
4. THE CONDITIONAL EXPRESSION OPERATOR or TERNARY OPERATOR
This conditional expression operator takes THREE operators. The two symbols used to denote this operator are the ? and the :. The first operand is placed before the ?, the second operand between the ? and the :, and the third after the :. The general format is,
condition ? expression1 : expression2.
If the result of condition is TRUE ( non-zero ), expression1 is evaluated and the
result of the evaluation becomes the result of the operation. If the condition is FALSE (zero), then expression2 is evaluated and its result becomes the result of the operation. An example will help,
s = ( x < 0 ) ? -1 : x * x; If x is less than zero then s = -1
If x is greater than zero then s = x * x
Example:
#include <stdio.h>
main()
{
int input;
printf(“I will tell you if the number is positive, negative or zero!”\n”);
printf(“please enter your number now-à”);
scanf(“%d”, &input );
(input < 0) ? printf(“negative\n”) : ((input > 0) ? printf(“positive\n”) :
printf(“zero\n”));
}
5. BIT OPERATIONS
C has the advantage of direct bit manipulation and the operations available are,
Example:
/* Example program illustrating << and >> */
#include <stdio.h>
main()
{
int n1 = 10, n2 = 20, I = 0;
I = n2 << 4; /* n2 shifted left four times */
printf(“%d\n”, i);
I = n1 >> 5; /* n1 shifted right five times */
printf(“%d\n”, i);
}
Example:
/* Example program using EOR operator */
#include <stdio.h>
main()
{
int value1 = 2, value2 = 4;
value1 ^= value2;
value2 ^= value1;
value1 ^= value2;
printf(“Value1 = %d, Value2 = %d\n”, value1, value2);
}
Example:
/* Example program using AND operator */
#include <stdio.h>
main()
{
int loop;
for( loop = „A‟; loop <= „Z‟; loop++ )
printf(“Loop = %c, AND 0xdf = %c\n”, loop, loop & 0xdf);
}

No comments:

Post a Comment