Notes-3
Notes-3
1
(e) Range → -32768 to 32767 (for 16-bit compiler)
(f) Integer constant:023=>23 in octal no.sys;0x23=> 23 in hex no.sys;23L or 23l=>long int,
23U or 23u=>unsigned int
Real Constants :- (also called as Floating point Const.)
Can be written as –: fractional form Or exponential form
(a) It must have atleast one digit.
(b) Decimal point present.
(c) +ve or –ve, default +ve
(d) No comma/blank allowed.
(e) It is represented in exponential form if the value is either too small or too large.
In exponential form two parts of constant →
Mantissa exponent
e
Expression: It is made up of one or more operands and operators in it. as: a=b+c
Primary expression – It consists of only one operand with no operator.
eg.
Name, literal constant, parenthetical expression
a, b12, 5,123.98,
price, ‘A’, “welcome”
2
INT_MAX
(2 * 3+4), (a = 23 + b * 6)
Simple expression: a+b has 1 operator: ‘+’
Compound expression: c=a+b has 2 operators: ‘+’ & ‘=’
Thus, an expression is a sequence of operands and operators that specifies the computation of a value.
Classification of operators
UNARY
OPERATOR
BINARY
BASED ON OPERATOR
NO. OF ARITHMETIC
TERNARY
OPERANDS OPERATORS
OPERATOR
RELATIONAL
CLASSIFICATION OPERATORS
OF OPERATORS LOGICAL
OPERATORS
BASED ON BITWISE
ROLE OF OPERATORS
OPERATOR
ASSIGNMENT
OPERATORS
MISCELLANEOUS
OPERATORS
1.Unary operators
It operates on one operand (operand on left or on right of operator),
as: in expression “-3”, ‘-’ is a unary minus operator It operates on 3 only.
Other unary operators are : ++(increment operator), --(decrement operator), &(address of operator),
sizeof( ) operator,!(logical not operator), ~(bitwise not operator) etc.
Expression types on the basis of position of ++ and --(Unary operators):
PostFix & PreFix expression : ++, - -
1. prefix: operator , operand, ex: ++ a(increment a by 1=>(a= a+1))
3
2. Post fix: operand, operator Variable, ex: a++(increment a by 1=>(a= a+1))
Evaluation of postfix ++: (first use then increment)
a++
x=a
x=a++ step(1) value of expression is a(assigned to x)
a = a +1
step(2) value of a is incremented by 1(assigned to a)
Evaluation of prefix: (first increment then use)
++ a a = a +1
(1) value of a is incremented by 1(assigned to a)
x=a++
x=++a
x=a
(2) value of expression is a after increment(assigned to x).
Similarly for a-- & --a
Both are: a= a – 1, after execution.
#include <stdio.h>
Void main(){
int c=2,d=2;
printf("%d\n",c--); //this statement displays 2 then, only c incremented by 1 to 3.
printf("%d",++c); //this statement increments 1 to c then, only c is displayed. }
Size of () :
It tells us the size, in bytes of a type or a primary expression.
Eg.
sizeof (int); => 2, size of integer data type is 2 bytes or 4 bytes.
sizeof(-345.23); => 4, size of floating point data type is 4 bytes
sizeof (x)
2.Binary operators
It operates on two operands (one operand on left and other on right of operator),
It is left to right associative
as: in expression “10+4”, ‘+’ operates on 10 and 4.
Other binary operators are: +, -, /, %, <<(left shift operator),==(equality operator),
&&(logical and operator) etc.
4
3.Ternary Operator (conditional operator)
It operates on three operands. As: conditional operator(? : ) is ternary operator.
Ex:
#include <stdio.h>
main(){
int d1,d2, larger;
printf("Enter two numbers a & b: ");
scanf("%d%d",&d1,&d2);
larger=d1>d2?d1:d2;
printf("Larger Number is = %d",larger);
}
5
Arithmetical Operators: addition(+),subtraction(-),Multiplication(*),division(/),modulus(%) etc.
Modulus operator (%) :
It returns the remainder as a result after division operation
4 %3 = 1, -5 % 2 = -1
7% 3 = 1, 5 % -2 = 1, 2 % 3 = 2
Logical data :- A piece of data is called logical if it conveys the idea of true or false.
C has no logical data type. Integer can be used to represent logical data.
If a data Value is zero(‘0’) → false
If a data Value is non-zero (+ve or -ve)→ true
6
If E1 evaluates to 0 (i.e. false) then E2 will be evaluated to determine the truth value of overall
expression.
Example:
void main() main()
{
{
int a=10,b=-5;
int x=3,y=0; int i=0,j=0,k=2,l;
int c,d,u,v; l=i||j&&k;
c=a&&b;
d=a||b;
u=x&&y; printf("%d%d%d%d",i,j,k,l);
v=x||y; }
printf("%d%d%d%d",c,d,u,v);
}
O/P-> 1 1 0 1 O/P:0 0 2 0
RELATIONAL OPERATORS
• Relational operators are used to compare two quantities (i.e. their
operands). There are six relational operators in C:
7
KEY POINTS:RELATIONAL
OPERATORS
✓ There should be no white space character between two symbols of
a relational operator.
✓ The result of evaluation of a relational expression (i.e. involving
relational operator) is a boolean constant i.e. 0 or 1.
✓ Each of the relational operators yields 1 if the specified relation is
true and 0 if it is false. The result has type int.
✓ The expression a<b<c is valid and is not interpreted as in ordinary
mathematics. Since, less than operator (i.e. <) is left-to-right
associative, the expression is interpreted as (a<b)<c. This means
that “if a is less than b, compare 1 with c, otherwise, compare 0
with c”.
✓ An expression that involves a relational operator forms a condition.
For example, a<b is a condition.
Ex:
main() main()
{ {
int a=2,b=3,c=1,d; int a=2,b=3,c=1,d;
d=a<b; d=a<b>c; //it is:(a<b)>c
printf("%d",d); printf("%d",d);
} }
Output: 1 Output: 0
8
Precedence and associativity Of Operators
The order in which the operators will operate depends upon the precedence and associativity.
Associativity of operators:
When an expression has two operators of equal priority/precedence the tie between them
is settled using the associativity of the operators.
3. 4+5-55/5%-10 . Ans:8
9
After solving above A=1.
10
Table: Name, Symbol, Category, Precedence and Associativity of Operators
Data-Type Conversion
In any expression if two variables of different data-types are used, then at least one
of them will be converted into another, and then only that operation can be performed.
Two Categories of type conversion –
(A) Implicit
(B) Explicit(type-casting)
(A) Implicit Type Conversion :- When the 2 operands of different types are used in
a binary expression, c compiler automatically converts one type to another, this is called
implicit type conversion.
o This is generally done with the intermediate values during evaluation of expression.
Rules to perform implicit type conversion are as based on hierarchy:
11
Conversion Rank (Hierarchy):
o In assignment expression :
L.H.S. = R.H.S.; then there may be 2 kinds of conversion exists-
Promotion – if the right expression (R.H.S.) has lower rank so it is promoted.
Demotion – if the right expression (R.H.S.) has a higher rank so it is demoted. Demotion occurs only in
assignment operation implicitly.
Since small object can be easily kept in a big box so promotion is always possible but
reverse is not true.
eg:
char c = ‘A’;
int i = 1234;
long double d = 3458.0004;
now promotion occurs in following independent assignments:
i = c; - value of i = 65
d = i; - value of d is 1234.0000
o So in Demotion – If the size of the variable at the left side can accommodate the value of the
expression, it is O.K. otherwise results may be unpredictable.
eg.: short s = 78;
char c = ‘A’; int j = INT_MAX(=32767);
int k = 65;
s=j - value of s may be - unexpected
c = k+1 - demotion : value of c is ‘B’.
Conversion in other Binary Expressions: Summary of rules can be followed in these 3 steps:
1. The operand with the higher rank is determined using the ranking in hierarchy figure
2. The lower-ranked operand is promoted to the rank defined in 1st step. In 2nd step after promotion,
both expression have same rank.
3. The operation is performed with the expression value having the type of promoted rank.
ex:
char c = ‘A’; int i = 3650; short s = 78;
long double d = 3458. 0004;
i*s; // result is an int
d*c; // result is long double.
float
long
float
long
float
float
double
double
int
Integer & Float conversions:
(a) Integer {(arithmetic operation)} integer = Integer (result)
(b) Real (float) {(arithmetic opn.)} real (float) = Real (result)
(c) Integer {(arithmetic opn.)} real (float) = Real (float) result.
5.0/2 = 2.50000
5/2 = 2
5/2.0 = 2.5
Explicit conversion :-
o By this we can convert data from one type to another forcefully (not automatically)
o It is done by using a unary operator – cast operator: “( )” and this operation is called-TypeCasting
o To cast (convert) data from one type to another, we specify data-type (new) in parenthesis before
the value we want to convert.
eg.: to convert an integer, a , to a float : (float) a
o Like any other operation, the value of a is still of type int, but the value of the expression is
promoted to float.
General form of explicit conversion is :
(data type-name) expression
ex:
a= (int)21.3/(int)4.5
so result stored in a is 5
y = (int)(a+b), The result of a+b is converted to int.
x= (int) (y+0.5)
If y = 27.6 → y +0.5 = 28.1 but after casting x = 28 is stored. So, the result is changed not the
expression
o It is always true that we should use explicit conversion (casting), instead of leaving it on system.
main() main()
{ {
float u=3.5; int u=3.5,v,w,x,y;
int v,w,x,y; v=(int)(u+0.5);
v=(int)(u+0.5); w=(int)u+0.5;
13
w=(int)u+0.5; x=(int)((int)u+0.5);
x=(int)((int)u+0.5); y=(u+(int)0.5);
y=(u+(int)0.5); printf("%d%d%d%d",v,w,x,y);
printf("%d%d%d%d",v,w,x,y); }
}
Output:4333 3333
main() main()
{ {
char aa='c'; int i=5;
int b=97; printf("%d%d%d%d%d",i++,i--,++i,--i,i);
printf("%d",aa+10); }
printf("\t%c",b-10);
}
45555
Output:109 w
Bit-wise operators
S.No Operator Name of Category ary of Precedence Associativity
Operator Operators amongst
bitwise class
1. ~ Bitwise NOT Unary Unary Level-I R→L
2. Set operations: You can also use bits to represent elements of a (small) set. If a bit is 1,
then element is in the set, otherwise it's not. You can use bitwise AND to implement set
intersection, bitwise OR to implement set union.
3. Encryption: swapping the bits of a string for e.g. according to a predefined shared key will
create an encrypted string.
14
✓ Bitwise operators operate on the individual bits of operands and are used for bit
manipulation.
✓ They can only be applied on operands of type char, short, int, long, whether signed or
unsigned (never on float/double).
✓ The bitwise-AND and bitwise-OR operators operate on the individual bits of the
operands according to the truth tables.
Int x=12, y=10 then, printf(“%d”,x&y) will give 8 in decimal. printf(“%d”,x|y) will give 14 in
decimal. printf(“%d”,x^y) will give 6.
15
16
Left shift Operator “<<”
The left shift operator will shift the bits of x(decimal no. represented in binary digits) towards
left for the given number of times(n) as x<<n
int a=2<<1;
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 0 1 0
Now shifting the bits towards left for 1 time, will give the following result
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 1 0 0
Now the result in decimal is 4. You can also note that, 0 is added as padding in the position 0.
If you left shift like 2<<2, then it will give the result as 8. Therefore left shifting n times, is
equal to multiplying the value (x) by 2n.
17
Right shift Operator “>>”
The right shift operator will shift the bits towards right for the given number of times.
int a=8>>1;
Let’s take the binary representation of 8 assuming int is 1 byte for simplicity.
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 1 0 0 0
Now shifting the bits towards right for 1 time, will give the following result
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 1 0 0
Now the result in decimal is 4. Right shifting 1 time, is equivalent to dividing the value by 2. You
can also note that, 0 is added as padding in the position 7.
Therefore right shifting n times, is equal to dividing the value (x) by 2n.
In case of arithmetic shift, the sign-bit ( MSB ) is preserved. Logical shift will not preserve the
signed bit. Let’s see this via an example.
#include<stdio.h>
int main() {
signed char a=-8;
signed char b= a >> 1;
printf("%d\n",b);
}
In the above code, we are right shifting -8 by 1. The result will be “-4″. Here arithmetic shift is
applied since the operand is a signed value.
int main() {
unsigned char a=-8;
unsigned char b= a >> 1;
18
printf("%d\n",b);
}
Note: Negative number are represented using 2’s complement of its positive equivalent.
2's compliment of +8 is: 1111 1000
• x (+) 0 = x
XORing with 0 gives you back the same number. Thus, 0 is the identity for XOR.
• x (+) 1 = ~x
XORing with 1 gives you back the negation of the bit. Again, this comes from the truth
table. For bitwise XOR, the property is slightly different: x ^ ~0 = ~x.
That is, if you XOR with all 1's, the result will be the bitwise negation of x.
• x (+) x = 0
XORing x with itself gives you 0. That's because x is either 0 or 1, and 0 (+) 0= 0and 1 (+)
1 = 0.
• XOR is associative.
That is: (x (+) y) (+) z = x (+) (y (+) z).
You can verify this by using truth tables.
• XOR is commutative.
That is: x (+) y = y (+) x.
You can verify this by using truth tables.
Swapping without "temp"
temp = x ;
x=y;
y = temp ;
Now solve this without using a temp variable. This means you can ONLY use x and y.
This does NOT mean that you name the variable temp2.
x=x^y;
y=x^y;
x=x^y;
The key to convincing yourself this works is to keep track of the original value of x and y.
Let A be the original value of x(that is, the value x has just before running these three
lines of code).
Similarly, let B be the original value of y.
We can comment each line of code to see what's happening.
// x == A, y == B
x=x^y;
// x == A ^ B, y == B
y=x^y;
// x == A ^ B
// y == (A ^ B) ^ B == A ^ (B ^ B) (by Assoc)
// == A ^ 0 (by z ^ z == 0 property)
19
// == A (by z ^ 0 == z property)
x=x^y;
// x == ( A ^ B ) ^ A
// == ( A ^ A ) ^ B (by Assoc/Commutativity)
// == 0 ^ B (by z ^ z == 0 property)
// == B (by z ^ 0 == z property)
// y == A
After the second statement has executed, y = A. After the third statement, x = B.
20