Thursday, November 6, 2008

arithmatic operations in c

Arithmetic Operations
As well as the standard arithmetic operators (+ - * /) found in most languages, C provides some more operators. There are some notable differences with other languages, such as Pascal.

Assignment is = i.e. i = 4; ch = `y';

Increment ++, Decrement -- which are more efficient than their long hand equivalents, for example:-- x++ is faster than x=x+1.

The ++ and -- operators can be either in post-fixed or pre-fixed. With pre-fixed the value is computed before the expression is evaluated whereas with post-fixed the value is computed after the expression is evaluated.

In the example below, ++z is pre-fixed and the w-- is post-fixed:


int x,y,w;

main()
{

x=((++z)-(w--)) % 100;

}

This would be equivalent to:



int x,y,w;

main()
{

z++;
x=(z-w) % 100;
w--;

}

The % (modulus) operator only works with integers.


Division / is for both integer and float division. So be careful.


The answer to: x = 3 / 2 is 1 even if x is declared a float!!


RULE: If both arguments of / are integer then do integer division.


So make sure you do this. The correct (for division) answer to the above is x = 3.0 / 2 or x= 3 / 2.0 or (better) x = 3.0 / 2.0.


There is also a convenient shorthand way to express computations in C.


It is very common to have expressions like: i = i + 3 or x = x*(y + 2)


This can written in C (generally) in a shorthand form like this:




which is equivalent to (but more efficient than):




So we can rewrite i = i + 3 as i += 3


and x = x*(y + 2) as x *= y + 2.


NOTE: that x *= y + 2 means x = x*(y + 2) and NOT x = x*y + 2.


Comparison Operators
To test for equality is ==


A warning: Beware of using ``='' instead of ``=='', such as writing accidentally


if ( i = j ) .....


This is a perfectly LEGAL C statement (syntactically speaking) which copies the value in "j" into "i", and delivers this value, which will then be interpreted as TRUE if j is non-zero. This is called assignment by value -- a key feature of C.


Not equals is: !=


Other operators < (less than) , > (grater than), <= (less than or equals), >= (greater than or equals) are as usual.



Logical Operators
Logical operators are usually used with conditional statements which we shall meet in the next Chapter.


The two basic logical operators are:


&& for logical AND, || for logical OR.


Beware & and | have a different meaning for bitwise AND and OR ( more on this later in Chapter 12).


Order of Precedence
It is necessary to be careful of the meaning of such expressions as a + b * c


We may want the effect as either


(a + b) * c


or


a + (b * c)
All operators have a priority, and high priority operators are evaluated before lower priority ones. Operators of the same priority are evaluated from left to right, so that


a - b - c

is evaluated as


( a - b ) - c


as you would expect.


From high priority to low priority the order for all C operators (we have not met all of them yet) is:



( ) [ ] -> .
! - * & sizeof cast ++ -
(these are right->left)
* / %
+ -
< <= >= >
== !=
&
|
&&
||
?: (right->left)
= += -= (right->left)
, (comma)

Thus

a < 10 && 2 * b < c


is interpreted as
( a < 10 ) && ( ( 2 * b ) < c )

and


a =

b =
spokes / spokes_per_wheel
+ spares;

as


a =

( b =
( spokes / spokes_per_wheel )
+ spares
);

0 Comments: