Chapter 4: Basic C Operators: Cseb2113 Programming I
Chapter 4: Basic C Operators: Cseb2113 Programming I
OPERATORS
CSEB2113 PROGRAMMING I
Badariah Solemon
5 + 2;
Operands
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
OPERATIONS OF SAME OR MIXED DATA TYPES
Examples:
int 5 + 3 is 8
int
5 / 3 is 1
int
BS SEPT2021
OPERATIONS OF SAME OR MIXED DATA TYPES D +E
BS SEPT2021
MODULO OPERATOR (%) D+E
BS SEPT2021
PRACTICE
1. Write a C program that sum up 4 integer
numbers and display the computed sum.
BS SEPT2021
PREFIX AND POSTFIX INCREMENT D+E
BS SEPT2021
PREFIX AND POSTFIX DECREMENT D+E
BS SEPT2021
PRACTICE
• What is the output of the following program?
1.
2.
BS SEPT2021
ASSIGNMENT OPERATORS D+E
• Syntax:
Variable = Expressions;
BS SEPT2021
COMPOUND ASSIGNMENT OPERATORS D+E
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.
BS SEPT2021
LOGICAL OPERATORS D+R+E
BS SEPT2021
TYPE CASTING AND ORDER OF
OPERATIONS & GROUPING
Module 4.2
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
BS SEPT2021
4.2.2 C RULES FOR EVALUATING 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: ? :
BS SEPT2021
BS SEPT2021