CHAPTER 4: BASIC C
OPERATORS
CSEB2113 PROGRAMMING I
Badariah Solemon
BS SEPT2021 Photo by Dillon Shook on Unsplash
MODULES
4.1 Expressions, Operations and
Operators
4.1.1 Expressions and Operations
4.1.2 C Arithmetic Operators
4.1.3 Other Types of Operators
4.2 Type Casting and Rules for
Evaluating Expression
4.2.1 Type Casting
4.2.2 C Rules For Evaluating
Expression
BS SEPT 2021
EXPRESSIONS, OPERATIONS AND
OPERATORS
Module 4.1
BS SEPT2021 Photo by AltumCode on Unsplash
4.1.1 EXPRESSIONS AND OPERATIONS
BS SEPT2021 Photo by Fatos Bytyqi on Unsplash
C EXPRESSIONS
• An expression is a collection of OPERATORS and OPERANDS that represents a
specific value.
• Example:
Operator
5 + 2;
Operands
Operator: Specifies an operation on the data which yields a value
Operand: Data item on which the operator act.
BS SEPT2021
TYPES OF C EXPRESSIONS
p = q + r;
• The entire line is a statement, not an expression.
• The portion after the equal is an expression.
ARITHMETIC Expression RELATIONAL expressions LOGICAL Expressions CONDITIONAL Expressions
• To perform arithmetic • To perform comparison • To perform complex • Works on three operands
operation operations of two operands comparison operations
• Computes a value of type • The result is either Zero • The result is either Zero
int, float or (True) or Non-zero (False) (True) or Non-zero (False)
double
• Using ARITHMETIC • Using RELATIONAL • Using LOGICAL OPERATORS • Using CONDITIONAL
OPERATORS OPERATORS OPERATOR
BS SEPT2021
C PROGRAMMING OPERATORS
ARITHMETIC Operators RELATIONAL Operators
• Binary (operation on 2 operand) • ==
+ - * / % • >
• <
• Unary (operation on 1 operand) • >=
+ - ++ -- • <=
• !=
• Assignment
= += -= *= /= %= LOGICAL Operators
• &&
CONDITIONAL Operator • ||
• ? : • !
Chapter 5
BS SEPT2021
4.1.2 C ARITHMETIC OPERATORS
BS SEPT2021 Photo by JESHOOTS.COM on Unsplash
BINARY OPERATORS D+E
• Require TWO operands to produce a new value
BS SEPT2021
OPERATIONS OF SAME OR MIXED DATA TYPES
Examples:
int 5 + 3 is 8
int
5 / 3 is 1
int
float float 5.2 + 3.1 is 8.3
5.0 / 3.0 is 1.6
float
float 5.2 + 3 is 8.2
float
5.0 / 3 is 1.6
int 5 / 3.0 is 1.6
BS SEPT2021
OPERATIONS OF SAME OR MIXED DATA TYPES D +E
BS SEPT2021
MODULO OPERATOR (%) D+E
• Also known as modulus operator
• Will give the REMAINDER that is left over when one number is divided by
another
BS SEPT2021
PRACTICE
1. Write a C program that sum up 4 integer
numbers and display the computed sum.
2. Write a C program to compute and display
the average of three numbers.
3. Write a C program to read the value of the
height, width and length of a box from the
user and print its volume.
BS SEPT2021
UNARY OPERATORS D+E
• Require ONE operand to produce a new value
BS SEPT2021
PREFIX AND POSTFIX INCREMENT D+E
• a++ or ++a is equals to a = a + 1 but they work differently
BS SEPT2021
PREFIX AND POSTFIX DECREMENT D+E
• Similarly, --a or a-- is equals to a = a - 1 but they work
differently
BS SEPT2021
PRACTICE
• What is the output of the following program?
1.
2.
BS SEPT2021
ASSIGNMENT OPERATORS D+E
• Syntax:
Variable = Expressions;
= assigns values from right side operands to left side operand
BS SEPT2021
COMPOUND ASSIGNMENT OPERATORS D+E
• A COMBINATION of arithmetic operator with the basic assignment operator
BS SEPT2021
PRACTICE
1. What is the output of the following C program?
BS SEPT2021
PRACTICE
2. Write a C program to read a number and
generate a list of 5 numbers with
multiply of 3 values by applying
compound assignment operator.
3. Write a C program to show the following
list of numbers by reading the first
number:
291 277 263 249 235 221
BS SEPT2021
4.1.3 OTHER TYPES OF OPERATORS
BS SEPT2021 Photo by Nick Fewings on Unsplash
RELATIONAL OPERATORS D+R+E
BS SEPT2021
LOGICAL OPERATORS D+R+E
BS SEPT2021
TYPE CASTING AND ORDER OF
OPERATIONS & GROUPING
Module 4.2
BS SEPT2021 Photo by Gayatri Malhotra on Unsplash
4.2.1 TYPE CASTING
BS SEPT2021 Photo by Markus Spiske on Unsplash
TYPE CASTING
• Refers to CAST/CONVERT one datatype into another datatype
• To ensure that you do not loose any data from the arithmetic operation.
• Syntax:
(data type) expression
BS SEPT2021
TYPE CASTING APPLICATION
BS SEPT2021
PRACTICE
1. Write a C program to read two integer values from user and display the
addition, subtraction, multiplication, division and modulo operations table
of the two numbers. Make sure to retain the decimal points of the
division operation. Sample format:
Enter two numbers: x y
C Operator Operations Results
---------------------------------------------
+ Addition z
- Subtraction z
* Multiplication z
/ Division z.zzz
% Modulus z
---------------------------------------------
BS SEPT2021
4.2.2 C RULES FOR EVALUATING EXPRESSION
BS SEPT2021 Photo by Gayatri Malhotra on Unsplash
OPERATOR PRECEDENCE
• Determines which operator is performed first in an expression with more
Contents_Here
than one operators with different precedence. Example Text : Get a
modern PowerPoint
• Example: s = x / y + z; Presentation that is
beautifully designed. I
hope and I believe that
• The rules for evaluating expressions are:
this Template will your
Time, Money and
Reputation.
PRECEDENCE RULE ASSOCIATIVITY RULE
specifies which of the used when two operators of
operators will be same precedence appear in
evaluated first. an expression
BS SEPT2021
PRECEDENCE AND ASSOCIATIVITY RULES IN C
• Depends on IDE
Precedence Operator Associativity
Highest (evaluated [ ] ( ) postfix++ postfix-- Left to Right
first) unary+ unary- ! prefix++ prefix-- Right to Left
* / % Left to Right
binary+ binary- Left to Right
< > <= >= Left to Right
== != Left to Right
&& || Left to Right
?: Right to Left
Lowest (evaluated last) = *= /= %= += -= Right to Left
BS SEPT2021
EXAMPLE
BS SEPT2021
PRACTICE
1. Show the evaluation steps of below C statements (assumed to be
evaluated individually):
int a = 5, b = 4, c = 3, n;
n = c + b * a;
n = (a - c) * a - c;
n = a % c;
n = --b * c-;
n = c / a % ++a –-b;
BS SEPT2021
PRACTICE
2. Write a program to convert temperature in Fahrenheit to Celsius.
3. Write a program to create a table of Olympic competition running
distances in meters, kilometers, yards, and miles given distances in
meters. The following distances should be used: 100m, 200m, 400m, and
800m. (Hint: 1 m = 0.001 km = 1.094 yd = 0.0006215 miles). Sample
output:
BS SEPT2021
RECAP
EXPRESSIONS AND OPERATIONS C ARITHMETIC OPERATORS OTHER TYPES OF OPERATORS
Operator, operand Binary: + - * / % Relational: == >
Expression/operator: Unary: + - ++ -- < >= <= !=
arithmetic, relational, Assignment : = += Logical: && || !
logical, conditional -= *= /= %= Conditional: ? :
TYPE CASTING C RULES FOR EVALUATING EXPRESSION
Data type conversion Precedence Rules
( datatype ) Associativity Rules
IDE-dependent
BS SEPT2021
BS SEPT2021