L3 Operators
L3 Operators
• In programming, various Operators are defined and used for the processing
purpose, to perform various mathematical and logical tasks.
• C language is rich in built-in operators. The characters that are used as
operators in C are
ME171: Computer Programming Language -> ++ -- & ^ + - / * % = >= <=
ME 171 Department of Mechanical Engineering, BUET ME 171 Department of Mechanical Engineering, BUET
Arithmetic Operators Cast Operator
Order of precedence • int a=14; float b=10.00; printf("%d",(a-b)%3); // error
• int a=14; float b=10.00; printf("%d",((int) (a-b))%3); // 1
• Changing the data type of an arithmetic expressions by force is known as
Casting. Example:
(int) 7.5
(float) 5 / (float) 2
Precedence and Associativity
Operator Associativity
() Left to right
*/% Left to right
+- Left to right
• printf("%d", 5/3+2); // 3
• printf("%d", 5/(3+2)); // 1
• printf("%d", 5/(3+2)*2); // 2
• printf("%d", 5*(3+2)/2); // 12
ME 171 Department of Mechanical Engineering, BUET ME 171 Department of Mechanical Engineering, BUET
ME 171 Department of Mechanical Engineering, BUET ME 171 Department of Mechanical Engineering, BUET
Increment Operator (++) Example problem Decrement Operator (--)
• It works in the same way as increment operator, just decrement a variable by
• #include <stdio.h> • #include <stdio.h> 1.
int main() int main() • Both ++ and – only works on variables. Its operand can not be a constant or
{ any expression. So, the followings are wrong:
{
int a=14,app,d=10,ppa; p = ++(a+d); 61--; --(a++) ;
int a=14,d=10,ppa,app;
app = a++ + d; // 24 app = a++ + a + d; // 39
a=14; Precedence and Associativity
a=14;
ppa = ++a + d; // 25 ppa = ++a + a + d; // 40 Operator Associativity
printf("%d\t%d",app,ppa); printf("%d\t%d",app,ppa); () Left to right
return 0; return 0; Unary +, Unary - Right to left
} } ++ -- Right to left
*/% Left to right
+ - Left to right
ME 171 Department of Mechanical Engineering, BUET ME 171 Department of Mechanical Engineering, BUET
== Checks if the values of two operands are equal or not. A == B <= Checks if the value of left operand is less than or A <= B
If yes, then the condition becomes true. False equal to the value of right operand. If yes, then the True
condition becomes true.
!= Checks if the values of two operands are equal or not. A != B
If the values are not equal, then the condition True
becomes true. • Find the output of the following: •Find the output of the following:
#include <stdio.h> #include <stdio.h>
> Checks if the value of left operand is greater than the A>B
value of right operand. If yes, then the condition False int main() int main()
becomes true. {float a=2.5; double b=2.5; { printf("%d", 10+20>=30);
printf("%d\t%d", 2<5,a==b); return 0;}
return 0;} Output: 1
ME 171 Department of Mechanical Engineering, BUET Output: 1 1
Logical Operator Logical Operator
• They works on based on some logics. They are of three categories: Logical Negation (!)
Logical AND (&&), Logical OR (||), and Logical negation (!) operator. • Its an unary operator. It should be placed before its operand.
• If the operands of a logical negation expression has a value 0, then it reverse
Operand 1 Operand 2 (Operand 1) && the value of the expression as 1 and vice versa.
(Operand 2)
Example: C = !0; // C=1
0 0 0
0 1 (any non zero value) 0 printf(“%d”, !1); // print 0
1(any non zero value) 0 0 printf(“%d”, !8); // print 0
1(any non zero value) 1(any non zero value) 1
HomeTask: Find the value of the following expressions.
Operand 1 Operand 2 (Operand 1) || !5-4
(Operand 2) !(5-4)
0 0 0
0 1(any non zero value) 1 3<3 && 0<5
1(any non zero value) 0 1 4-1<=4 || -1 == 0-1
1(any non zero value) 1(any non zero value) 1 -(-4-1) == 5 || !(3==3) && -1 > 0-1
-(-4-1) == 5 && !(3==3) || -1 > 0-1
ME 171 Department of Mechanical Engineering, BUET ME 171 Department of Mechanical Engineering, BUET
ME 171 Department of Mechanical Engineering, BUET ME 171 Department of Mechanical Engineering, BUET