operators
Operators
C supports a rich collection of operators. An operator is a symbol that tells the computer to perform certain mathematical or logical operations. Operators are used to manipulate data and variables. The data items that operators act upon are called operands. Operators and operands are combined together to form expressions. Some operators require only one operand while others need more than one operand. Accordingly, operators are called unary operators if they act on a single operand and are called binary operators if they act on two operands.
C operators can be broadly classified into the following categories:
1) Arithmetic operators
2) Relational operators
3) Logical operators
4) Assignment operators
5) Conditional operator
6) Special Operators
Arithmetic Operators
C provides five arithmetic operators, which are given below:
+ addition
- subtraction
* Multiplication
/ Division
% Modulus(remainder)
Note that C has no operator for exponentiation. However, there is a library function(pow) to carry out exponentiation.
It should be understood that arithmetic operators can act upon numeric operands only. Thus the valid operands for arithmetic operators are integer quantities, floating-point quantities and characters (since characters are represented as integer values that are determined by the computer's character set). Moreover, the modulus operator (%) requires that both the operands must be integers and the second operand must be non-zero. Similarly, the second operand of the division operator (/) must also be non-zero, however the operands need not necessarily be integers.
Example:
Suppose m and n are integer variables whose values are 11 and 4 respectively.
Expression Value
m + n 15
m - n 7
m * n 44
m/n 2
m % n 3
Notice, the truncated quotient resulting from the division operation. This happens because both the operands represent integer quantities. Division of one integer quantity by another is referred to as integer division. The result of integer division is always an integer, that is, the decimal part of the result is always truncated. On the other hand, if a division operation is carried out with two floating-point numbers, or one floating-point number and one integer, the quotient will be a floating-point number.
Example:
Suppose x and y are floating-point variables whose values are 11.5 and 4.0 respectively.
ExpressionValue
x + y
15.5
x-y
7.5
x * y
46.0
x/y
2.875
Example:
Suppose a and b are character variables that represent the characters a and A respectively.
Expression Value
a 65
a + b 162
a + b + 'O' 210
Precedence
We should now know how an expression containing multiple arithmetic operators is evaluated. There are two distinct priority levels of arithmetic operators.
* / % Have higher precedence + + - Have lower precedence
The evaluation of an expression is basically two-pass process. In the first pass, high priority operators(if any) are applied as they are encountered from left to right. In the second pass, low priority operators (if any) are applied as they encountered from left to right.
Example:
n=a-b/4+c*2-3
If a=10, b=16 and c=5, then the statement becomes
n = 10-16/4 + 5*2-3
and is evaluated as
Pass 1 : Step 1: n=10-4+5*2-3
Step 2: n= n = 10-4 + 10-3
Pass 2 : Step 1: n = 6 + 10-3
Step 2: n = 16-3
Step 3: n = 13
The natural precedence of operations can be altered through the use of parentheses. Whenever parentheses are used, the expressions within parentheses assume highest priority.
Example :
n = 10 -18/ (4 + 5)* (3 -2) is evaluated as
Pass 1 :
Step1: n = 10 -18/9 * (3 - 2)
Step 2: n = 10-18/9*1
Pass 2 : Step1: n = 10-2*1
Step 2: n = 10-2
Pass 3 : Step 1: n = 8
Apart from the five binary arithmetic operators mentioned so far, C also supports three unary operators viz. unary minus(-), increment operator (++) and decrement operator (- -).
Unary Minus (-)
The symbol is the same as used for subtraction. Unary minus is used to indicate or change the algebraic sign of a value. For example:
a = -75;
b = -a;
assign the value -75 to a and the value 75 (i.e., -(-75)) to b. The minus sign used in this way is called the unary operator because it takes just one operand.
Strictly speaking, there is no unary '+' operator in C.
Increment and Decrement Operators
C includes two useful operators, which are generally not found in other computer languages developed prior to C. These are the increment operator (++) and the decrement operator (--). The operator ++ adds 1 to its operand, whereas the operator ~ subtracts 1 from its operand.
To be precise,
x = x + 1; can be written as x++;
and
x = x -1; can be written as x--;
Both these operators may either precede or follow the operand, i.e.,
x = x + 1; can be represented asx++; or ++x;
The former one is known as the post-increment operator, while the latter one is known as the pre-increment operator.
Similarly,
x = x -1; can be represented asx--; or --x;
and the operators are known as post-decrement operator and pre-decrement operator respectively.
The difference between pre- and post- fixing the operator is useful when it is used in an expression. When the operator precedes the operand, C performs the increment or decrement operation before using the value of the operand. On the other hand if the operator follows the operand, the value of the operand is used before incrementing or decrementing it.
Example:
x = 1;
i = i+(++x);
In the above expression, the value of x, which is added to i, is 2. On the other hand,
x=1;
i = i+(x++);
x = 1 is added to i; however, in both the cases the value of x after evaluating the expression is 2.
Example:
x=1;
printf(“x = %d\n", x);
printf(“x = %d\n", x++);
printf(“x = %d\n", ++x);
The output of the above segment of code is : x = 1
X =1
x = 3
The first printf statement simply prints the value of x, which is initially 1. The second printf statement does two things - first it displays the value of x, which is then 1; next it increments the value of x by 1, which then becomes 2. The third printf statement again does two things - first it increments the value of x, which now becomes 3 and then prints the updated value of x.
Note:
Al! the three unary operators viz. minus, increment and decrement fall in the same precedence group, and each of them have a higher precedence than all binary arithmetic operators.
Exercises
1) If a = 22.2/b + (3 * b +( 4 * c) / b *d) / (2 / n) ; which operation will beperformed first?
a) 22.2/b
b) 3*b
c) 4*c
d) 2/n
2) If b is an integer variable, b = 9/2; will return a value:
a) 4.5
b) 5
c) 4
d) 0
3) The expression , b = 30 * 1000 + 2768 evaluates to:
a) 32768
b) -32768
c) 113040
d) 0
Answers to Selected Exercises
a) a
b) c
c) a
Relational Operators
C supports the following 6 relational operators:
> greater than
>= greater than or equal
< equal =" ="><=>>= Have higher precedence== != Have lower precedence
Each of these relational operators require two operands and can be combined together to form relational expressions or logical expressions, which represent conditions that are either true or false. However, the resulting expressions are of type integer, since in C true is represented by the integer value 1 and false is represented by the value 0.
Precedence
The precedence of all these operators is lower than all the binary arithmetic operators, and are evaluated left-to-right. Thus, when arithmetic expressions are used on either side of a relational operator, the arithmetic expressions will be evaluated first and then the results will be compared.
Example:
Suppose i is an integer variable whose value is 10, f is a floating-point variable whose value is 2.5 and c is a character variable whose value is A.
Expression Value
i>5 1
(i + f)<12>i + f 1
c = = 65 1
c + i<=f*i*10 1
Logical Operators In addition to the relational operators discussed, C supports three logical operators that act upon logical expressions. These are && ANDII OR ! NOT Thus, logical operators can be used to form more complex conditions that are either true or false. The && (AND) operator 1. Evaluates true when both the relational expression are true. 2. Evaluates false when any one of the relational expression is false. The following truth table will make things clearer: Condition 1 False True False True Condition 2 Output False False True True True False False False The (OR) operator 1. Evaluates true when any one the relational expressions is true. 2. Evaluates false when both the relational expressions are false. The following truth table will make things clearer: Condition 1 Condition 2 Output False False False True True True False True True True False True Bitwise OR operator Condition 1 Condition 2 Output 0 0 0 0 1 1 1 0 1 1 1 1 In the above examples True is considered as 1 and False is considered as 0. Suppose a program has to perform certain steps on the basis of the condition that a < b ="=" b="=" b ="="> = 10))
/* s is not equal to or more than 10 */
Both relational and logical operators are lower in precedence than the arithmetic operators.
Example:
5>4 + 3
is evaluated as, 5 > 7. The arithmetic operation (4 + 3) precedes the logical expression (5>).
(The result for this will be false, that is zero will be returned).
The following printf()
printf("%d", 5> 4+ 3)
will give
0
as 5 is not greater than 7 i.e. (4 + 3). So it returns FALSE i.e. 0.
The !(NOT) operator
This is a unary operator that negates the value of a relational or logical expression.
1. Evaluates true when the relational expression is false.
2. Evaluates false when the relational expression is true.
The following truth table will make things clearer:
Condition
Output
False
True
True
False
In this context, it should be noted that any nonzero value, not just 1, is interpreted as true.
Precedence
Logical and has a higher precedence than logical or and both of them are evaluated left to right. Also, both of them have a lower precedence than that of the group containing equality operators. The unary NOT operator, however, fall into the same precedence group as the unary arithmetic operators.
Example:
Suppose i is an integer variable whose value is 10, f is a floating-point variable whose value is 2.5 and c is a character variable whose value is A.
Expression Value
(i>5) && (c!='a') 1
(i<=f) (c= =’a’) 0 !(f>2) 0
(i>5) (c=='a') && (i
Similarly, in the fifth expression, !i is first evaluated to check for true of false. Here, it should be noted that the ! operator has a higher precedence than &&.
Bitwise Operators
C provides six operators for bit manipulation; these may only be applied to integral operands, that is, char, short, int and long, whether signed or unsigned.
& bitwise AND
bitwise inclusive OR
^ bitwise exclusive OR
« left shift
» right shift
~ one's complement (unary)
The bitwise AND operator & is often used to mask off some set of bits.
Example:
n = n&0177
If we now convert 0177 to binary numbers the number becomes this 0000000101110111 and any number added to this number will have the higher order 7 bits to zero because 1 & 0 = 0.
The bitwise OR operator is used to turn bits on:
#define SET_ON 5 /* 5 in binary is 00101*/
x = x I SET_ON;
The operator is used for inclusive OR i.e if x = (11001 )2 and the bits in SET_ON are 00101 then
11001 00101
11101
Sets to one in x, the bits that are set to one in SET_ON.
The bitwise exclusive OR operator A sets a one in each bit position where its operands have different bits and zero where they are the same.
If we take two binary numbers supposing 110010 and 101000 then if we perform the bitwise exclusive OR operatorA the result would be:
110010 101000
011010
Exercises
1) Perform the AND (&&) operation on the following two binary numbers
a) 1010 and 0101
b) 11100101 and 01001101
2) Perform the OR () operation on the following two binary numbers
a) 101011 and 010101
b) 101001010 and 010100011
3) Perform the exclusive OR ( A Operation on the following two binarynumbers
a) 0110 and 0101
b) 1100110 and 0011010
Answers To Selected Exercises
a) 0000
b) 01000101
a) 111111
b) 111101011
a) 0011
b) 1111100
Assignment Operators
The most commonly used assignment operator in C is = and it is used as follows:
identifier = expression
where identifier generally represents a variable and expression represents a constant, variable and a more complex expression.
Note that the assignment operator = is different from the equality operator ==. The assignment operator is used to assign a value to an identifier, where as, the equality operator is used to determine if expressions have the same value.
Also, note that the two operands in an assignment can be of different types. In such cases, the value of the expression on the right will automatically be converted to the type of the identifier on the left. For example, in case of the assignment expression
i = 5.8
causes the integer variable i to have the value 5, that is, the decimal part is truncated.
C also allows multiple assignment statements to be combined into a single statement as shown below:
identifierl = identified = ? = identifiern = expression
Here the assignments take place from right-to-left. That is, the assignment statement
identifierl = identifier?. = expression is equivalent to
identifierl = (identifier2 = expression)
C also supports 5 shorthand assignment operators viz. +=, -=, *=, /=, %=. The assignment statement
identifier += expression is equivalent to
identifier = identifier + expression
The assignment statement
identifier -= expression is equivalent to
identifier = identifier - expression and so on for the remaining 3 operators.
Example:
Suppose i and j are integer variables with values 5 and 10, and f and g are floating-point variables with values 2.5 and 5.5.
Expression Equivalent Expression Value
i+=5 i = i+5 10
f-=j f = f-j 7.5
j*=(i + 6) j=j*(i+6) 110
f /= 5 f = f / 5 0.5
j%=(i-1) j=j%(i-1) 2
Precedence
Assignment operators have a lower precedence than any of the operators discussed so far. Thus unary operations, arithmetic operations, relational operations, equality operations, logical operations are all carried out before assignment operations. Also, in a multiple assignment statement, assignments take place from right-to-left.
Conditional Operator
C offers a ternary operator pair '? :' that can be used to carry out simple conditional operations. It is used as follows:
expressioni ? expression2 : expression3
where expressioni is a logical expression, while expression2 and expression3 can be constants, identifiers or other complex expressions.
When a conditional operator is used, expressioni is first evaluated. If expressioni is true(i.e. if it is nonzero), then expression2 is evaluated and this becomes the value of the expression. However, if expressioni is true(i.e. if its value is zero), then expression3 is evaluated and this becomes the value of the expression. Note that only one of the embedded expressions(either expression2 or expression3) is evaluated.
Example:
Suppose i and j are integer variables, with values 5 and 10 respectively.
(i>j)?i:j
The resulting expression will have the value 10. This expression essentially returns the maximum of the two numbers.
Precedence
The precedence of the conditional operator is just above that of the assignment operators and they are evaluated from right-to-left.
Hierarchy of Operators
Now that we have discussed so many operators, we should try to summarize all that we have learnt so far. The following table summarizes the precedence from highest to lowest, of all the operators discussed so far.
Operator Description
Operators
Associativity
Unary Operators
- ++ - !
Right-to-Left
Arithmetic multiply, divide,
* / %
Left-to-Right
modulus
Left-to-Right
Arithmetic add, subtract
+ -
Left-to-Right
Relational operators
<<=>>=
Left-to-Right
Equality operators
== !=
Left-to-Right
Logical and
&&
Left-to-Right
Logical or
II
Right-to-Left
Conditional operator
?:
Right-to-Left
Assignment operators
= += .= *= /=
%=
Example:
Suppose i, j and k are integer variables.
k += (i > 0 && i<=10 ) ? ++i: i%j; This statement works as follows : first the conditional expression (i>0&&i<=15)
is evaluated. If this expression is true, the expression ++i is evaluated; otherwise, the expression i%j is evaluated. Finally, the assignment operation += is carried out, causing the value of k to be increased by the value of conditional expression.
If i, j and k have the values 5, 10 and 15 respectively, then the value of th conditional expression will be 6(because the expression ++i will be evaluated) and the value of k will increase to 21 (k = k + 6). On the other hand, if i, j and have the values 15, 10 and 6 respectively, then the value of the condition expression will be 3(because the expression i%j will be evaluated), and t value of k will increase to 9(k = k + 3).

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home