0% found this document useful (0 votes)
3 views

Lesson 02 - Operators in C

Uploaded by

keithtran31201
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lesson 02 - Operators in C

Uploaded by

keithtran31201
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Operators in C

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 1


Lesson Objectives

 Arithmetic operators
 Relational operators
 Logical operators
 Bitwise operators
 Assignment operators
 Conditional operators
 Special operators

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 2


Operators in C

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 3


Section 1
ARITHMETIC OPERATORS

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 4


Arithmetic operators (1)

C supports all
the basic
arithmetic
operators.
The following
table shows
all the basic
arithmetic
operators.

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 5


Arithmetic operators (2)

These are the operators used to perform arithmetic/mathematical


operations on operands. Examples: (+, -, *, /, %,++,–). Arithmetic
operator are of two types:

* Unary Operators: Operators that operates or works with a single


operand are unary operators. For example: (++ , –)

* Binary Operators: Operators that operates or works with two operands


are binary operators. For example: (+ , – , * , /)

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 6


Arithmetic operators (3)
#include <stdio.h>
int main()
{
int a = 10, b = 4, res;
printf("a is %d and b is %d\n", a, b); // printing a and b
res = a + b; // addition
printf("a+b is %d\n", res);
res = a - b; // subtraction
printf("a-b is %d\n", res);
res = a * b; // multiplication
printf("a*b is %d\n", res);
res = a / b; // division
printf("a/b is %d\n", res);
res = a % b; // modulus
printf("a%b is %d\n", res);
return 0;
}

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 7


Arithmetic operators (4)
#include <stdio.h>
int main()
{
int a = 10, b = 4, res;
// post-increment example: res is assigned 10 only, a is not updated yet
res = a++;
printf("a is %d and res is %d\n", a, res); // a becomes 11 now
// post-decrement example: res is assigned 11 only, a is not updated yet
res = a--;
printf("a is %d and res is %d\n", a, res); // a becomes 10 now
// pre-increment example: res is assigned 11 now since a is updated here itself
res = ++a;
// a and res have same values = 11
printf("a is %d and res is %d\n", a, res);
// pre-decrement example: res is assigned 10 only since a is updated here itself
res = --a;
// a and res have same values = 10
printf("a is %d and res is %d\n", a, res);
return 0;
}

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 8


Section 2
RELATIONAL OPERATORS

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 9


Relational operators (1)

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 10


Relational operators (2)
#include <stdio.h>
int main()
{
int a = 21, b = 10, c;
if( a == b ) { printf("Line 1 - a is equal to b\n" );}
else { printf("Line 1 - a is not equal to b\n" );}
if ( a < b ) { printf("Line 2 - a is less than b\n" );}
else { printf("Line 2 - a is not less than b\n" );}
if ( a > b ) { printf("Line 3 - a is greater than b\n" );}
else { printf("Line 3 - a is not greater than b\n" );}
/* Lets change value of a and b */
a = 5; b = 20;
if ( a <= b ) {
printf("Line 4 - a is either less than or equal to b\n" );}
if ( b >= a ) {
printf("Line 5 - b is either greater than or equal to b\n" );}
return 0;
}
7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 11
Section 3
LOGICAL OPERATORS

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 12


Logical Operators (1)

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 13


Logical Operators (2)

#include <stdio.h>
int main()
{
int a = 5, b = 20, c ;
if ( a && b ) { printf("Line 1 - Condition is true\n" );}
if ( a || b ) { printf("Line 2 - Condition is true\n" );}
/* lets change the value of a and b */
a = 0; b = 10;
if ( a && b ) { printf("Line 3 - Condition is true\n" );}
else { printf("Line 3 - Condition is not true\n" );}
if ( !(a && b) ) { printf("Line 4 - Condition is true\n" );}
return 0;
}

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 14


Section 4
BITWISE OPERATORS

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 15


Bitwise Operators (1)

Assume A = 60
and B = 13 in
binary format,
they will be as
follows −
A = 0011 1100
B = 0000 1101

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 16


Bitwise Operators (2)
#include <stdio.h>
int main()
{
// a = 5(00000101), b = 9(00001001)
unsigned char a = 5, b = 9;
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a & b); // The result is 00000001
printf("a|b = %d\n", a | b); // The result is 00001101
printf("a^b = %d\n", a ^ b); // The result is 00001100
printf("~a = %d\n", a = ~a); // The result is 11111010
printf("b<<1 = %d\n", b << 1); // The result is 00010010
printf("b>>1 = %d\n", b >> 1); // The result is 00000100
return 0;
}
7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 17
Section 5
ASSIGNMENT OPERATORS

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 18


Assignment Operators (1)

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 19


Assignment Operators (2)
#include <stdio.h>
int main() {
int a = 21, c;
c = a; printf("Line 1 - = Operator Example, Value of c = %d\n", c ); OUTPUT:
c += a; printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a; printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a; printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /= a; printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
c = 200;
c %= a; printf("Line 6 - %= Operator Example, Value of c = %d\n", c );
c <<= 2; printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );
c >>= 2; printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );
c &= 2; printf("Line 9 - &= Operator Example, Value of c = %d\n", c );
c ^= 2; printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );
c |= 2; printf("Line 11 - |= Operator Example, Value of c = %d\n", c );
return 0;
}

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 20


Section 6
CONDITIONAL OPERATOR

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 21


Conditional operator (1)

The conditional operators in C language are known by two more names


+) Ternary Operator
+) ? : Operator
It is actually the if condition that we use in C language decision making,
but using conditional operator, we turn the if condition statement into a
short and simple operator.
The syntax of a conditional operator is :

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 22


Conditional operator (2)

The question mark "?" in the syntax represents the if part.


The first expression (expression 1) generally returns either true or false, based on
which it is decided whether (expression 2) will be executed or (expression 3)
If (expression 1) returns true then the expression on the left side of " : " i.e
(expression 2) is executed.
If (expression 1) returns false then the expression on the right side of " : " i.e
(expression 3) is executed.

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 23


Section 7
SPECIAL OPERATORS

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 24


Special Operators (1)

sizeof operator: sizeof is a much used in the C/C++ programming language. It is a compile
time unary operator which can be used to compute the size of its operand. The result of
sizeof is of unsigned integral type which is usually denoted by size_t. Basically, sizeof
operator is used to compute the size of the variable. To learn about sizeof operator in details
you may visit this link.

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 25


Special Operators (2)

Comma Operator: The comma operator (represented by the token ,) is a binary operator
that evaluates its first operand and discards the result, it then evaluates the second operand
and returns this value (and type). The comma operator has the lowest precedence of any C
operator. Comma acts as both operator and separator. To learn about comma in details visit
this link.

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 26


Special Operators (3)

Conditional Operator: Conditional operator is of the form Expression1 ? Expression2 :


Expression3 .Expression1 is the condition to be evaluated. If the condition(Expression1) is
True then we will execute and return the result of Expression2 otherwise if the
condition(Expression1) is false then we will execute and return the result of Expression3. We
may replace the use of if..else statements by conditional operators.

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 27


Special Operator: Ternary Operator

#include <iostream>
using namespace std;

int main()
{
int test = 0; s
cout << "First character " << '1' << endl;
cout << "Second character " << (test ? 3 : '1')
<< endl;

return 0;
}

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 28


Operator precedence (1)
Precedence of operator decreases from top to bottom.

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 29


Operator precedence (2)

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 30


Operator precedence (3)

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 31


Operator precedence (4)

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 32


Thank you
Q&A

7/19/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 33

You might also like