Wednesday, 4 March 2015

Operators in C


Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the expression will be manipulated. C supports several types of operators and they can be classified as:

· Assignment operator
· Arithmetic operators
· Relational operators
· Logical operators
· Bitwise operators
· Special operators
· Pointer operators

Pointer operators and special operators will not be covered in this chapter. They will be introduced
later under relevant sections.
.
Assignment Operator

The assignment operator is the simple equal sign (=). The assignment operator is used to assign a value to a variable. The format of an assignment statement is:

variable-name = expression;

The expression can be a single variable or a literal, or it may contain variables, literals and operators. The assignment operation always takes place from right to left. Consider the following set of examples:

a = 5; // value of variable ‘a’ becomes 5
a = 5+10; // value of variable ‘a’ becomes 15
a = 5 + b; // value of variable ‘a’ becomes 5 + value of b
a = b + c; // value of variable ‘a’ becomes value of b + value of c
a = (x*x + y*y)/2;

In C lots of shortcuts are possible. For example, instead of the statement,

a = a + b;

the programmer may use the shorthand format:

a += b;

Such operators are called compound assignment operators. The assignment operator can be combined with the major arithmetic operations such as; +, -, *, / and %. Therefore many similar assignments can be used such as:

a -= b; // is same as a = a-b;
a *= b; // is same as a = a*b;
a /= b; // is same as a = a/b;
a %= b; // is same as a = a%b;

Arithmetic Operators

C supports five major arithmetic operators and two extended (shortcuts) operators (Table 3.1). Program-3.1 illustrates the usage of major arithmetic operators.

/* Program-3.1 */

#include <stdio.h>
int main()
{
int a,b;
printf("Enter a: ");
scanf("%d", &a); //read value of a
printf("Enter b: ");
scanf("%d", &b); //read value of b
printf("\na+b = %d", a+b); //display sum of a & b
printf("\na-b = %d", a-b); //display subtraction of b from a
printf("\na*b = %d", a*b); //display multiplication of a & b
printf("\na/b = %d", a/b); //display division of a by b
printf("\na%%b = %d", a%b); //display modulus of a divided by b
return 0;
}

Executing Program-3.1 with “a” as 5 and “b” as 2 will display the following:

Enter a: 5
Enter b: 2
a+b = 7
a-b = 3
a*b = 10
a/b = 2
a%b = 1


Increment and Decrement Operators

Increment and decrement operators are very useful operators. The operator ++ means “add 1” or  increment by 1”. Similarly operator -- mean “subtract 1” or “decrement by 1”. They are also equivalent to +=1 and -=1. For example, instead of using the following expression to increment a variable:

a = a + 1;

you may use:

a +=1; or a++;

Also expression:

a = a - 1;

can be written as:

a -=1; or a--;

Both increment and decrement operators can be used as a prefix or as a suffix . The operator can be written before the identifier as a prefix (++a) or after the identifier as a suffix (a++). In simple operations such as a++ or ++a both have exactly the same meaning. However in some cases there is a difference. Consider the following set of statements:

int a, x;
a = 3;
x = ++a;

After executing above segment of code, “a” will be 4 and “x” will be 4. In line 3, first the variable “a” is incremented before assigning it to variable “x”. Consider the following segment of code.


int a, x;
a = 3;
x = a++;

After executing above code segment “a” will be 4 and “x” will be 3. In the second approach in line 3 first “a” is assigned to “x” and then “a” is incremented.


Precede nce and Associatively of Arithmetic Operators

Precedence defines the priority of an operator while associativity indicates which variable(s) an operator is associated with or applied to. Table 3.2 illustrates the precedence of arithmetic operators and their associativity.


In the same expression, if two operators of the same precedence are found, they are evaluated from left
to right, except for increment and decrement operators which are evaluated from right to left. Parentheses can also be used to change the order of evaluation and it has the highest precedence. It is recommended to use parentheses to simplify expressions without depending on the precedence.

Relational and Logical Operators

The relational operators are used to compare values forming relational expressions. The logical operators are used to connect relational expressions together using the rules of formal logic. Both types of expressions produce TRUE or FALSE results. In C, FALSE is the zero while any nonzero number is TRUE. However, the logical and relational expressions produce the value “1” for TRUE and the value “0” for FALSE. Table 3.3 shows relational and logical operators used in C.


Precedence of Relational and Logical Operators

As arithmetic operators, relational and logical operators also have precedence. Table 3.4 summarises the relative precedence of the relational and logical operators. These operators are lower in precedence than arithmetic operators.



For an example consider the following expression:

10 > 8+1

is equivalent to the expression:

10 > (8+1)

In order to understand how logical expressions are evaluated consider the following expression:

a==b && c > d

This expression says that; “(a is equivalent to b) AND (c is greater than d)”. In other words this expression is evaluated as TRUE if both the conditions are met. That is if a == b is TRUE and c > d is also TRUE. If AND (&&) is replaced by logical OR (||) operation then only one condition needs to be TRUE.

Consider another expression (assume both “a” and “b” are integers and their value is 5) :
!(a==b)

In this case, since both “a” and “b” have similar value 5, the logical value of the expression within the parenthesis is TRUE. However the NOT operation will negate the result which is TRUE. Therefore
the final result will be FALSE.

In order to see the effect of precedence consider the following expression:

a==b && x==y || m==n

Since equal operator (==) has a higher precedence than the logical operators the equality will be checked first. Then the logical AND operation will be performed and finally logical OR operation will be performed. Therefore this statement can be rewritten as:

((a==b) && (x==y)) || (m==n)

This expression is evaluated as TRUE if:

· a==b is evaluated as TRUE and x==y is evaluated as TRUE
· Or m==n is evaluated as TRUE.



Bitwise Operators

Using C you can access and manipulate bits in variables, allowing you to perform low level
operations. C supports six bitwise operators and they are listed in Table 3.5. Their precedence is lower
than arithmetic, relational and logical operators (see Table 3.6).







Bitwise AND, OR and XOR operators perform logical AND, OR and XOR operations at the bit level.
Consider following set of expressions:

int a,b,c;
a = 12;
b = 8;
c = a & b;

The bitwise AND operation will be performed at the bit level as follows:

a = 12 à 00001100
b = 8 à 00001000 &
00001000

Then the variable “c” will hold the result of bitwise AND operation between “a” and “b” which is
000010002 (810).


Shift Operators

Shift operators allow shifting of bits either to left or right. We know that 8 is represented in binary as:

8 = 00001000

and when it is shifted by one bit to right, we get:

8 = 00001000 à 00000100 = 4

Similarly when the number is shifted by one bit to left, we get:

16 = 00010000 ß 00001000 = 8

The C right shift (>>) operator shifts an 8-bit number by one or more bits to right while left shift (<<) operator shifts bits to left. Shifting a binary number to left by one bit will multiply it by 2 while shifting it to right by one bit will divide it by 2. After the shift operation the final result should also be a 8-bit number, therefore any additional bits are removed and missing bits will be filled with zero. In general the right shift operator is used in the form:

variable >> number-of-bits

and left shift is given in the form:

variable >> number-of-bits

In the following expression the value in the variable “a” is shift by 2 bits to right:

a >> 2

Table 3.7 summarises all the operators supported by C, their precedences and associativit ies. However
some operators can be confusing, as they use the same characters (for example , * is used for multiplication and also to denote pointers). However the following rule will reduce any confusion. The unary operators (operators with only one operand such as &a (address of a)) have a higher precedence than the binary operators (operators with two operands such as a*b). When operators have the same precedence an expression is evaluated left to right.







No comments:

Post a Comment