Home arrow Virus Trojans Worms arrow Operators in C

Language Translator

Hacking Zone

Hacking Tools
Attacking

Configure Windows

Windows Configuration

Mix Tutorials

Asterisk
Website Building

Novels

Mix Novels

Human Personality

Body Language
Operators in C Print E-mail
Article Index
Operators in C
Page 2
Page 3

Operators in C



The aim of this chapter is to cover the full range of diverse operators available in C. Operators dealing with pointers, arrays and structures will be left to late

Arithmetic Operators C provides the expected mathematical operators. There are no nasty surprises.

+, -, *, / As might be expected, “+” and “-” may be used in a unary sense as follows:

x = +y;

or

x = -y;

The first is rather a waste of time and is exactly equivalent to “x = y” The second multiplies the value of “y” by -1 before assigning it to “x”. C provides a modulo, or “remainder after dividing by” operator. Thus 25/4 is 6, % (25%4) is 1. This calculation only really makes sense with integer numbers where there can be a remainder. When dividing floating point numbers there isn’t a remainder, just a fraction. Hence this operator cannot be applie to reaals.

 

 

Using Arithmetic Operators

One operator “+” must add integers together and add reals together. It might almost have been easier to provide two, then the programmer could carefully choose whenever addition was performed. But why stop with two versions? There are, after all, different kinds of integer and different kinds of real. Suddenly we can see the need for many different “+” variations. Then there are the numerous combinations of int and double, short and float etc. etc.

C gets around the problem of having many variations, by getting the “+” operator to choose itself what sort of addition to perform. If “+” sees an integer on its left and its right, integer addition is performed. With a real on the left and right, real addition is performed instead.

This is also true for the other operators, “-”, “*” and “/”. The compiler examines the types on either side of each operator and does whatever is appropriate. Note that this is literally true: the compiler is only concerned with the types of the operands. No account whatever is taken of the type being assigned to. Thus in the example above:

h = i / j;

It is the types of “i” and “j” (int) cause integer division to be performed. The fact that the result is being assigned to “h”, a double, has no influence at all.

The Cast Operator

Clearly we face problems with assignments like:

f = i / j;

if the compiler is just going to proceed with integer division we would be forced to declare some real variables, assign the integer values and divide the reals. However, the compiler allows us to “change our mind” about the type of a variable or expression. This is done with the cast operator. The cast operator temporarilychanges the type of the variable/expression it is applied to. Thus in:

f = i / j;

Integer division would normally be performed (since both “i” and “j” are integer).

However the cast:

f = (double)i / j;

causes the type of “i” to be temporarily changed to double. In effect 5 becomes 5.0. Now the compiler is faced with dividing a double by an integer. It automatically promotes the integer “j” to a double (making it 4.0) and performs division using double precision maths, yielding the answer 1.25.



 
< Prev
Your Ad Here

RSS socialnet

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits