2022-23 r20 I-I Cse PPSC Unit I Part 4 Notes
2022-23 r20 I-I Cse PPSC Unit I Part 4 Notes
Based on type of operation, these operators can be categorized into the following groups:
The operators used for performing these arithmetic operations are as follows
A B A%B
7 4 3
-7 4 -3
7 -4 3
-7 -4 -3
The following table shows how the division operator operates on various data types.
Operation Result Example
int/int (integer division) int 2/5=0
real/int (mixed division) real 5.0/2=2.5
int/real (mixed division) real 5/2.0=2.5
real/real (real division) real 5.0/2.0=2.5
In the above table first example the integer division truncates the fractional part.
Integer arithmetic: It requires both operands are integer values for arithmetic operation
Ex: a=5,b=4
Expression result
a+b 9
a-b 1
Floating point arithmetic(real arthimetic): It requires both operands are float type for
arithmetic operation.
Ex: a=6.5,b=3.5
Expression result
a+b 10.0
a-b 3.0
Mixed arithmetic: One of the operands are real and the other one is integer. The integer
is also upgraded to real and the result will become a real arithmetic.
An Example „C‟ program that illustrates the usage of arithmetic operators.
#include<stdio.h>
int main()
{
int a=9,b=3;
printf("%d+%d=%d\n",a,b,a+b);
printf("%d-%d=%d\n",a,b,a-b);
printf("%d*%d=%d\n",a,b,a*b);
printf("%d/%d=%d\n",a,b,a/b);
printf("%d%%%d=%d\n",a,b,a%b);
return 0;
}
Output
9+3=12
9-3=6
9*3=27
9/3=3
9%3=0
Relational operators
A relational operator, also known as a comparison operator, is an operator that compares two
operands.
The operands can be variables, constants or expressions.
Relational operators always return either true or false depending on whether the conditional
relationship between the two operands holds or not.
Zero is considered as false and non-zero value is considered as true.
When you try print a true and false value,(i.e result of relational operation) it will
printed as 1 for true and 0 for false.
Syntax: op1 relational operator op2
op1,op2 are variables or constants or expressions
Ex: 9>2 this is true so ‘1’ is returned
C has six relational operators. The following table shows these operators along with their
meanings
Operator Example Description Example Result
> x>y x is greater than y 5>5 0( false)
< x<y x is less than y 5<5 0
>= x >= y x is greater than or equal to y 5>=5 1(true)
<= x <= y x is less than or equal to y 5<=5 1
== x == y x is equal to y 5==5 1
!= x != y x is not equal to y 5!=5 0
An example program that illustrates the use of arithmetic operators.
#include<stdio.h>
int main()
{
int a=9,b=3;
printf("%d>%d=%d\n",a,b,a>b);
printf("%d>=%d=%d\n",a,b,a>=b);
printf("%d<%d=%d\n",a,b,a<b);
printf("%d<=%d=%d\n",a,b,a<=b);
printf("%d==%d=%d\n",a,b,a==b);
printf("%d!=%d=%d\n",a,b,a!=b);
return 0;
}
Output
9>3=1
9 >= 3 = 1
9<3=0
9 <= 3 = 0
9= =3 = 0
9 != 3 = 1
Logical operators
Operators which are used to combine two or more relational expressions are known as
logical operators. C language supports three logical operators
Operator meaning
&& logical AND
|| logical OR
! logical NOT
Logical&& and logical || are binary operators whereas logical ! is an unary operator.
All of these operators when applied to expressions yield either integer value 0 (false) or an integer
value 1(true).
&&: This operator return true i.e,’1’ when both the conditions are true otherwise it
returns ‘0’.
||: This operator returns true if at least one of the conditions combined is true
!: This operator reverses the value of the expression it operates on
The working of these operations are illustrated for possible input values through the
below tables
Truth tables for logical AND
While performing the logical AND, if the first part of the expression(here A) is resulting
false, then the result is false and the remaining part of the expression is not processed.
Truth tables for logical OR
While performing the logical OR, if the first part of the expression(here A) is resulting true,
then the result is true and the remaining part of the expression is not processed.
Truth tables for logical OR
Example: (3<7)&&(7<5)
(true)1&& (false)0
0(false)
result is false
Example: program to demonstrate use of logical operators
#include<stdio.h>
#include<conio.h>
void main()
{
int c1,c2,c3;
clrscr();
printf(“enter values of c1,c2,c3\n”);
scanf(“%d%d%d”,&c1,&c2,&c3);
printf(“\n(c1<c2)&&(c2<c3) is %d”,(c1<c2)&&(c2<c3));
printf(“\n(c1<c2)||(c2<c3) is %d” ,(c1<c2)||(c2<c3));
printf(“\n!(c1<c2) is %d”,!(c1<c2));
getch();
}
Output:
enter values of c1,c2 c3
315
(c1<c2)&&(c2<c3) is 0
(c1<c2)||(c2<c3) is 1
!(c1<c2) is 1
Logical AND
It is used to simultaneously evaluate two conditions or expressions with relational
operators. If expressions on the both sides (left and right side) of logical operators is true then
the whole expression is true otherwise false. The truth table of logical AND operator is given
below:
A B A && B
True True True
True False False
False True False
False False False
Logical OR
It is used to simultaneously evaluate two conditions or expressions with relational
operators. If one or both the expressions on the left side and right side of logical operators is
true then the whole expression is true otherwise false. The truth table of logical OR operator
is given below:
A B A || B
True True True
True False True
False True True
False False False
Logical NOT
It takes single expression and negates the value of expression. The truth table of logical NOT
operator is given below
A !A
True False
False True
For example: x and y are two variables. The following equations explain the use of logical
operators.
z1 = x&&y …1
z2 = x||y …2
z3=!x….3
Equation1 indicates that z1 is true if both x and y is true. Equation2 indicates z2 is true when
x or y or both true. Equation3 indicates z3 is true when x is not true.
int z1,z2,z3;
z1=7>5 && 10>15;
z2=7>5 || 10>15;
z3=!(7>5);
printf(" z1=%d\n z2=%d\n z3=%d
\n",z1,z2,z3); return 0;
}
Output z1=0 z2=1 z3=0
Leap year is an year which is if it is not century year, it divisible by 4 and if it is century, it is divisible
by 400
Increment (++) and decrement (− −) operators
The increment operator is unary operator that increases value of its operand by 1.
Similarly, the decrement operator decreases value of its operand by 1. These operators are
unique in that they work only on variables not on constants. These are used in loops like
while, do-while and for statements.
There are two ways to use increment or decrement operators in expressions. If you put
the operator in front of the operand (prefix), it returns the new value of the operand
(incremented or decremented). If you put the operator after the operand (postfix), it returns
the original value of the operand (before the increment or decrement).
Syntax:
Increment operator:
Pre increment: ++var_name ;
Post increment: var_name++;
Decrement operator:
Pre decrement: -- var_name ;
Post decrement: var_name – -;
Bitwise operators
As the name suggests, the bitwise operators operate on bits. These operations include:
1. bitwise AND(&)
2. bitwise OR(|)
3. bitwise X-OR(^)
4. bitwise NOT(~)
5. shift left(<<)
6. shift right(>>)
For example:
In a C program, the | operator is used as follows.
int a=4,b=2,c;
c=a^b;
printf(“%d”,c); //prints 6
1 1 1 1 0 0
1 0 0 1 1 0
0 1 0 1 1 1
0 0 0 0 0 1
Shift operators
C supports two bitwise shift operators. They are shift-left (<<) and shift-right (>>).
These operations are simple and are responsible for shifting bits either to left or to the right.
The syntax for shift operation can be given as:
int x;
x=10;
Assigns the value 10 to variable x. if we have,
int x=2,y=3,sum=0;
sum = x + y;
then sum=5.
The assignment has right-to-left associativity, so the
expression int a=b=c=10;
is evaluated as
(a=(b=(c=10)))
Consider the following set of examples
#include <stdio.h>
int main()
{
int a = 21,c;
c = a;
printf("Operator is = and c = %d\n", c );
c += a;
printf("Operator is += and c=%d\n",c);
c −= a;
printf("Operator is −= and c=%d\n",c);
c *= a;
printf("Operator is *= and c=%d\n",c);
c /= a;
printf("Operator is /= and c=%d\n", c);
c = 200;
c %= a;
printf("Operator is %= and c=%d\n",c);
c <<= 2;
printf("Operator is <<= and c=%d\n",c);
c >>= 2;
printf("Operator is >>= and c=%d\n",c);
c &= 2;
printf("Operator is &= and c = %d\n", c );
c ^= 2;
printf("Operator is ^= and c = %d\n", c );
c |= 2;
printf("Operator is |= and c = %d\n", c );
return 0;
}
Output
Operator is = and c = 21
Operator is += and c = 42
Operator is −= and c = 21
Operator is *= and c = 441
Operator is /= and c = 21
Operator is %= and c = 11
Operator is <<= and c = 44
Operator is >>= and c = 11
Operator is &= and c = 2
Operator is ^= and c = 0
Operator is |= and c = 2
Advantages:
1. Short hand expressions are easier to write.
2. The statement involving short hand operators are easier to read as they are more
concise.
3. The statement involving short hand operators are more efficient and easy to
understand.
Conditional operator (? : )
It is also called ternary operator because it takes three operands. It has the general form:
variable = expression1 ? expression2 : expression3;
If the expression1 is evaluated totrue then it evaluates expression2 and its value is
assigned to variable, otherwise it evaluates expression3 and its value is assigned to variable.
#include<stdio.h>
int main()
{
printf("char occupies %d bytes\n",sizeof(char));
printf("int occupies %d bytes\n",sizeof(int));
printf("float occupies %d bytes\n",sizeof(float));
printf("double occupies %d bytes\n",sizeof(double));
printf("long double occupies %d bytes\n",sizeof(long double));
return 0;
}
Output
char occupies 1 bytes
int occupies 2 bytes
float occupies 4 bytes
double occupies 8 bytes
long double occupies 10 bytes
Other special operators , are
‘&’ to get address of variable
‘*’ to get the value stored at an address
‘.’ to access members of structure variable, period or member operator
‘→’ to access members of structure pointer
‘(type)’ to convert one type of data to another type
Expressions
In C programming, an expression is any legal combination of operators and operands
that evaluated to produce a value.Every expression consists of at least one operand and can
have one or more operators. Operands are either variables or values, whereas operators are
symbols that represent particular actions.
In the expression x + 5; x and 5 are operands, and + is an operator.
In C programming, there are mainly two types of expressions are available. They are as
follows:
1. Simple expression
2. Complex expression
Simple expression: In which contains one operator and two operands or constants.
Example: x+y; 3+5; a*b; x-y etc.
Complex expression: In which contains two or more operators and operands or constants.
Example: x+y-z; a+b-c*d; 2+5-3*4; x=6-4+5*2 etc.
< <= > >= Relational less than/ less than or equal to Relational left-to-right
greater than/ greater than or equal to
== != Relational is equal to /is not equal to left-to-right
Type Conversion
Type casting: Type casting is a way to convert a variable from one data type to another
data type. It can be of two types:
1. Implicit type casting
2. Explicit type casting
Implicit type casting/type conversion
When the type conversion is performed automatically by the compiler without
programmer‟s intervention, such type of conversion is known as implicit type conversion or
type promotion. In this, all the lower data types are converted to its next higher data type.
If the both operands are of the same type, promotion is not needed. If they are not,
promotion follows these rules:
float operands are converted to double.
char or short are converted to int.
If anyone operand is double, the other operand is also converted to double, and that is the type of result.
or
If anyone operand is long, the other operand is also converted to long, and that is the type of
result.
If anyone operand is unsigned, the other operand is also converted to unsigned, and
that is the type of result.
The smallest to the largest data types with respect to size are given as follows:
For example:
Syntax
(data_type) expression;
Where, data_type is any valid C data type, and expression may be constant, variable or
expression.
The following rules have to be followed while converting the expression from one type to
another to avoid the loss of information:
1. All integer types to be converted to float.
2. All float types to be converted to double.
3. All character types to be converted to integer.