Sunday, March 18, 2007

Expressions & Operators

Operators

An operator is a function which is applied to values to give a result.

Arithmetic operators are the most common. Other operators are used for comparison of values, combination of logical states, and manipulation of individual binary digits.Operators and values are combined to form expressions. The values produced by these expressions can be stored in variables, or used as a part of even larger expressions.

Assignment Statement

The easiest example of an expression is in the assignment statement.An expression is evaluated, and the result is saved in a variable.

y = (m * x) + c
This assignment will save the value of the expression in variable y.

Arithmetic operators

The most common arithmetic operators are,
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Reduction (Its simply the remainder from integer division)

*, / and % will be performed before + or - in any expression.
Brackets can be used to force a different order of evaluation to this.
Where division is performed between two integers, the result will be an integer, with remainder discarded.
Modulo reduction is only meaningful between integers.
If a program is ever required to divide a number by zero, this will cause an error, usually causing the program to crash.

C has some operators which allows abbreviation of certain types of arthmetic assignment statements.

i++; or ++i; This is equivalent to i=i+1;
i--; or --i; This is equivalent to i=i-1;


These operations are usually very efficient. They can be combined with another expression.x = a * b++; This expression is commputed as follows,

x = a * b;b = b + 1;
Where as,x = --i * (a + b);
This is computed as follows,i = i - 1;
is computed first and with that value of i the expressionx = i * (a + b); is computed.

ComparisonC has a lot of comparison operators, Comparison operators are used to compare two values.

The Comparison operators are,

== Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!= Not equal to


Note that == is used to compare two variables and = is used asassignments operator.Examples of comparison operators,

x==y
i>10
a + b != c


Logical ConnectorsThe Logical operators are,

&& AND
OR
! NOT


They are frequently used to combine relational operators,
Example:
x <>= 10

In C these logical connectives employ a technique known as lazy evaluation. They evaluate their left hand operand, and then only evaluate the right hand one if this is required. Clearly false && anything is always false, true anything is always true. In such cases the second test is not evaluated.

Not operates on a single logical value, its effect is to reverse its state.
Here is an example of its use.

if ( ! acceptable )
printf("Not Acceptable !!\n");

-----------------------------------------------------------------------------------------------


Type conversion

Type casting is the process of converting a data of one type into another for calculations. For example a float is converted into int for calculating.

char types will be treated as int when that is directly considered for calculation and the value will the ASCII value of the char.
  1. 1. When the variable is too small to hold the value. In this case it will be corrupted, So the variable is type casted to the right type.
  2. 2. The variable is an integer type and is being assigned a real value, the value is rounded down.

Values passed as function arguments must be of the correct type. The function has no way of determining the type passed to it, so automatic conversion cannot take place. This can lead to corrupt results. The solution is to use a method called casting which temporarily disguises a value as a different type.

Consider the following example.The function sqrt finds the square root of a double.

int i = 256;
int sqrroot;


sqrroot = sqrt( (double) i);

The cast is made by putting the bracketed name of the required type just before the value. (double) in this example. The result of sqrt( (double) i); is also a double, but this is automatically converted to an int on assignment to root.

No comments: