Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

CSE287

Computer Programming

Lecture 4:
Operators and Expressions in C

Khaled Mahmud Shahriar


Assistant Professor
Department of CSE, BUET Offered to: Department of MME, L-2, T-1
Expressions
• Combination of operators and operands
• Appear on the right side of an assignment statement
Operators
• Depending on the number of operand, operators can be-
• Unary (-a)
• Binary (a-b)
• Ternary (later)
• Depending on the functionality, operators can be-
• Arithmetic
• Bitwise
• Assignment
• Relational
• Logical
• Others
• Operators containing two symbols can not be separated by space.
Arithmetic Operators Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm

Operator Description Type Example


(A=10, B=20)
+ Adds two operands. Binary A + B = 30
Subtracts second operand from the first. Binary A − B = -10

* Multiplies both operands. Binary A * B = 200


Divides numerator by de-numerator. Binary B/A=2
/

Modulus Operator and remainder of after an integer Binary B%A=0


% division.
Increment operator increases the integer value by one. Unary A++ = 11
++ There are two versions: postfix and prefix ++A = 11
Decrement operator decreases the integer value by one. Unary A-- = 9
-- There are two versions: postfix and prefix --A = 9
Unary minus negates the value of the operand Unary -A=-10
-
Sample Code: Postfix and Prefix Operators
#include<stdio.h>
int main()
{
int a,b;
//postfix increment //postfix decrement //convoluted examples
a=5; a=5; b = a++ + a++;
printf("%d\n",a++); printf("%d\n",a--); printf("%d, %d\n", a, b);
b = ++a + ++a;
a=5; a=5; printf("%d, %d\n", a, b);
b=a++;//b=a; a=a+1; b=a--;//b=a; a=a-1; b = (++a) + (++a);
printf("%d %d\n",a,b); printf("%d %d\n",a,b); printf("%d, %d\n", a, b);

//prefix increment //prefix decrement //unary operator


a=5; a=5; a=5;
printf("%d\n",++a); printf("%d\n",--a); b=-a;
printf("%d %d\n",a,b);
a=5; a=5;
b=++a;//a=a+1; b=a; b=--a;//a=a-1; b=a; return 0;
printf("%d %d\n",a,b); printf("%d %d\n",a,b); }
5
Relational Operators Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm

These operators are used for comparison. The result is boolean. All are binary operators.
Example
Operator Description (A=1, B=0)
Checks if the values of two operands are equal or not. If yes, then the condition (A == B) is not true.
== becomes true.
Checks if the values of two operands are equal or not. If the values are not equal, (A != B) is true.
!= then the condition becomes true.

Checks if the value of left operand is greater than the value of right operand. If yes, (A > B) is not true.
> then the condition becomes true.

Checks if the value of left operand is less than the value of right operand. If yes, then (A < B) is true.
< the condition becomes true.

Checks if the value of left operand is greater than or equal to the value of right (A >= B) is not true.
>= operand. If yes, then the condition becomes true.

Checks if the value of left operand is less than or equal to the value of right operand. (A <= B) is true.
<= If yes, then the condition becomes true.
Sample Code: Relational Operators
#include<stdio.h>
int main()
{
int a,b;
//Relational Operators: ==, !=, >, <, <=, >=
a=20;
b=20;
printf("%d\n",a==b);//Output: 1
printf("%d\n",a!=b);//Output: 0
printf("%d\n",a>b);//output: 0
printf("%d\n",a<b);//output: 0
printf("%d\n",a>=b);//output: 1
printf("%d\n",a<=b);//output: 1
return 0;
}
7
Logical Operators Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm

These operators are evaluating logical expressions. The result is boolean

Example
Operator Description Type (A=1, B=0)
Called Logical AND operator. If both the operands Binary (A && B) is
&& are non-zero, then the condition becomes true. false.

Called Logical OR Operator. If any of the two Binary (A || B) is true.


operands is non-zero, then the condition becomes
|| true.

Called Logical NOT Operator. It is used to reverse Unary !(A && B) is


the logical state of its operand. If a condition is true, true.
! then Logical NOT operator will make it false.
Sample Code: Logical Operators
#include<stdio.h>
int main()
{
int a,b,c,d;
//Logical Operators: binary: &&, ||, unary: !
a=-20;
b=10;
c=50;
d=60;
printf("%d\n",a && b);//equivalent: (a!=0) && (b!=0) Output: 1
printf("%d\n",(a>b)&&(c>d));//Output: 0
printf("%d\n",(a>b)||(c>d));//Output: 0
printf("%d\n",!(a<b));//output: 0
return 0;
}
9
Bitwise Operators Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm

These operators are used for bitwise logic operations. The operands must be integer values

Operator Description Example


(A=60, B=30)
Binary AND Operator copies a bit to the result if it exists in both (A & B) = 12, i.e., 0000 1100
& operands.
| Binary OR Operator copies a bit if it exists in either operand. (A | B) = 61, i.e., 0011 1101
Binary XOR Operator copies the bit if it is set in one operand but (A ^ B) = 49, i.e., 0011 0001
^ not both.
~ Binary One's Complement Operator is unary and has the effect (~A ) = ~(60), i.e,. -0111101
of 'flipping' bits.
Binary Left Shift Operator. The left operands value is moved left
<< by the number of bits specified by the right operand. A << 2 = 240 i.e., 1111 0000

Binary Right Shift Operator. The left operands value is moved


>> right by the number of bits specified by the right operand. A >> 2 = 15 i.e., 0000 1111
Truth Table for Bitwise Operators
p ~p p q p&q p|q p^q
0 1 0 0 0 0 0
1 0 0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm
Sample Code: Bitwise Operators
#include<stdio.h>
Explanations of the Bitwise Operations
int main()
a=60 00111100 a=60 00111100
{
b=30 00011110 ------------------------------
char a,b;
------------------------------ ~a= 11000011
//binary: &, |, ^, >>, <<, unary : ~ a&b=28 00011100
a=60; a=60 00111100
b=30; a=60 00111100 -------------------------------
printf("%u\n",a & b);//Output: 28 b=30 00011110 a<<2=240 11110000
printf("%u\n",a | b);//Output: 62 -----------------------------
a|b=62 00111110 a=60 00111100
printf("%u\n",a ^ b);//Output: 34
-------------------------------
a=60 00111100 a>>2=15 00001111
b=30 00011110
printf("%u\n",~a);//Output: ??
----------------------------
printf("%u\n",a<<2); //Output: 240
a^b=34 00100010
printf("%u\n",a>>2); //Output: 15
return 0;
12
}
Assignment Operators Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm

These operators assign the value of the expression on the right to the variable on the left
Operator Description Example
Simple assignment operator. Assigns values from right side operands C = A + B will assign the
= to left side operand value of A + B to C

+= Add AND assignment operator. It adds the right operand to the left C += A is equivalent to
operand and assign the result to the left operand. C=C+A

-= Subtract AND assignment operator. It subtracts the right operand from C -= A is equivalent to C
the left operand and assigns the result to the left operand. =C-A
Multiply AND assignment operator. It multiplies the right operand with C *= A is equivalent to
*= the left operand and assigns the result to the left operand. C=C*A

/= Divide AND assignment operator. It divides the left operand with the C /= A is equivalent to
right operand and assigns the result to the left operand. C=C/A

%= Modulus AND assignment operator. It takes modulus using two C %= A is equivalent to


operands and assigns the result to the left operand. C=C%A
Assignment Operators (2) Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm

Operator Description Example

<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2

>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2

&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2

^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2

|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2


Sample Code: Assignment Operators
#include<stdio.h>
int main()
{
int a,b; a=30;
//Assignment Operators: =, +=, -=, b=60;
*=, /=, %=, >>=, <<=, &=, |=, ^= b&=a;//b=b&a
a=30; printf("%d\n",b);//Output: 28
b=60; b|=a;//b=b|a
b+=a;//b=b+a printf("%d\n",b);//Output: 30
printf("%d\n",b);//Output: 90 b^=a;//b=b^a
b-=a;//b=b-a printf("%d\n",b);//Output: 0
printf("%d\n",b);//Output: 60 b=60;
b*=a;//b=b*a b<<=2;//b=b<<2
printf("%d\n",b);//Output: 1800 printf("%d\n",b);//Output: 240
b/=a;//b=b/a b>>=2;//b=b>>2
printf("%d\n",b);//Output: 60 printf("%d\n",b);//Output: 60
b%=a;//b=b%a return 0;
printf("%d\n",b);//Output: 0 } 15
Other Operators Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm

Operator Description Example

sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.

& Returns the address of a variable. &a; returns the actual address of the variable.

* Pointer to a variable. *a;

?: Conditional Expression. If Condition is true ? then value X : otherwise


value Y
Operator Precedence and Associativity
Consider evaluation of the below expression

Precedence
x= 7/6*4+3/5+3
• If there are a chain of operations, then C
defines which of them will be applied first.
x= 1*4+3/5+3 • Example: *, / and % are higher in precedence that +
and -
x= 4+3/5+3 • Precedence can be altered by using
parentheses
• Innermost parentheses evaluated first
x= 4+0+3 • Example: 6+4/2 = 8 whereas (6+4)/2 = 5
Associativity
x= 4+3 • Evaluation order of operators of same
precedence is determined by associativity
x= 7 • All the operators associate from left to right
except for assignment operators
Precedence Chart Ref: https://www.tutorialspoint.com/cprogramming/c_operators.htm

Category Operator Associativity


Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right
Type Conversion
Type Casting
#include <stdio.h>
int main(){
double f; f=7/(double)2;
f=7/2; printf ("%lf \n", f);
printf ("%lf \n", f);
int x=f;
f=7.0/2; printf ("%d %f\n", x,f);
Implicit printf ("%lf \n", f);
Conversion
return 0;
f=7/2.0; }
printf ("%lf \n", f);

f=(double)7/2;
printf ("%lf \n", f);

For details see: https://www.tutorialspoint.com/cprogramming/c_type_casting.htm


Acknowledgement
All the slides of this course have been prepared by taking help from numerous resources.
The notable contributors are listed below.
1. Content and organization of many pages have been taken from the lecture slides and
codes of the course CSE110 offered to the Department of EEE that were -
i. primarily created by Johra Muhammad Moosa, Assistant Professor (on leave),
CSE, BUET and
ii. later modified by Madhusudan Basak, Assistant Professor, CSE, BUET
2. Most of the wonderful coding examples have been taken from the course CSE281
offered to the Department of BME instructed by Rifat Shahriyar, Professor, CSE,
BUET (course link).
3. search and all the sites that it made available in response to the course
related queries. Some of the sites are: https://geeksforgeeks.org/,
https://www.tutorialspoint.com, https://www.w3schools.com and the list goes on …
Thank You

21

You might also like