0% found this document useful (0 votes)
24 views26 pages

CSC 183 Chap-4

This document covers Chapter 4 of a C programming course, focusing on operators and expressions. It details various types of operators in C, including arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special operators, along with examples and exercises. Additionally, it discusses operator precedence, associativity, expression evaluation, and type conversion in C programming.

Uploaded by

sabbir565455
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views26 pages

CSC 183 Chap-4

This document covers Chapter 4 of a C programming course, focusing on operators and expressions. It details various types of operators in C, including arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special operators, along with examples and exercises. Additionally, it discusses operator precedence, associativity, expression evaluation, and type conversion in C programming.

Uploaded by

sabbir565455
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

CSC – 183

Programming C
Chapter - 4

Operators and Expressions

March 8, 2025 CSC-183 1


Today’s Outline:
• C operators
• Operator Precedence & Associativity
• C Expression Evaluation
• Type Conversion in Expressions

March 8, 2025 CSC-183 2


C Operators
• An operator is a symbol that tells the computer to perform certain
mathematical or logical manipulations.

• Operators used in programs to manipulate data & variables.

• C is very rich in built-in operators, classified into following number of


categories:
1. Arithmetic
2. Relational
3. Logical
4. Assignment
5. Increment/Decrement
6. Conditional
7. Bitwise
8. Special
March 8, 2025 CSC-183 3
C Operators – Arithmetic Operators
• Following table lists C’s arithmetic operators:
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulus

• Integer division(/) truncates any fractional part (remainder).


• Modulus operator (%) can not used be used on floating-point
data. During modulus operation, sign of the result is always the
sign of the first operand.
• Example:
- 11 % 2 = - 1
- 11 % 2 = - 1
OPERAND: 11 % 2 = REMAINDER: 1
March 8, 2025 CSC-183 4
C Operators – Arithmetic Operators (Cont.)
• Application of modulus operator
– From total days find out months and rest days.
total_days = 66 days.
= 2 months and 6 days
– Find out a number is odd or even by checking its reminder.
– Determine a number is divided by a specific number.
– If(6%3==0){printf(“even number”);}
• Exercise (Home work):
– Write a program, it takes days as input and determine numbers
of years, months, weeks and rest days.

March 8, 2025 CSC-183 5


C Operators – Relational Operators
• Sometimes need to compare two quantities, depending on their
relation, to take certain decisions.
• These comparisons can be done with the help of relational
operators.
– Compare: age of two persons, value of two number.
• Expressions that use relational operators return 0 for false & 1 for
true.
Operator Meaning
> Is greater than
>= Is greater than or equal to
< Is less than
<= Is less than or equal to
== Is equal to
!= Is not equal to

• Example: marks > 89 , 10<20 is true, 20<10 is false

March 8, 2025 CSC-183 6


C Operators – Relational Operators
• Exercise:
4.5 <= 10 ?
4.5 < -10 ?
-35 >= 0 ?
10 < (7+5) ?
(a+b) < (c+d) ?
a=0 b = 6 c= 5 d = 2
6 < 7 NO

4.5 <= 10 TRUE


4.5 < -10 FALSE
-35 >= 0 FALSE
10 < (7+5) TRUE
(a+b) < (c+d) ? (depends on value of a,b,c,d)
March 8, 2025 CSC-183 7
C Operators – Relational Operators (Program)
To find out the large number between two number:

#include<stdio.h>
Int main()
{
int num1,num2;
num1 = 25;
num2 = 20;
if(num1 > num2)
{
printf(“num1 is large”);
}
else
{
printf(“num2 is large”);

}
return 0;
}
March 8, 2025 CSC-183 8
C Operators – Logical Operators
• Logical refers to the ways relationships can be connected.
• Used when need more than one condition to take decision.
• Expressions that use logical operators return 0 for false & 1 for true.
• The truth table for logical operators is shown below:

Operator Meaning P Q P && Q P || Q !P


0 0 0 0 1
&& AND
0 1 0 1 1
|| OR
1 1 1 1 0
! NOT
1 0 0 1 0
• Some examples:
To check age between 18 to 30
if( age > 18 && age<30 ) allow to apply for job
To check Grade:
if( marks > 0 && marks < 60) then Grade is F
March 8, 2025 CSC-183 9
Leap Year Program: using logical opeators
#include<stdio.h>

int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if((year%4 == 0) && (year%100 != 0) || (year%400 == 0))
{
printf("\n%d is a leap year",year);
}
else
{
printf("\n%d is not a leap year",year);
}
return 0;
}
March 8, 2025 CSC-183 10
C Operators – Assignment Operator
• ‘=‘ used to assign the result of an expression to a variable.
General form: variable_name = expression; c = a+b
• Multiple assignments: x = y = z = 0;
• Compound assignments: The statement of the form var = var
operator expression can be written as var operator =
expression
e.g. x = x + 1 can written as x += 1
• It also called shorthand assignment operator.
Operator Operator Example
+= X=X+1 X += 1
-= X=X-1 X -= 1
*= X=X*Y X *= Y
/= X=X/Y X /= Y
%= X=X%Y X %= Y

March 8, 2025 CSC-183 11


C Operators – Increment/Decrement
• ‘ ++ ’ adds 1 to its operand and ‘ -- ’ subtracts 1 from its operand.
• x = x +1 is same as ++x or x++ and x=x -1 same as --x or x--
• Both the increment and decrement operator may either in prefix or
postfix form.
• When postfix (x++ or x--) is used with a variable in an expression, the
expression is evaluated first using the original value of the variable and
then the variable is incremented (or decremented) by one. i.e
• m = 5; y = m++;
• m=? y=?
• Result: m = 6; y= 5
• When prefix (++x or --x) is used in an expression, the variable is
incremented (or decremented) first and then the expression is evaluated
using new value of the variable.
• m = 5; y = ++m;
• m=? y=?
• Result: m = 6 ; y = 6

March 8, 2025 CSC-183 12


C Operators – Conditional Operator
• Also called ternary operator “ ?: ”
• General form: exp1 ? exp2 : exp3
– exp1, exp2, exp3 are expression. i.e. a + b, a < b, a == b
• Operation same as if-then-else statement

a = 10; if (a > b)
x = a;
b = 15;
else
x = ( a > b) ? a : b; x = b;

March 8, 2025 CSC-183 13


C Operators – Bitwise Operators
• To manipulate data bit level, bitwise operators used.
• Bitwise operation refers to testing, setting, or shifting the
actual bits in a byte or word, which correspond to the
standard char or int data types.
• Bitwise operator can not be used on float, double, long
double or void data types.
Operator Meaning
& AND
| OR
^ XOR
~ One’s Complement (NOT)
>> Shift right
<< Shift left
March 8, 2025 CSC-183 14
C Operators – Special Operators
• Comma operator: (,)
Comma operator can be used to link the related expression
together.
Ex: int a, b, c; a=1, b=2, c=3;
• Sizeof operator: Returns the number of bytes, occupied by
operand.
m = sizeof(sum);
i = sizeof(int);
• Pointer operator: ( & and * )
• Member selection operator: ( . and -> )
• Exercise: Example 3.3

March 8, 2025 CSC-183 15


Arithmetic Expressions:
• An arithmetic expression is a combination of variables,
constants and operators and function calls by maintaining C
syntax.
• C does not have an operator for exponentiation.
• Expression represent in C:

Algebraic expression C expression


a x b -c a*b-c
(m+n)(x+y) (m+n)*(x+y)
3x2+2x+1 3*x*x + 2*x + 1
 x
  c
 y 
x/y+c

March 8, 2025 CSC-183 16


March 8, 2025 CSC-183 17
Precedence and Associativity
• Precedence: Decides the order in which different operators are
evaluates or calculated. Give priority to operators.
• x = 9 - 12 / 3 + 3 * 2 - 1
• x=9-4+3*2-1
• x=9-4+6-1
• x=5+6-1
• x = 11- 1
• x = 10
• Associativity: Decide the calculation order of expression when
multiple operators have same precedence in an expression.
Decides Left to Right or Right to Left evaluation.
• x=3*2*2+5*2 (Left to Right)
• x=6*2+5*2
• x = 12 + 5 * 2
• x = 12+ 10
• x = 22
March 8, 2025 CSC-183 18
Evaluation of Expressions:
• variable = expression;
x = a – b / 3 + c * 2 – 1;
y = a – b / (3 + c) * (2 – 1);
z = a – (b / (3 + c) * 2) – 1;

If a = 9 , b = 12, c = 3; what is value of x, y, z?

x = 10
y=7
z=4

March 8, 2025 CSC-183 19


March 8, 2025 CSC-183 20
March 8, 2025 CSC-183 21
March 8, 2025 CSC-183 22
C Type Conversion: Two Types
• Implicit type conversion: C automatically converts any
intermediate values to the proper type so that the expression
can be evaluated without loosing any significance, called
implicit type conversion.
• Rule: If the operands are of different types, the ‘lower type’ is
automatically converted to ‘higher type’ before operation
proceeds.
• int i,x;
• float f;
• double d;
• x=i*f+d
» i automatically converted to float type, then multiplication performs.
» Then (i * f) converted to double and add with d.
» Then final result will converted to int type, data type of x.

March 8, 2025 CSC-183 23


C Type Conversion: Two Types
• Explicit type conversion: When we force an expression to be
a specific type, called explicit type conversion.
• General form: (data_type) expression;
• e.g. int x;
float y = 20.0;
x = (int) (y + 10.5);
• a = (int) 21.3 / (int)4.5 equivalent to a = 21/4 = 5
• x = (int) a + b [a converted to integer and add with b]

March 8, 2025 CSC-183 24


March 8, 2025 CSC-183 25
March 8, 2025 CSC-183 26

You might also like