C supports a rich set of built-in operators. We have already used
several of them, such as =, +, -, *, & and <. An operator is a symbol
that tells the computer to perform certain mathematical or logical manipulations.
Operators are used in programs to manipulate data and variables. They usually
form a part of the mathematical or
logical expressions.
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.
Bitwise operators
8.
Special operators
C provides all the basic arithmetic operators.
Operator
|
Meaning
|
Example
|
+
|
Addition
|
a + b
|
-
|
Subtraction
|
a - b
|
*
|
multiplication
|
a * b
|
/
|
Division
|
a / b
|
%
|
Module Division
|
a % b
|
C supports six
relational operators. These operators used for
comparisons of variables and values.
comparisons of variables and values.
Operator
|
Meaning
|
Example
|
<
|
is less than
|
a < b
|
<=
|
is less than or equal to
|
a <= b
|
>
|
is greater than
|
a > b
|
<=
|
is greater than or equal to
|
a >= b
|
==
|
is equal to
|
a == b
|
!=
|
is not equal to
|
a != b
|
C provided three
logical operators.
Operator
|
Meaning
|
Example
|
&&
|
Logical AND
|
a > 50 && b < 40
|
||
|
Logical OR
|
a > 50 || b < 40
|
!
|
Logical NOT
|
a != 50
|
The logical operators && and || are used when we want to test more than one condition and make
decisions.
Assignment
operators are used to assign the result of an expression to a variable.
Operator
|
Meaning
|
Example
|
+=
|
a = a + 1
|
a += 1
|
-=
|
a = a - 1
|
a -= 1
|
*=
|
a = a*(n+1)
|
a *= n+1
|
/=
|
a=a/(n+1)
|
a /= n+1
|
%=
|
a=a % b
|
a %= b
|
C allows two very useful operators not
generally found in other languages. These are the increment and decrement
operators.
Operator
|
Meaning
|
Example
|
++
|
Increment
|
a ++ or ++a
|
--
|
Decrement
|
a -- or --a
|
Rules for ++ and - - operators:
Ø
Increment and decrement
operators are unary operators and they require variable as their operands.
Ø
When postfix ++ (or --) is used with a variable in
an expression, the expression is first using the original value of the variable
and then the variable is incremented (or
decremented) by one.
Ø
When prefix ++ (or --) is used in an expression,
the variable is incremented (or
decremented) first and then the expression is evaluated using the new value
of the variable.
Ø
The precedence and
associatively of ++ and -- operators are the same as those of unary + and unary -.
A ternary operator pair “?:” is available in
C to construct conditional expressions of the form. It takes three arguments.
exp1 ? exp2 : exp3
|
Where
exp1, exp2 and exp3 are expressions.
The operator ? : works as
follows: exp1 is evaluated first. If it is nonzero (true), then the expression
exp2 is evaluated and becomes the value of the expression. If exp1 is false,
exp3 is evaluated and its value becomes the value of the expression.
Example:- int x, y;
scanf
("%d", &x);
y = (x > 5 ? 3:4);
This
statement will store 3 in y if x is greater than 5, otherwise it will
store 4 in y.
C has a distinction of supporting special
operators known as bitwise operators for manipulation of data at bit level.
These operators are used for testing the bits, or shifting them right or left.
Bitwise operators may not be applied to float
or double.
Operator
|
Meaning
|
&
|
Bitwise AND
|
|
|
Bitwise OR
|
^
|
Bitwise exclusive OR
|
<<
|
Shift left
|
>>
|
Shift right
|
C supports some
special operators of interest such as comma operator, sizeof operator, pointer operators (& and *) and member
selection operators (. and ->).
Unary
operators are operators that only take one argument. For example: sizeof( ) operator etc.
The comma operator can be used to link the related expressions
together. Comma lists of expressions are evaluated left to right and the value
of right most expression value of the combined expression.
Example:- value = (x=10, y=5, x+y);
First assign the
value 10 to x, then assign 5 to y, and finally assign 15 (10 + 5) to Value.
Since comma operator
has the lowest precedence of all operators, the parentheses are necessary.
Some example of comma
operator is:
for (n=1, m=10, n<=m; n++, m++)
Exchanging values:
t
= x, x = y, y = t;
The sizeof operator is a compile time operator and when used with an
operand, it returns the number of bytes the operand occupies. The operand may
be a variable, a constant or a data type qualifier.
Example:- m = sizeof (a);
n = sizeof (long int);
k = sizeof (20L);
The sizeof operator is normally used to determine the lengths of arrays
and structures when their sizes are not known to the programmer. It is also
used to allocate memory space dynamically to variables during execution of a program.
0 comments:
Post a Comment