Tutorial 4: Operators

Arithmetic Operators
Arithmetic operations can be performed in C programming language. There are five operators available in C which are + (addition), - (subtraction), * (multiplication), / (division) and % (modulus). +, -, * and / are self explained. % operator is to obtain the remainder of when we are dividing a number with a number. For example,

x % y

produce the remainder when x is divided by y.

The multiplication, division and modulus have higher precedence over the addition and subtraction. Its means /, * and % will be evaluated before + and - operators.

Relational and Logical Operators
There are six (6) relational operators which are < (Less than), > (Greater than), <= (Less or Equal to), >= (Greater or Equal to), == (Equal to) and != (Not equal to). Note that relational operators have lower precedence than arithmetic operators.

There are two (2) logical operators which are || (OR) and && (AND). Expression consists of logical operators is evaluated left to right. The precedence of && is higher than || and logical operators have lower precedence than relational operators. There is a unary negation operator ! that converts a zero (0) to one (1) and vice-verse.

For example lets look at the following expression. Assume x = 2, y = 9 and z = 3
x > 5 || y < 7 && z != 2

Since relational operators have higher precedence, they will be evaluated first.
x > 5 returns False (0)
y < 7 returns True (1)
z != 2 returns True (1)

Thus, the expression can be rewritten as
0 || 1 && 1

Now, && is evaluated then ||. Expression 1 && 1 returns 1, then the expression becomes
0 || 1

which returns 1.

Increment and Decrement Operators
C provides two operators for incrementing and decrementing variables. If we have
++x

the expression is adding x by 1. Similarly if we have
--x

the expression is subtracting x by 1.

Postfix and Prefix
Writing the increment or decrement operator in front of a variable is called prefix form. We may write the operator after a variable and this is called postfix form. Post-incrementing or pre-incrementing variable has the same effect if it is not used in an expression. Ultimately the value of the variable is added by 1. Post-incrementing and pre-incrementing must be considered when it is used in an expression. This is because for a variable that is post-incremented, the incrementing of the variable is performed after the variable is used while for pre-incrementing, the incrementing is performed before the variable is used. This is a bit confusing but lets look at an example to have better understanding. Assume i = 20 and j = 4 in the statement

sum = i - j++;

sum will have the value of 16 since j is incremented after the expression is evaluated. After evaluation j will have the value of 5.

Now consider the following statement.

sum = i - ++j;

sum will have the value of 15 since j is incremented before the expression is evaluated. After evaluation j will have the value of 5. Now you can see the different. Post-incrementing and pre-increment do can effect the result of an expression. Therefore we have to be careful when using with it in an expression.

Summary of Operators Precedence and Associativity¹


¹(Kernighan & Ritchie) The Ansi C Programming Language

No comments:

Post a Comment