Operators

 

Unary Operators

The Unary operators prefixed to an integer constant tells the compiler to reverse the sign by subtracting the value from zero. The effect it has similar to using the - sign to indicate a number less than zero.

Consider the following statement:

int value=-12;

 

Binary Operators

Binary Operators of C are the same as in other programming languages. These are + (add), - (subtract), * (multiply), / (divide) and % (modulo). The parathenses () are used to clarify complex operation.

For instance,

int x,y,z;

x = 27;

y = x % 5;     

/* y set to 2 */

z = x / 5;       

  /* z set to 5 and not 5.4 */

 

Ternary Operators

C offers a shorthand way of writing the commonly used if...else construct:

if (condition)

{

  // statements if condition is true

}

else

{

 // statements if conditions is false

}

 The shorthand can reduce code in many situations.

The general form of an expression that uses the ternary operator is:

(test-expression) ? T-expression : F-expression;

Thus ternary expression contains 3 parts and thus term ternary.

 

Compound Assignment Operator

Apart from the standard assignment operator (=), C offers compound assignment operators which simplify the following statements.

For example:

total=total+some_value;   /* Increase total by some_value */

can be written as:

total +=some_value;

Thus general form of this compound assignment is

left-value op= right-expression;

The above general form translates to:

left-value = left-value op right-expression

where op          is the Binary Operator

Increment/ Decrement Operator

Incrementing  and decrementing by 1 is such a common computation that C offers several shothand versions of the above type of assignment.

For instance:

total = sum++;     /* Statement 1 */

and

total =++sum;   /* Statement 2 */

The first statement is equivalent to:

total = sum;

sum = sum + 1;

The second statement is equivalent to :

sum = sum + 1;

total = sum;

However , ++ (or --) cannot be used on the left side of an assignment operator.

For instance,

sum ++= total;

is not valid.

 

 

Copyright © 2008 Manikkumar. All rights reserved.