Chap 3
Chap 3
exp !exp
T F
F T
Operator Description Example a=20,b=10 Output
&& logical AND (a>b)&&(a<c) (10>20)&&(10<30) 0
|| logical OR (a>b)||(a<=c) (10>20)||(10<30) 1
! logical NOT !(a>b) !(10>20) 1
• Assignment operator
• Assignment operator are used to assign the result of an expression to a variable.
• We already have seen the usual assignment operator, ‘=‘.
• In addition, C has a set of shorthand assignment operators of the form:
V op= exp;
• Where v is a variable, exp is an expression and op is a C binary arithmetic operator.
• The operator op= is known as the shorthand assignment operator.
• The assignment statement
V op= exp;
Is equivalent to
V = V op exp;
• The use of shorthand assignment operators has three advantages:
• What appears on the left-hand side need not be replaced and therefore it
becomes easier to write.
• The statement is more concise and easier to read
• The statement is more efficient
• Increment and decrement operators (Unary operator)
• Increment operator (++)
• This operator increments the value of a variable by 1
• The two types include −
pre increment
post increment
• If the increment operator is placed before the operand, then it is pre-
increment. Later on, the value is first incremented and next operation is
performed on it.
• For example,
z = ++a; is equivalent to a= a+1 then z =a
• If we place the increment operator after the operand, then it is post
increment and the value is incremented after the operation is
performed.
• For example,
z = a++; is equivalent to z=a then a= a+1
What is the output of the following program:
#include <stdio.h>
int main() {
float a = 5, b=6;
int x = ++a - b +10;
printf("%d\n%f",x,a);
return 0;
}
• Decrement operator:
• It is used to decrement the values of a variable by 1.
• The two types are −
• pre decrement
• post decrement
• If the decrement operator is placed before the operand, then it is called pre
decrement. Here, the value is first decremented and then, operation is
performed on it.
• For example,
z = - - a; is equivalent to a= a-1 then z=a
• If the decrement operator is placed after the operand, then it is called
post decrement. Here, the value is decremented after the operation is
performed.
• For example,
z = a--; is equivalent to z=a then a= a-1
• Conditional operator
• A ternary operator pair “?:” is available in C to construct conditional
expressions of the form
Exp1?exp2:exp3
Where exp1, exp2 and exp3 are expressions.
• The operator “?:” works as follows: exp1 is evaluated first. If it is non zero
(true), then the expression exp2 is evaluated and becomes the value of the
expression.
• If exp1 is false, exp3 is evaluated and its value becomes the value of the
expression.
• Note that only one of the expressions (either exp2 or exp3) is evaluated.
If exp1 is true, exp2 is evaluated. Otherwise, exp3 is evaluated. Or in
the form of if-else.
if (exp1)
exp2;
else
exp3;
• Bit wise operator
• C has a distinction of supporting special operators known as bitwise operators
for manipulation of data at bit level.
• These operators are used for testing the bits, or shifting them right or left.
• Bitwise operators may not be applied to float or double.
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Left Shift
>> Right shift
~ One's Complement
Bitwise AND Bitwise OR
a b a&b a b a|b
0 0 0 0 0 0
0 1 0 0 1 1
1 0 0 1 0 1
1 1 1 1 1 1