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

Lecture-4.1-C Operators and Expression

DIU CSE PPS Lectures

Uploaded by

Toaha Siddique
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)
3 views

Lecture-4.1-C Operators and Expression

DIU CSE PPS Lectures

Uploaded by

Toaha Siddique
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/ 27

Operators And

Expression
Operators and
Expressions
Consider the expression A + B * 5 , where,
 +, * are operators,
 A, B are variables,
 5 is constant,
 A, B and 5 are called operand, and
 A+B*5 is an expression.
Types of C operators

C language offers many types of operators, such


as:
Arithmetic operators
Assignment operators
Increment/decrement operators
Relational operators
Logical operators
Bit wise operators
Conditional operators (ternary operators)
Special operators
Arithmetic
operators

Arithmetic Operators Assignment operators


Inc/dec operators
Relational operators
Logical operators
Bit wise operators
Conditional operators
Special operators
C Arithmetic operators are used to perform
mathematical calculations like addition, subtraction,
multiplication, division and modulus in C programs.
Arithmetic

Arithmetic Operators operators


Assignment operators
Inc/dec operators
Relational operators
Logical operators
 There are three types of arithmetic operations using Bit wise operators
Conditional operators
arithmetic operators: Special operators

① Integer arithmetic : when all operands are integer. If a=15 , b=10,


 a + b =25
 a / b =1 (decimal part)
 a % b =5 (remainder of division)
② Real arithmetic : All operands are only real number. If a=15.0 ,
b=10.0
 a / b = 1.5
③ Mixed model arithmetic : when one operand is real and another is
integer. If a=15 and b= 10.0
 a / b = 1.5 whereas, 15/10=1

 Note: The modulus operator % gives you the remainder when


two integers are divided: 1 % 2 is 1 and 7 % 4 is 3.
 The modulus operator can only be applied to integers.
Arithmetic

Arithmetic Operators
operators
Assignment operators
Inc/dec operators
Relational operators
Logical operators
Bit wise operators
① Integer arithmetic : Conditional operators
Special operators
When an arithmetic operation is performed on two
whole numbers or integers than such an operation
is called as integer arithmetic.
It always gives an integer as the result.
Let x = 27 and y = 5 be 2 integer numbers. Then
the integer operation leads to the following results.
x + y = 32
x – y = 22
x * y = 115
x % y = 2
x / y = 5
Arithmetic

Example program for C operators


Assignment operators
Inc/dec operators

arithmetic operators Relational operators


Logical operators
Bit wise operators
Conditional operators
Special operators

Output:
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
Arithmetic

Arithmetic Operators operators


Assignment operators
Inc/dec operators
Relational operators
Logical operators
Bit wise operators

② Real arithmetic : Conditional operators


Special operators

When an arithmetic operation is preformed on two


real numbers or fraction numbers such an
operation is called real or floating point arithmetic.
The modulus (remainder) operator is not applicable
for floating point arithmetic operands.
Let x = 14.0 and y = 4.0 then
x + y = 18.0
x – y = 10.0
x * y = 56.0
x / y = 3.50
Arithmetic

Arithmetic Operators operators


Assignment operators
Inc/dec operators
Relational operators
Logical operators
Bit wise operators

③ Mixed mode arithmetic :


Conditional operators
Special operators

When one of the operand is real and other is an


integer and if the arithmetic operation is carried
out on these 2 operands then it is called as mixed
mode arithmetic.
If any one operand is of real type then the result
will always be real
Let x = 15 and y = 10.0 then
x / y = 1.5
Note that: 15 / 10 = 1 (since both of the operands
are integer)
Arithmetic operators

Assignment Operators Assignment


operators
Inc/dec operators
 In C programs, values for the variables are assigned Relational operators
Logical operators
using assignment operators. Bit wise operators
Conditional operators
 For example, if the value “10″ is to be assigned for the Special operators
variable “sum”, it can be assigned as sum = 10;

Shorthand or
Increment and Decrement Arithmetic operators
Assignment operators
Inc/dec operators

Operators Relational operators


Logical operators
Bit wise operators
Conditional operators
There are two more shorthand operators: Special operators

 Decrement __
 Increment ++

These two operators are for incrementing and


decrementing a variable by 1.
For example, the following code increments i by 1
and decrements j by 1.
Arithmetic operators

Increment and Decrement Assignment operators


Inc/dec operators
Relational operators

Operators Logical operators


Bit wise operators
Conditional operators
Special operators

 The ++ and - - operators can be used in prefix or suffix


mode, as shown in Table
Arithmetic operators

Increment and Decrement Assignment operators


Inc/dec operators
Relational operators

Operators Logical operators


Bit wise operators
Conditional operators
Special operators

If the operator is before (prefixed to) the variable,


the variable is incremented or decremented by 1,
then the new value of the variable is returned.
If the operator is after (suffixed to) the variable, then
the variable is incremented or decremented by 1,
but the original old value of the variable is returned.
Therefore, the prefixes ++x and --x are referred to,
respectively, as the preincrement operator and the
predecrement operator; and the suffixes x++ and x
-- are referred to, respectively, as the postincrement
operator and the postdecrement operator.
Arithmetic operators

Increment and Decrement Assignment operators


Inc/dec operators
Relational operators
Operators Logical operators
Bit wise operators
Conditional operators
Special operators

The prefix form of ++ (or --) and the suffix form of ++ (or --) are the
same if they are used in isolation, but they cause different effects
when used in an expression. The following code illustrates this:

In this case, i is incremented by 1, then the old value of i is returned


and used in the multiplication. So newNum becomes 100. If i++ is
replaced by ++i as follows,

i is incremented by 1, and the new value of i is returned and used in


the multiplication. Thus newNum becomes 110.
Arithmetic operators
Exercise on ++ and - - Assignment operators
Inc/dec operators
Relational operators
Logical operators
Bit wise operators
Conditional operators
Special operators

 int x=2 , y = 5 , z = 0;
 x++ ; y++ ;
 x=y++ + x++;
 y=++y + ++x;
 y=++y + x++;
 y += ++y;
 y += 1 + (++x);
 y += 2 + x++;
Arithmetic operators
Assignment operators
Inc/dec operators
Relational
Relational Operators operators
Logical operators
Bit wise operators
Conditional operators
Special operators
 Relational operators are used to find the relation between
two variables. i.e. to compare the values of two variables.
Exercise
 int i=10, j=20, k=30;

 float f=5.5;

 char ch=‘A’;

• i<j
• (j+k)>=(i+k)
• i+f <=10
• i+(f <=10)
• ch==65
• ch >= 10*(i+f)
Arithmetic operators

Logical Operators
Assignment operators
Inc/dec operators
Relational operators
Logical operators
 These operators are used to perform logical Bit wise operators
Conditional operators
operations on the given expressions. Special operators

 There are 3 logical operators in C language. They are,


logical AND (&&), logical OR (||) and logical NOT (!).
Logical Operators
Exercise

 Given that: int a = 5, b = 2, c = 4, d = 6,, e = 3 ;


What is the result of each of the following relational
expressions?
1. a > b
2. a != b
3. d % b == c % b
4. a * c != d * b
5. d * b == c * e
6. a * b < a % b * c
7. c % b * a == b % c * a
8. b % c * a != a * b
9. d % b * c > 5 || c % b * d < 7
10.d % b * c > 5 && c % b * d < 7
Exercise

 For each of the following statements, assign variable names for the
unknowns and rewrite the statements as relational expressions.
1. A customer's age is 65 or more.
2. The temperature is less than 0 degrees and greater than -15
degrees.
3. A person's height is in between 5.8 to 6 feet.
4. The current month is 12 (December).
5. The person's age is 65 or more but less than 100.
6. A number is evenly divided by 4 or 400 but not with 100
7. A person is older than 55 or has been at the company for more
than 25 years.
8. A width of a wall is less than 4 meters but more than 3 meters.
9. An employee's department number is less than 500 but greater
than 1, and they've been at the company more than 25 years.
Arithmetic operators
Example program for Assignment operators
Inc/dec operators
Relational operators
logical operators in C Logical operators
Bit wise operators
Conditional operators
Special operators

Output:
&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as
false
Try this example program
and explain the results

#include<stdio.h>
int main()
{
int a=5, b=-7, c=0, d;
d = ++a && ++b || ++c;
printf("\n %d %d %d
%d",a,b,c,d);
}
Arithmetic operators
Assignment operators

Bit wise Operators Inc/dec operators


Relational operators
Logical operators
Bit wise operators
Conditional operators
Special operators

 One of C’s powerful features is a set of bit manipulation operators. These


permit the programmer to access and manipulate individual bits within a
piece of data to perform bit operations. The various Bitwise Operators
available in C are shown in Figure
 Decimal values are converted into binary values which are the sequence
of bits and bit wise operators work on these bits.
 These operators can operate upon ints and chars but not on floats and
doubles.
Arithmetic operators

Special Operators Assignment operators


Inc/dec operators
Relational operators
Logical operators
Bit wise operators
Conditional operators
Special operators
Example program for
Special operators in C
Example program for
sizeof() operator in C

Output:
Storage size for int data type:4
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data
type:8

You might also like