0% found this document useful (0 votes)
11 views16 pages

Lecture 05

Uploaded by

imalshaamashan
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)
11 views16 pages

Lecture 05

Uploaded by

imalshaamashan
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/ 16

Fundamentals of Programming

COSC 11023 / COST 11023

Operators in C

Sachintha Pitigala
© 2023
Expressions in C
● Most of the C statements are expressions.

● Expression is a combinations of operators and operands.


x

● Example:

2
Assignment Operator (=)
● In C, equal sign (=) is called as the assignment operator.

● It is different from the mathematical equal sign.

● It assigns the values in the right hand side to the left


hand side variable.
int x;
x = 12;
● Above statement sets the value of x to 12.
3
Assignment Operator (=)
● Since the assignment is also an expression, we can combine
several assignments as illustrated in the following example:

int x, y, z;
x = y = z = 12;

● Also, variables can be declared and initialized in one


statement:

float n = 4.5;
4
Arithmetic Operators
● Arithmetic operators are used to perform calculations in
C.

● The arithmetic operators available in C:


■ + Addition
■ - Subtraction
■ * Multiplication
■ / Division
■ % Modulus
5
Using Arithmetic Operators
● Arithmetic operators are used with two operands.

● The portion to the right of the operator is the expression.

● Example statements:
○ What does each one mean?
■ cost = price + tax;
■ owed = total - discount;
■ area = l * w;
■ one_eighth = 1 / 8;
■ r = 5 % 2;
■ x = -y;
6
The Modulus Operator
● The modulus operator (%) returns the remainder rather
than the result of division.

● Examples:
○ 9 % 2 would result in 1.
○ 10 % 2 would result in 0.
○ 20 % 6 would result in 2.

7
The Negation Operator
● The negation operator used to flip the sign of a number.

● It is a unary operator.

● Examples:
int x = 10;
printf (“%d\n” , -x);

output:
-10 8
Incrementing and Decrementing Operators
● Adding one to a variable is called incrementing.

● In C, you increment with the ++ operator.

● Subtracting one from a variable is called decrementing.

● In C, you decrement with the -- operator.

9
Incrementing and Decrementing Operators
● Variation if increment and decrement (Post
increment/decrement and Pre increment/decrement)
k = j++;
○ In the case of the statement above, k is assigned the value
of the variable j before j is incremented.

k = ++j;

○ In this second example, k is assigned the value of j after j is


incremented.
10
Incrementing and Decrementing Operators
● For example, if n is 5, then
x = n++;
is equivalent
x = n;
n++;
but
x = ++n;
is equivalent
n++;
x = n;
● The increment and decrement operators can only be applied to
variables; an expression like (i+j)++ is illegal. 11
Operator Precedence
● Consider the following arithmetic operation:
○ left to right
6/2*1+2=5

○ right to left
6/2 * 1 + 2 = 1

● using parentheses
= 6 / (2 * 1) + 2
= (6 / 2) + 2
=3+2
=5 12
Order of Operators (Operator Precedence)
•The following table operators are listed top to bottom, in
descending precedence.

Precedence Operator Description Associativity

1 () Parenthesis Left to right

2 (+), (-), ++, - - Unary plus or minus, prefix increment or Right to left
decrement

3 */% Multiplication, division, modulus Left to right

4 +- Addition or subtraction Left to right


13
Parenthesized Expressions
● What if the precedence order is not what you want?

example: sale1 + sale2 * TAXRATE

● in this case the multiplication would happen first, but you might
want the addition first

● answer: parenthesize the expressions you want to happen first


result: (sale1 + sale2) * TAXRATE

● parenthesized expressions have highest precedence 14


Programming Tip: Parenthesizing
● Does not hurt to include parentheses for all expressions

● Thus you can guarantee the order of evaluation

● Example:
total = totalSale + TAXRATE * totalSale
becomes
total = (totalSale + (TAXRATE * totalSale))
15
Operator Precedence - Examples

16

You might also like