Notes CCP U2
Notes CCP U2
C constants
Character Const:-
Single – alphabet/digit/special symbol kept within single inverted commas.
e.g. ‘A’, ’I’, ’5’, ’=’
Range is -128 to 127, occupies 1 Byte of memory space.
Escape sequences: An escape sequence is a sequence of characters that does not represent itself
when used inside a character or string literal, but is translated into another character or a sequence of
characters that may be difficult or impossible to represent directly. Only, its effect is shown in result.
2
Anurag Tewari
KIET, GZBD
Data Types in C
-32,768(215) to 32,767(215-1 ) %d
or
int 2 or 4 bytes
-2,147,483,648 to 2,147,483,647
3
Anurag Tewari
KIET, GZBD
(6 decimal places)
Operations on Variables:
Variables are Declared – name is given
Defined – memory space is reserved for above name
Initialized – used (computed) with 1st value Used–
Modified, Saved and printed…
data-type char codeidentifier (name)
code = ‘A’ initialization
int C = 0 initialization
When a variable is defined, it is not initialized (automatically). We must initialize any variable
with prescribed data before the function starts using it with some value otherwise a garbage value
may exist .
Operand: it is an entity on which an operation is to be performed. It can be : a var, const,
function call or a macro name. Eg. In expression:a+b, a & b are operands.
Operator: It specifies an operation to be performed on its
operands. Eg. In expression:a+b, ‘+’ is an addition operator applied on operands :a & b.
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”
INT_MAX
(2 * 3+4), (a = 23 + b * 6)
Simple expression: a+b has 1 operator: ‘+’
Compound expression: c=a+b has 2 or more operators: ‘+’ & ‘=’
Assignment expression(use of assignment operator:‘=’):
It evaluates the operand on the right side of the operator (=) & keeps its value in the variable on
the left.
2 types of assignment expressions.
5
Anurag Tewari
KIET, GZBD
Classification of operators=>
UNARY
OPERATOR
BINARY
BASED ON OPERATOR
NO. OF ARITHMETIC
TERNARY
OPERANDS OPERATORS
OPERATOR
RELATIONAL
OPERATORS
CLASSIFICATION
OF OPERATORS LOGICAL
OPERATORS
BASED ON BITWISE
ROLE OF OPERATORS
OPERATOR
ASSIGNMENT
OPERATORS
MISCELLANEOUS
OPERATORS
a = a +1
step(2) value of a is incremented by 1(assigned to a)
6
Anurag Tewari
KIET, GZBD
++ 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>
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.
scanf("%d%d",&d1,&d2);
larger=d1>d2?d1:d2;
printf("Larger Number is = %d",larger);
}
8
Anurag Tewari
KIET, GZBD
9
Anurag Tewari
KIET, GZBD
O/P-> 1 1 0 1 O/P:0 0 2 0
Examples:
main() { int i=1,j=1,k=2,l;
l=i||j++&&++k;
printf("%d %d %d %d",i,j,k,l); }
//O/P:1 1 2 1
main()
{ int i=0,j=1,k=2,l;
l=++i&&j++||++k;
printf("%d %d %d %d",i,j,k,l); }
//O/P:1 2 2 1
main()
{ int i=0,j=1,k=2,l;
l=++i&&--j||++k;
printf("%d %d %d %d",i,j,k,l); }
//O/P:1 0 3 1
main()
{ int i=0,j=1,k=2,l;
l=++i&&j--||++k;
printf("%d%d%d%d",i,j,k,l); }
O/p:1 0 2 1
10
Anurag Tewari
KIET, GZBD
RELATIONAL OPERATORS
• Relational operators are used to compare two quantities (i.e. their
operands). There are six relational operators in C:
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
11
Anurag Tewari
KIET, GZBD
== C
omplement of
!=
We can also simplify operators as:
!(x >y) x<=y
! (x < y) x>=y
!(x! = y) x==y
Programs objectives to utilize Relational & Logical Operators:
1. Wap to check whether i/p no. is even or odd.
2. Wap to check if the person is eligible for voting or not.
3. Wap to check if the i/p year is Leap year or not
4. Wap to calculate the salary of a person after increment, given conditions are- if experience is
between 0 to 3 years then 10% increment otherwise 20% increment will be given.
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.
Associativity types: left to right
right to left
eg: a = 3/2 * 5
here ‘/’ & * have same priority & both also use left to right associativity.
So ‘/’ or * which one should perform its operation first.
so ‘/’ will be performed first.
‘=’ has R to L associativity so in “a = b = 3”, b = 3 will be operated first.
a = b =c= 3 => a=(b=(c=3))
In arithmetic operators precedence is same as Hierarchy among operators:
1st priority Parenthesized value(value within bracket or paranthesis): a*(b/(c-d))
2nd priority *, /, %.
3rd priority +, -
4th priority =
Direction of solving (associativity) is from left to right in an expression.
i = 2 * -3 / 4 + 4/4 + 8 -2 + 5%8
Result
6/4 + 4/4 +8 - 2 + 5/8
12
Anurag Tewari
KIET, GZBD
1 + 4/4 + 8 - 2 + 5/8
1 + 1 + 8 -2 + 5/8
10 – 2 + 0
8+08
B= 6*-3%-9+18/3-5
=(6*3)%9+(18/3)-5=0+6-5=1
Calculate the results of following expression by applying precedence & associativity rules:
1. a-b/(3+c)*(2-1); a=9,b=12,c=3. Ans:
3. 4+5-55/5%-10 . Ans:8
13
Anurag Tewari
KIET, GZBD
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:
14
Anurag Tewari
KIET, GZBD
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(=2147483647);
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
expressions 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.
15
Anurag Tewari
KIET, GZBD
o Long int to int causes dropping of the excess higher order bits.
Example: int i, x;
float f;
double d;
long int L;
x = L / i + i * f - d
float
long
float
long
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.
16
Anurag Tewari
KIET, GZBD
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;
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); }//compiler dependent
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
1. Compression: Occasionally, you may want to implement a large number of Boolean variables,
without using a lot of space. A 32-bit int can be used to store 32 Boolean variables. Normally, the
17
Anurag Tewari
KIET, GZBD
minimum size for one Boolean variable is one byte. All types in C must have sizes that are multiples
of bytes. However, only one bit is necessary to represent a Boolean value.
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.
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.
18
Anurag Tewari
KIET, GZBD
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.
19
Anurag Tewari
KIET, GZBD
20
Anurag Tewari
KIET, GZBD
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
21
Anurag Tewari
KIET, GZBD
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.
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>
22
Anurag Tewari
KIET, GZBD
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;
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
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)
// == 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.
24
Anurag Tewari