Arithmetic instruction is used to manipulate data using
operators.
Here is the list of operators.
Operator
|
Description
|
Associativity
|
()
[] . -> |
Parentheses
(grouping)
Brackets (array subscript) Member selection via object name Member selection via pointer |
left-to-right
|
++ --
+ - ! ~ (type) * & sizeof |
Prefix,Postfix
increment/decrement
Unary plus/minus Logical negation/bitwise complement Cast (change type) Dereference Address Determine size in bytes |
right-to-left
|
* / %
|
Multiplication/division/modulus
|
left-to-right
|
+ -
|
Addition/subtraction
|
left-to-right
|
<< >>
|
Bitwise shift left,
Bitwise shift right
|
left-to-right
|
< <=
> >= |
Relational less
than/less than or equal to
Relational greater than/greater than or equal to |
left-to-right
|
== !=
|
Relational is equal
to/is not equal to
|
left-to-right
|
&
|
Bitwise AND
|
left-to-right
|
^
|
Bitwise exclusive
OR
|
left-to-right
|
|
|
Bitwise inclusive
OR
|
left-to-right
|
&&
|
Logical AND
|
left-to-right
|
||
|
Logical OR
|
left-to-right
|
?:
|
Ternary conditional
|
right-to-left
|
=
+= -= *= /= %= &= ^= |= <<= >>= |
Assignment
Addition/subtraction assignment Multiplication/division assignment Modulus/bitwise AND assignment Bitwise exclusive/inclusive OR assignment Bitwise shift left/right assignment |
right-to-left
|
,
|
Comma (separate
expressions)
|
left-to-right
|
To understand operators in a better way we put them in 8
groups. This also helps you in learning precedence rule of operators. Each
group contains operators of same nature. So it would be better if you learn the
group names in the same order as listed below.
l Unary
Operators
l Arithmetic
Operators
l Bitwise
Operators
l Relational
Operators
l Logical
Operators
l Conditional
Operators
lAssignment
Operators
lMiscellaneous
Operators
Unary Operators (+,
-, ++, --, sizeof() )
Operators required operands to perform its operation. Unary
operators are those who required with only single operand to perform its task.
Unary + and –
These operators are not addition and subtraction, rather
they are unary + and – that shows direction of data. For example -3, +4, -345
etc
Increment Operator ++
Increment operator (++) increase value of variable by one.
Let us assume we have an int variable x containing 4.
int x=4;
//declaration of variable x and assign 4 to it.
x++; //Value of
variable x is incremented by one and become 5
++x; // Value of
variable x is incremented by one and become 6
Here notice that we have two conventions to use increment
operator. You can put operator before operand or after operand. If it is after operand
known as post increment, otherwise known as pre increment. Both these
convention have same effect over operand x as they increase its value by 1. But
these two conventions differ in priority rule. Pre increment has the highest
priority among all the operators. Post increment has the lowest priority among
all the operators.
sizeof() operator
sizeof() operator is used to know size of data type,
variable or constant.
Example :
main()
{
int x,y;
float k;
char ch;
double d;
x=sizeof(float); // sizeof returns 4 and assigned to x
x=sizeof(char); // sizeof returns 1 and assigned to x
x=sizeof(int); // sizeof returns 2 and assigned to x
x=sizeof(double); // sizeof returns 8 and assigned to x
x=sizeof(d); // sizeof returns 8 and assigned to x
x=sizeof(k); // sizeof returns 4 and assigned to x
x=sizeof(ch); // sizeof returns 1 and assigned to x
x=sizeof(y); // sizeof returns 2 and assigned to x
x=sizeof(45); // sizeof returns 2 and assigned to x
x=sizeof(23.67); // sizeof returns 8 and assigned to x
x=sizeof(‘a’); // sizeof returns 2 and assigned to x
}
Always remember that the size of real constant is 8 bytes
and character constant is 2 bytes.
Character constants are internally treated as integers. Each
character constant has a unique integer code called ASCII code. ASCII stands
for American Standard Code for Information Interchange.
Associativity rule for unary operators is right to left.
Which means when more than two unary operators of same priority appears in a
single arithmetic expression, they will be solved from right to left.
Arithmetic Operators
(+,-,*,/,%)
+,- and * operators are same as they are in mathematics. +
is used to add two numbers, - is used to subtract two numbers and * is used to
multiply two numbers.
Divide operator ( / )
main()
{
int x;
x=5/2;
printf(“%d”,x);
}
Output is:
2
Compiler understands numbers in two categories one is
integer and second is real. We have used both the operands integer in above
program. When two integers operate, result is always an integer. So 5/2 yield 2
and not 2.5
To get result 2.5 we need to make at least one of the
operand real.
main()
{
int x;
x=5/2.0;
printf(“%d”,x);
}
Output is :
2
Here we made one operand real but still result is 2. It
happens because variable x is of type int and could not hold real constant. So
choose float variable.
main()
{
float x;
x=5/2.0;
printf(“%d”,x);
}
Output is:
2
Oh man! Again the output is 2. Although we made one of the
operand real and variable is also of type float, but notice the format
specifier used in printf to display content of x, which is wrong. You have to
use %f for float variable.
main()
{
float x;
x=5/2.0;
printf(“%f”,x);
}
Output is:
2.500000
Modulo operator (%)
Modulo operator gives remainder in result. It can not work
for real constants.
main()
{
int x;
x=5%2;
printf(“%d”,x);
}
Output is:
1
When 5 is divided by 2 remainder comes 1.
Note: Modulo operator is good to check divisibility of a
number. When remainder is 0, number is divisible.
Precedence of *, /
and % is same but higher than + and -.
Precedence of + and – is same.
Associativity rule for arithmetic operators is left to
right.
Bitwise operator (
&,|,^,~,>>,<<)
Bitwise operators work on binary numbers that is bits. You
should know how to convert a number from decimal to binary and from binary to
decimal.
Nature of operators
Bitwise AND (&)
0&0 is 0
0&1 is 0
1&0 is 0
1&1 is 1
Bitwise OR ( | )
0|0 is 0
0|1 is 1
1|0 is 1
1|1 is 1
Bitwise XOR (^)
0^0 is 0
0^1 is 1
1^0 is 1
1^1 is 0
Bitwise NOT (~)
~0 is 1
~1 is 0
main()
{
int x;
x=5&12;
printf(“%d”,x);
}
Output is:
4
Bitwise AND applies on 5 and 12. We need to convert them in
binary.
5 =00000000 00000101
12=00000000 00001100
& -------------------------
4=00000000 00000100
Shift Operators
Right shift >>
Left shift <<
main()
{
int x;
x=12>>2;
printf(“%d”,x);
}
Output is:
3
Convert 12 into binary and shift bits to their right 2
times. This makes last two bits out and two new bits (always 0) append at the
left.
12=00000000 00001100
Right shift two times
3 =00000000 00000011
main()
{
int x;
x=12<<2;
printf(“%d”,x);
}
Output is:
48
Convert 12 into binary and shift bits to their left 2 times.
This makes last two left most bits out and two new bits (always 0) append at
the right.
12=00000000 00001100
Left shift two times
48=00000000 00110000
Relational Operators
(<, >, <=, >=, ==. !=)
Relational operators are always evaluated as true or false.
True is 1 and false is 0.
main()
{
int x;
x=5<3;
printf(“%d”,x);
}
Output is:
0
As the relation 5<3 is false, x will contain 0 as a
result of relational operator.
Priority of <, >, <=, >= is higher than
== and !=
Associativity is left to right.
Logical Operators (
&&, ||, ! )
Logical operators (&& and ||) are used to combined
two expressions.
They work as:
C1 && C2 Result
T && T T
T && F
F
F && X
F
C1 || C2
Result
F || F
F
F || T
T
T || X T
Example
main()
{
int x;
x=
5>3&&4<0;
printf(“%d”,x);
}
Output is:
0
Here, two conditions 5>3 and 4<0 are combined to form
a single condition using && operator as 5>3&&4<0.
Condition one is evaluated as TRUE as 5 is greater than 3. Since condition one
is TRUE condition two is tested and evaluated as FALSE as 4 is not less than 0.
According to the above chart, T&&F is treated as FALSE thus 0 is stored
in x.
Logical NOT ( ! )
! operator is used to invert the truth value.
main()
{
int x;
x=!(5>4);
printf(“%d”,x);
}
Output is:
0
5>4 is evaluated as TRUE, NOT (!) operator invert this
result and make it FALSE.
Thus 0 is stored in x.
Conditional Operator
( ? : )
Conditional operator is also known as ternary operator. We
will discuss it in our next chapter control instructions.
Assignment operators
(=, +=, -=, *=, /=, %=)
Assignment operators are used to assign values to variable
main()
{
int x;
x=4;
}
In the above code we used assignment operator to store 4 in
x.
main()
{
int x=5;
x+=4; //same as
x=x+4
}
Here, += is also an assignment operator, but used to store
data after small manipulation. First 4 is added to the content of x and then
assigned to x.
The same operation can be performed using = and + operator
as x=x+4, but this expression contains two operators which requires precedence
rule during evaluation.
Although, generated results are same that is x is containing
9.
main()
{
int x=3, y=3;
x*=4+3;
y=y*4+3;
}
Now you can understand difference between the two
expressions. First x*=4+3, + is operated first as it has higher priority then
+=. So 4+3 becomes 7. x is then added to 7 to give result 10. This 10 is
getting stored in x.
Second expression y=y*4+3 which is thought as the substitute
of first expression but it does not work in similar fashion. * has the highest
priority then + and then = comes at the last. Content of y is multiplied by 4
which give 12. 12 is added to 3 which becomes 15 and get stored in y.
Other operators -=, /= and %= are works in a similar
fashion.
No comments:
Post a Comment