0% found this document useful (0 votes)
17 views6 pages

L3 Operators

Uploaded by

roukff61736220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

L3 Operators

Uploaded by

roukff61736220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Operators and Operands

• 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 -> ++ -- & ^ + - / * % = >= <=

< > << >> <<= >>= && || ! != ?: += etc


Semester: January 2024

• The operators in C can be categorized based on their purpose as


1. Arithmetic operators 2. Unary operators 3. Relational operators
Operators 4. Logical operators 5. Assignment operators 6. Conditional operators
7. Bitwise operators. 8. Bit field operators
• Operators works with two or one (Unary operators) variables, constants or
expressions. These variables, constants or expressions on which operators
works are called as Operands.
Dr. Sheikh Mohammad Shavik • The suitable use and presentation of these operators and operands make
Associate Professor, ME, BUET Expressions.
Department of Mechanical Engineering, BUET 1 ME 171 Department of Mechanical Engineering, BUET

Arithmetic Operators Arithmetic Operators


Arithmetic Operator Description Expression (Let A = 10, B = 15) • int a=14,b=10;
printf("%d",a-b); /* %f can’t be used for integer type
+ Adds two operands. A+B // 25
will show garbage value*/
- Subtracts second operand A − B // -5 • int a=14; float b=10.00;
from the first.
printf("%f",a-b); /* %d can’t be used for float type
* Multiplies both operands. A*B // 150
will show garbage value */
/ Divides numerator by de- B / A // 1 (not 1.5) type)
(For integer
numerator.
• long double > double > float > long int > int > short int > char
% Modulus Operator and B % A // 5
remainder of after an A % B // 10
integer division. #include <stdio.h>
void main()
{ char ch1='A', ch2='B';
• All the operators are Binary operators and the operands can be of any data printf("%d\t",ch1+ch2);
type except modulas operator (%). printf("%d\t",ch1+ch2+'5');
• The operands for modulas operator must be of integer data type. printf("%d",ch1+ch2+5);}
Output: 131 184 136 // ASCII value of char '5' is 53

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

Cast Operator Unary Operator


• Works on only single operand. They are:
In mathematics the floor, ceil and round of a fractional number x is unary plus (+)
defined as follows: unary minus (-)
increment operator (++)
floor(x) = the largest integer not exceeding x
decrement operator (--)
ceil(x) = the smallest integer not below x • B = 420; A = -B; // A is -420
round(x) = the nearest integer to x • -5*(B-A); // The value of the expression is -4200
According to the above definition when x = 2.3, Increment Operator (++)
• It increments a variable by 1. That if b = 420, then b++ means b = b +1. b will
floor(x) = 2, ceil(x) = 3 and round(x) = 2 then store 421 in his assigned memory. (No space between ++).
(a) If x = 2.7 what are its floor, ceil and round values? • ++ operator can be put both as a prefix or postfix of a variable. b++ and ++b
(b) Write down a program that will take a positive fractional number both increment its value by 1. But they have following dissimilarities:
 b++ increments b after its previous value has been used. (x = b++; // x takes
x as input and will print its floor and rounded values (ceil will be the value of b)
shown later when we learn if-else).  ++b increments b before its previous value has been used. (x = ++b; //x takes
the value of b+1)

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

Relational Operator Relational Operator


• Relational operators are binary operators. They relate two operands and Relational Description Expression (Let A =
compare them. Operator 10, B = 20
• A true condition assign a value of 1 to its corresponding expression. < Checks if the value of left operand is smaller than the A<B
value of right operand. If yes, then the condition True
• A false condition assign a value of 0 to its corresponding expression.
becomes true.
• The following table shows all the relational operators supported by C:
>= Checks if the value of left operand is greater than or A >= B
Relational Description Expression (Let A = equal to the value of right operand. If yes, then the False
Operator 10, B = 20) condition becomes true.

== 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

Assignment Operators Assignment Operators


• These operators are used to assign the value of a variable, constant or Operator Description Expression
expression to another variable. %= Modulus AND assignment operator. It C %= A
Operator Description Expression takes modulus using two operands Equivalent to C = C % A
= Simple assignment operator. Assigns values C = A + B Assign the and assigns the result to the left
from right side operands to left side operand value of A + B to C operand.
<<= Left shift AND assignment operator. C <<= 2 is same as C = C
+= Add AND assignment operator. It adds the C += A Equivalent to C = << 2
right operand to the left operand and assign C+A >>= Right shift AND assignment operator. C >>= 2 is same as C = C
the result to the left operand. >> 2
-= Subtract AND assignment operator. It C -= A &= Bitwise AND assignment operator. C &= 2 is same as C = C &
subtracts the right operand from the left Equivalent to C = C - A 2
operand and assigns the result to the left ^= Bitwise exclusive OR and assignment C ^= 2 is same as C = C ^
operand. operator. 2
*= Multiply AND assignment operator. It C *= A |= Bitwise inclusive OR and assignment C |= 2 is same as C = C | 2
multiplies the right operand with the left Equivalent to C = C * A operator.
operand and assigns the result to the left • If the data type of the two operands in an “=” assignment expression, left
operand. side operator will get the preference of its data type.
/= Divide AND assignment operator. It divides C /= A • Example: if b is an integer type variable,
the left operand with the right operand and Equivalent to C = C / A b = 5.3; will assign the value of b as 5
assigns the result to the left operand. b=“A”; will assign the value of b as 65
Conditional Operator (?:) Comma Operator (,)
• It assigns a value depending on the condition (True or False) of a given Comma (,) as separator
expression. It is usually used to separate two variables or conditions or expressions.
Structure: (condition) ? True_result : False_result Example:
Example: (x==0) ? printf(“2/x is not possible”) : printf(“%d”, 2/x); int a; int b; int c=20;
• This expression is equivalent to the following if else control statement: • These can be alternatively written as int a,b,c=20;
if(x==0) Comma (,) as an operator
printf(“2/x is not possible”); • Sometimes we assign multiple values to a variable using comma, in that
else printf(“%d”, 2/x); case comma is known as operator.
Home Task Example:
#include<stdio.h> a = 10,20,30; b = (10,20,30);
int main() • In the first statement, value of a will be 10, because assignment operator (=)
has more priority more than comma (,), thus 10 will be assigned to the
{ int x,y;
variable a.
scanf("%d",&x);
• In the second statement, value of b will be 30, because 10, 20, 30 are
x==0 ? printf("2/x is not possible") : printf("%d", 2/x); enclosed in braces, and braces has more priority than assignment (=)
y=(x==0) ? 0 : x; x!=0 ? y=x : printf("\n Remember, You have pressed 0"); operator. When multiple values are given with comma operator within the
printf("\n%d",y); braces, then right most value is considered as result of the expression. Thus,
return 0;} 30 will be assigned to the variable b.
ME 171 Department of Mechanical Engineering, BUET

Comma Operator (,) Bitwise Operators


#include <stdio.h> bitwise AND (&)
int main() a b a&b
0 0 0
{ int a,b; 0 1 0
a = 10,20,30; 1 0 0
b = (10,20,30); //printing the values Output: a=10 b=30 1 1 1
printf("a= %d, b= %d\n",a,b); bitwise OR (|)
return 0; } a b a|b
0 0 0
#include <stdio.h> 0 1 1
1 0 1
int main()
1 1 1
{ int a= 10,20,30; int b; Output: compilation error
bitwise XOR (^)
b= (10,20,30); //printing the values
a b a^b
printf("a= %d, b= %d\n",a,b); 0 0 0
return 0; } 0 1 1
• Consider the statement: int a= 10,20,30; It is a declaration with initialization 1 0 1
statement and here comma is a separator and we cannot use values like this. 1 1 0
ME 171 Department of Mechanical Engineering, BUET ME 171 Department of Mechanical Engineering, BUET
Bitwise Operators Bitwise Operators
• What is the output of a&b if a=15, b=50 ???? Write a code that takes a password as input and encrypts it using bitwise XOR operator.
void main() Your code should be able to decrypt your password back when needed.
15 = 0 0 0 0 1 1 1 1
{ #include <stdio.h>
50 = 0 0 1 1 0 0 1 0 void encrypt(char *pass) ; void encrypt(char *pass)
int a=15,b=50;
15&50 = 0 0 0 0 0 0 1 0 =2 { int i,m=5;
printf("%d", a&b); void decrypt(char *pass);
for (i=0;pass[i]!='\0';i++)
} int main() pass[i]^=m; }
{ charpass[8], a; void decrypt(char *pass)
• bitwise inverse (~) Output: 2 int i, m=5; { int i,m=5;
A= 0 0 0 0 1 1 1 1 (15) printf("Enter a 8 Digit/Letter Password: "); for (i=0;pass[i]!='\0';i++)
pass[i]^=m; }
B=~A= 1 1 1 1 0 0 0 0 (240) for (i=0;i<=8;i++)
• bitwise left shift (<<) scanf("%c", &pass[i]);
A= 0 0 0 0 1 1 1 1 (15) encrypt(pass);
puts(pass);
A<<2= 0 0 1 1 1 1 0 0 (60)
printf("DO YOU WANT TO DECRYPT YOUR PASSWORD: 'y' or 'n': ");
• bitwise right shift (>>)
scanf("%c",&a);
A= 0 0 0 0 1 1 1 1 (15)
if(a == 'Y' || a == 'y')
A>>2= 0 0 0 0 0 0 1 1 (3) { decrypt (pass);
puts(pass); }
ME 171 Department of Mechanical Engineering, BUET
return 0; }

Bit Field Operators Limitations of bit-field members


• Compare the programs: Limitations:
struct birth {int day; int month; int year;}date; //without bit field operator • bit-field can not take any manual input by scanf or other.
date.day=16;date.month=03;date.year=2020; • bit-field can only be used in custom data type.
printf(“Today is %d-%d-%d",date.day, date.month, date.year);
Outout: Today is 16-03-2020
Practice Task : What will be the output of the following code segment?
Total Memory Consumption:
struct birth
day (4 bytes) month (4 bytes) year (4 bytes) {int day:3;
int month:2;
struct birth {int day:4; int month:4; int year;}date; //using bit field operator
int year:4;}
date.day=16;date.month=03;date.year=2020;
date; date.day=31;
printf(" Today is %d-%d-%d ",date.day,date.month, date.year);
date.month=10;
Total Memory Consumption:
date.year=2018;
day (4 bits) month (4 bits) year (4 bytes)
Output: Today is 0-03-2020
printf(" Today is %d-%d-%d ",date.day,date.month,date.year);
• If day:5, Output: Today is -16-03-2020
• If day:6, Output: Today is 16-03-2020

ME 171 Department of Mechanical Engineering, BUET ME 171 Department of Mechanical Engineering, BUET

You might also like