C - Operators
Operators :-
An operator is a symbol which represents a
particular operation that can be performed on some data.
Types of Operator:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Shorthand assignment
operators
6. Conditional
operators
7. Increment &
Decrement operators
8. Bitwise operator
9. Special operator
C provides all the basic arithmetic operators.
Operator |
Meaning |
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus Operator |
Relational operators
are used to compare two operands to check whether they are equal, unequal or
one is greater than or less than the other.
Operator |
Meaning |
> |
Greater than |
>= |
Greater than or equals to |
< |
Less than |
<= |
Less than or equals to |
== |
Equality test. |
!= |
Non-equality test. |
Relational operators
are used to compare two operands to check whether they are equal, unequal or
one is greater than or less than the other.
Operator |
Meaning |
&& |
Logical AND. Returns 1 if both the expressions are non-zero. |
|| |
Logical OR. Returns 1 if either of the expression is non-zero. |
! |
Unary negation. It converts a non-zero operand into 0 and a zero operand into 1. |
Assignment operators are used to assign the result of an expression to a
variable. We use the assignment operator “=". In addition, C has a set of
'shorthand' assignment operators.
Statement with
simple assignment operator Statement with shorthand operator
Operator |
Meaning |
+ = |
Addition |
- = |
Subtraction |
* = |
Multiplication |
/ = |
Division |
% = |
Modulus Operator |
A ternary operator
"? : " is available in C to
construct conditional expression of the form exp1 ? exp2 : exp3; where exp1, exp2 and exp3 are expressions.
a=10;
b=15;
x=(a>b) ? a : b;
C has two very
useful operators not generally found in other languages. These are the
increment and decrement operators: ++ and --.
Operator |
Meaning |
++ |
Increment |
-- |
Decrement |
8.
Bitwise Operators:
These operators are
used for manipulating data at bit level. They are used for testing bits or
shifting them right or left.
Operator |
Meaning |
& |
Bitwise AND |
| |
Bitwise or |
^ |
Bitwise exclusive or |
<< |
shift left |
>> |
shift right |
Operator |
Description |
Example |
sizeof |
returns the size (length in bytes) of entity, for eg. a variable
or an array, etc. |
sizeof(x) will return size of the variable x |
& |
returns the memory address of the variable |
&x will return address of the variable x |
* |
represents pointer to an object. The * operator returns the
value stored at a memory address. |
m = &x (memory address of variable x) *m will return the value stored at memory address m |
. (dot)
operator |
used to access individual elements of a C structure or C
union. |
If emp is a structure with an element int age in it,
then emp.age will return the value of age. |
-> (arrow)
operator |
used to access structure or union elements using a pointer to structure
or union. |
If p is a pointer to the emp structure, then we can
access age element using p->age |
[] operator |
used to access array elements using indexing |
if arr is an array, then we can access its values
using arr[index], where index represents the array index starting from
zero |
comma
(,) operator: |
This
operator can be used to link the related expressions together. |
res=(a=5, b=10, a+b); It first assigns the value 5 to a, then assigns 10 to b, and finally assigns 15 to res. |
The precedence is
used to determine how an expression involving more than one operator is
evaluated. The operators at the higher level of precedence are evaluated first.
The operators of the same precedence are evaluated either from left to right or
from right to left, depending on the level. There are distinct levels of
precedence and an operator may belong to one of the levels. This is known as
also associativity property of an operator. There is list of some operators,
their precedence levels and their rules of association.
Precedence |
Operator |
Description |
Associativity |
1 |
() |
Parentheses (function call) |
left-to-right |
[] |
Brackets (array subscript) |
left-to-right |
|
. |
Member selection via object name |
left-to-right |
|
-> |
Member selection via a pointer |
left-to-right |
|
a++/a– |
Postfix increment/decrement (a is a variable) |
left-to-right |
|
2 |
++a/–a |
Prefix increment/decrement (a is a variable) |
right-to-left |
+/- |
Unary plus/minus |
right-to-left |
|
!~ |
Logical negation/bitwise complement |
right-to-left |
|
(type) |
Cast (convert value to temporary value of type) |
right-to-left |
|
* |
Dereference |
right-to-left |
|
& |
Address (of operand) |
right-to-left |
|
sizeof |
Determine size in bytes on this implementation |
right-to-left |
|
3 |
*,/,% |
Multiplication/division/modulus |
left-to-right |
4 |
+/- |
Addition/subtraction |
left-to-right |
5 |
<< , >> |
Bitwise shift left, Bitwise shift right |
left-to-right |
6 |
< , <= |
Relational less than/less than or equal to |
left-to-right |
> , >= |
Relational greater than/greater than or equal to |
left-to-right |
|
7 |
== , != |
Relational is equal to/is not equal to |
left-to-right |
8 |
& |
Bitwise AND |
left-to-right |
9 |
^ |
Bitwise exclusive OR |
left-to-right |
10 |
| |
Bitwise inclusive OR |
left-to-right |
11 |
&& |
Logical AND |
left-to-right |
12 |
|| |
Logical OR |
left-to-right |
13 |
?: |
Ternary conditional |
right-to-left |
14 |
= |
Assignment |
right-to-left |
+= , -= |
Addition/subtraction assignment |
right-to-left |
|
*= , /= |
Multiplication/division assignment |
right-to-left |
|
%= , &= |
Modulus/bitwise AND assignment |
right-to-left |
|
^= , |= |
Bitwise exclusive/inclusive OR assignment |
right-to-left |
|
<>= |
Bitwise shift left/right assignment |
right-to-left |
|
15 |
, |
expression separator |
left-to-right |
Comments
Post a Comment