Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Module2 Chapter1

Module 2 chapter 1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Module2 Chapter1

Module 2 chapter 1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

MODULE-2

2.1 Operators and Expressions


 Operator: Operator is a symbol (or token) that specifies the operation to be performed on
various types of data.
 Operand: A constant or variable or function which returns a value is an operand.
 Expression: A sequence of operands and operators that reduces to a single value is an
expression.
Operands

a + b Expression

a _ b

Operators

2.1.1 Classification of operators


The operators in C can be classified based on:
 The number of operands an operator has.
 The type of operation being performed.

1. Classification of operators based on the number of operands


Unary operators Ex: ++,-- etc.

Operators Binary operators Ex: +,-,*,/ etc.

Ternary operators Ex: ? and :

i) Unary operator: An operator which acts on only one operand to produce the result is
called Unary operator.
Ex: -10, -a, *b, ++a, a++, b-- etc.

ii) Binary operator: An operator which acts on two operands to produce the result is called
Binary operator.
Ex: a+b, a*b, 10/5 etc

iii) Ternary operator: An operator which acts on three operands to produce the result is
called Ternary operator.
Ex: a ? b : c;

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 1
2. Classification of operators based on type of operation
Arithmetic Operators Ex: +, - , * etc.

Assignment Operators Ex: =, +=

Increment/Decrement Operators Ex: ++, --

Relational Operators Ex: <, >, <=, >=

Types of Operators Logical Operators Ex: &&, ||

Conditional Operators Ex: ?:

Bitwise Operators Ex: &, ^

Special Operators Ex: ,, . ,[ ] , ( )

i) Arithmetic Operators
 The operators that are used to perform arithmetic operations such as addition,
subtraction, multiplication, division and modulus are called arithmetic operators.
 These operators perform operations on two operands and hence they are called binary
operators.

 % (modulus operator) divides the first operand by second and returns the remainder.
 % (modulus operator) cannot be applied to floating or double.
 % operator returns remaining value(remainder) of an integer division.

 *, / and % operators are having higher precedence than +, - operators.

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 2
Arithmetic Operator’s Precedence (Precedence of operators)
 The Arithmetic Expressions are evaluated based on “BODMAS” Rule. (Brackets of Operator
(x(2),[ ]), Order or Power (xy,23 etc.), Division, Multiplication, Addition, Subtraction).

ii) Assignment Operators


 An operator which is used to assign the data or result of an expression into a variable (also
called memory location) is called an assignment operator.
 Assignment operator is denoted by ‘=’ sign.
Ex: a=b; //value of b is copied into variable a

Types of Assignment statements:


a). Simple Assignment Statement

variable= expression;

Ex: a=10;
a=b;
a=a+b;
Area= l*b; //Result of Expression l*b is copied into variable area.

b). Shorthand Assignment Statement


 The operators such as +=, -=, *=, /= and %= are called shorthand assignment operators.
Ex 1: a=a+10; // simple assignment
a+=10; // shorthand assignment
Ex 2: i=i+2;
i+=2;
 If expr1 and expr2 are expressions then
expr1op = expr2 ;
is equivalent to
expr1 = (expr1) op (expr2) ;
i.e., x*=y+1 means x=x*(y+1) ;

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 3
c). Multiple Assignment Statement
 Assigning a value or a set of values to different variables in one statement is called multiple
assignment statement.
 The multiple assignments are used whenever same value has to be copied into various memory
locations.
Ex: int i=10; //simple assignment
int j=10;
int k=10;
int i=j=k=10; //multiple assignment

iii) Increment and Decrement Operators


Increment Operator
 ‘++’ is an increment operator. This is unary operator. It increments the value of a variable
by one.
Post Increment Ex: a++
Increment
Operator Pre Increment Ex: ++a

1. Post Increment
 It increments the value after (post) the operand value is used. i.e., operand value is used
first and then the operand value is incremented by 1.
Ex: void main()
{
int a=20,b; // a=20, b?
b=a++; // b=a=20, a=a+1= 20+1
printf(“%d”,a); // a=21
printf(“%d”,b); // b=20
}

2. Pre Increment
 It increments before (pre) the operand value is used. The operand value is incremented by
1 and this incremented value is used.

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 4
Ex: void main()
{
int a=20,b; // a=20, b?
b=++a; // a=++a=a+1= 20+1, b=a=21
printf(“%d”,a); // a=21
printf(“%d”,b); // b=21
}

Decrement Operator
 '--' is a decrement operator. This is a unary operator. It decrements the value of a variable
by one.
Post Decrement Ex: a--
Decrement
Operator Pre Decrement Ex: --a

1. Post Decrement
 It decrements the value after (post) the operand value is used. i.e., operand value is used
first and then the operand value is decremented by 1.
Ex: void main()
{
int a=20,b; // a=20, b?
b=a--; // b=a=20, a=a-1= 20-1
printf(“%d”,a); // a=19
printf(“%d”,b); // b=20
}

2. Pre Decrement
 It decrements before (pre) the operand value is used. The operand value is decremented by
1 and this decremented value is used.
Ex: void main()
{
int a=20,b; // a=20, b?
b=--a; // a=--a=a-1= 20-1, b=a=19
printf(“%d”,a); // a=19
printf(“%d”,b); // b=19
}

iv) Relational operators


 The operators that are used to find the relationship between two operands are called
relational operators.
 The relationship between the two operand values results in true (always 1) or false (always
0).

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 5
Relational operators available in C are:

 All the relational operators are having a same priority and left to right associativity.
 The relational operators have lower precedence than arithmetic operators.

Equality operators
 C supports two kinds of equality operators to compare their operands for strict equality or
inequality.
equal ==
not equal ! =
 Equality operators have lower precedence than the relational operators.

v) Logical operators
 The operators that are used to combine two or more relational expressions are called
logical operators.
 The output of relational expression is true or false, the output of logical expression is also
true or false.

Logical operators available in C:

Description Operators Priority Associativity


not ! 1 Left to Right
and && 2 Left to Right
or !! 3 Left to Right

Operand1 Operand2 AND(&&) OR(||) NOT(!) (Operand1)


True(1) True(1) True(1) True(1) False(0)
True(1) False(0) False(0) True(1) False(0)
False(0) True(1) False(0) True(1) True(1)
False(0) False(0) False(0) False(0) True(1)

Logical NOT: The logical NOT operator is denoted by ‘!’. The output of not operator can be true or
false. The result is true if the operand value is false and the result is false if the operand is true.

Logical AND: The logical AND operator is denoted by ‘&&’. The output of and operator is true if
both the operands are evaluated to true. If one of the operand is evaluated false, the result is false.

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 6
Logical OR: The logical OR operator is denoted by ‘||’. The output of or operator is true if and only
if at least one of the operands is evaluated to true. If both the operands are evaluated to false, the
result is false.

Ex: 1. If a, b, c are 3 sides of a triangle, then if a==b && b==c && c==a then triangle is equilateral
otherwise not an equilateral triangle.
2. If a, b, c are 3 sides of a triangle then if a==b || b==c || c==a then triangle is isosceles triangle
otherwise not a isosceles triangle.
3. int a=10, b:
b =!a ;
Output: value of b=0, because a = 10 then !a=0 and !a value assigned to b.

vi) Conditional Operator


 The conditional operator is also called as 'ternary operator'.
 An operator that operates on the three operands is called ternary operator.
Syntax:
(expr1)? expr2: expr3;

Where,
- expr1 is evaluated first.
- If expr1 is evaluated to true, then expr2is evaluated.
- If expr1 is evaluated to false, then expr3is evaluated.
Example:
1. Write a C program to find the biggest of two numbers using conditional operator.
#include<stdio.h>
void main()
{
int a,b,big;
printf(“Enter the values of a and b:”);
scanf(“%d%d”,&a,&b);
big=(a>b)?a:b;
printf(“Big=%d”,big);
}

2. Write a C program to find the smallest of two numbers using conditional operator.
#include<stdio.h>
void main()
{
int a,b,small;
printf(“Enter the values of a and b:”);
scanf(“%d%d”,&a,&b);
small=(a<b)?a:b;
printf(“Small=%d”,big);
}

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 7
vii) Bitwise Operators
 The operators that are used to manipulate the bits of given data are called bitwise
operators.

Bitwise operators available in C are:

 These may only be applied to integral operand. i.e., char, short, int and long whether signed or
unsigned.

a. One’s Complement(~)
 The operator that is used to change every bit from 0 to 1 and 1 to 0 in the specified operand
is called One’s complement operator.
Truth table of One’s Complement(~):
Op1 ~Op1
0 1
1 0

 One’s complement operator is denoted by ‘~(tilde)’ symbol.

Ex: Write a C program to show the usage of Bitwise Negate operator.


#include<stdio.h>
void main()
{
int a=10,b;
b=~a;
printf(“~%d=%d”,a,b);
getch();
}

Output: ~10=245
Binary Representation:
10 = 0 0 0 0 1 0 1 0
245= 1 1 1 1 0 1 0 1

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 8
b. Left shift operator (<<)
 The operator that is used to shift the data by a specified number of bit positions towards
left is called ‘left shift operator’.
Syntax:
b=a<<num;
Ex: Write a C program to show the usage of Left Shift operator.
#include<stdio.h>
void main()
{
int a=5,b;
b=a<<1;
printf(“%d<<1=%d”,a,b);
}
Output: 5<<1=10
MSB is discarded

0 0 0 0 0 1 0 1 a=5

b=10
0 0 0 0 1 0 1 0

0 is appended at LSB

c. Right shift operator (>>)


 The operator that is used to shift the data by a specified number of bit positions towards
right is called ‘right shift operator’.
Syntax:
b=a>>num;
Ex: Write a C program to show the usage of Right Shift operator.
#include<stdio.h>
void main()
{
int a=10,b;
b=a>>1;
printf(“%d>>1=%d”,a,b);
}
Output: 10>>1=5
LSB is discarded

0 0 0 0 1 0 1 0 a=10

0 0 0 0 0 1 0 1 b=5
0 is appended at MSB

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 9
d. Bit-wise AND (&)
 If the corresponding bit positions in both the operands are 1, then AND operation results in
1, otherwise AND operation results in 0.
Truth table of Bit-wise AND (&):
Op1 Op2 Op1&Op2
0 0 0
0 1 0
1 0 0
1 1 1
Ex: Write a C program to show the usage of ‘&’ operator.
#include<stdio.h>
void main()
{
int a=10,b=6;
c=a&b;
printf(“%d&%d=%d”,a,b,c);
}
Output: 10&6=2

Binary Representation:
10 = 0 0 0 0 1 0 1 0
06 = 0 0 0 0 0 1 1 0
02 = 0 0 0 0 0 0 1 0

e. Bit-wise OR (|)
 If the corresponding bit positions in both the operands are 0, then OR operation results in
0, otherwise OR operation results in 1.

Truth table of Bit-wise OR (|):


Op1 Op2 Op1|Op2
0 0 0
0 1 1
1 0 1
1 1 1

Ex: Write a C program to show the usage of ‘|’ operator.


#include<stdio.h>
void main()
{
int a=10,b=6;
c=a|b;
printf(“%d|%d=%d”,a,b,c);
}
Output: 10|6=14
Binary Representation: 10 = 0 0 0 0 1 0 1 0
06 = 0 0 0 0 0 1 1 0
14 = 0 0 0 0 1 1 1 0

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 10
f. Bit-wise XOR (^)
 If the corresponding bit positions in both the operands are different, then XOR operation
results in 1, otherwise XOR operation results in 0.
0^0=0
0^1=1
1^0=1
1^1=0
Ex: Write a C program to show the usage of ‘^’ operator.
#include<stdio.h>
void main()
{
int a=10,b=6;
c=a^b;
printf(“%d^%d=%d”,a,b,c);
}

Output: 10^6=12
Binary Representation:
10 = 0 0 0 0 1 0 1 0
06 = 0 0 0 0 0 1 1 0
12 = 0 0 0 0 1 1 0 0

viii) Special Operators


 Comma operator, size of operator and [ ], ->, Indirection operator, *, dot operator etc.

1. Comma operator
 Comma Operator has the least precedence among all the operators and it is left associative
operator.
 Comma Operator is used in the declaration to separate the variables.
Ex: int a,b,c;
 It can be used to separate the items in the list.
Ex: a=12,345,678;
 It can be used to combine two or more statements into a single statement.
Ex: sum=a+b,sub=a-b,mul=a*b,div=a/b,mod=a%b;

2. sizeof()
 ‘sizeof()’ operator is used to determine the number of bytes occupied by a variable or a constant
in the memory.
 Ex: sizeof(char) 1 byte
sizeof(int) 2 bytes
sizeof(float) 4 bytes
Example program: Write a C program that computes the size of int, float, char and double
variables.
#include<stdio.h>
void main()
{

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 11
char ch;
int x;
float y;
double z;
clrscr();
printf(“Number of bytes occupied by character variable=%d”,sizeof(ch));
printf(“Number of bytes occupied by integer variable=%d”,sizeof(x));
printf(“Number of bytes occupied by floating-point variable=%d”,sizeof(y));
printf(“Number of bytes occupied by double variable=%d”,sizeof(z));
}

2.1.2 Arithmetic Expressions


 The expression consisting of only arithmetic operators such as +, -, *, / and % are called
arithmetic expressions.

Example: Write the equivalent C expression for the Mathematical expressions.


Mathematical Expression C Equivalent Expression
𝑎+𝑏+𝑐 S=(a + b +c) /2
𝑆=
2
𝑎𝑟𝑒𝑎 = √𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐) area=sqrt(s*(s-a)*(s-b)*(s-c))

𝑥 = √2𝜋𝑛 x=sqrt(2*3.142*n)
a a/b
b
𝑏 x=-b/(2*a)
𝑥=−
2𝑎
2
𝑎𝑥 + bx + c a*x*x+b*x+c

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 12
2.1.3 Associativity of operators
 When two or more operators have the same precedence, then precedence rules are not
applicable.
 Associativity determines how the operators with the same precedence are evaluated in an
expression.
The two types of operators Associativity are:
1. Left Associativity
2. Right Associativity

1. Left Associativity
 In an expression, if two or more operators having the same priority are evaluated from
left-to-right, then the operators are called Left to Right associative operators.
 We normally denote it using L R.

2. Right Associativity
 In an expression, if two or more operators having the same priority are evaluated from
right-to-left, then the operators are called Left to Right associative operators.
 We normally denote it using R L.
Ex: i=j=k=10.

2.1.4 Precedence and order of Evaluation


 In C language, each operator is associated with priority value.
 Based on the priority the expressions are evaluated. The priority of each operator is pre-defined
in C language.
 The order in which the different operators are used to evaluate an expression is called
Precedence or hierarchy of operators
 The pre-defined priority or precedence order given to each of the operator is called precedence of
operator.

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 13
 Unary +, - and * have higher precedence than the binary forms.

2.2 Problems on Expression Evaluation

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 14
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 15
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 16
2.3 Type Conversion and Typecasting
 The process of converting the data or variable from one data type to another data type is
called Type Conversion or Typecasting.
 Type conversion is done implicitly by the compiler, whereas typecasting has to be done
explicitly by the programmer.

2.3.1 Type Conversion


 Type conversion is done when the expression has variables of different data types.
 To evaluate the expression, the data type is promoted from lower to higher level where the
hierarchy of data types (from higher to lower level) can be given as: double, float, long, int,
short and char.
char short int int unsigned int long int float double
Lower Rank Higher Rank
Data types Data types

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 17
 C compiler converts the data type with lower rank to the data type with higher rank. This
process of conversion of data from lower rank to higher rank automatically by the C
compiler is called “Implicit type Conversion”.

 If one operand type is same as that of other operand type, no conversion takes place.
Ex: int + int = int, float + float = float
 If one operand type is ‘int’ and other operand type is ‘float’, then the operand with type int is
promoted to ‘float’ (because float is up in ladder compared with int).
 Type conversion is automatically done when we assign an integer value to floating point
variable. Consider the code given below in which an integer data type is promoted to float. This
is known as promotion (where the lower level data type is promoted to higher type).
float x;
int y=3;
x=y;
Now, x=3.0, as automatically integer value is converted into its equivalent floating point
representation.

2.3.2 Typecasting
 Typecasting is also known as forced conversion.
 It is done when the value of a higher data type has to be converted into a value of a lower data
type.
 But this casting is done under the programmer’s control and not under the compiler’s
control.
 The programmer can instruct the compiler to change the type of the operand or variable
from one data type to another data type. This forcible conversion from one data type to
another data type is called “Explicit type Conversion” (Type Casting).
Syntax:
(type) Expression

Ex: (int) 9.43


i= (int) 5.99 / (int) 2.4, now it becomes 5/2 = 2, i=2
(float) (3/10) = 3.0 / 10.0 = 0.3

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 18

You might also like