0% found this document useful (0 votes)
6 views254 pages

C Programming 1

Uploaded by

garasiyaraj552
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views254 pages

C Programming 1

Uploaded by

garasiyaraj552
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

weightage

Monday, July 29, 2024 3:07 PM

c + dsa
total : 18 to 22 marks

C programming -
(i) data types and operators .
(ii) control flow statements : if-else, loops, etc.
(iii)functions and storage classes.
(iv) arrays and pointers.
(v) strings.
(vi) structure and union.
(vii) miscellanous topics.

weightage Page 1
chapter - 1 (introduction to C programming)
Monday, July 29, 2024 2:58 PM

introduction to C programming

program

#___
machine
voidmain(){
translator readable machine output
---
0.....1
--- - compiler
--- - interpreter
--- } - assembler

ATM machine
input program output

credentials money
(pin number)

q. why we cannot write program in 0 an 1?


sol.
- it is difficult to debug the program as a single 0 or 1 can change the entire program and
it will be difficult to find out which 0 or 1 was wrong.
- to perform addition we have to give the instructions in 0 and 1 and for every program we have
to remember the codes and that will be difficult.
- we humans are not good with numbers.

so, instead we started using letters

addition : ADD
subtraction : SUB
multipication : MUL

chapters Page 2
collections of these mnemonics are called assembly language.

00000011 00001001 change into ADD


(machine language) (assembly language)

- high level language :


3+9
a+b : hll (human readable)

ADD assembler 0....1

high level language to assembler to machine language :

ADD machine language


3+9 compiler assembler
3,9 00000011...0100
hll

set of rules for program :

(a) every programming language must have the facility to take input coming from the keyboard.
(b) every programming language must have the facility to display output from program to screen.
(c) every programming language must have the set of grammar rules to follow.

chapters Page 3
o/p

screen

i/p
CPU keyboard

programmer creating software for bank and user is accessing the programs through interface :

software
set of programs
p1
programmer p2
p3... whenever you click on any
particular option, the set of
code behind machine is
running.
interface
withdraw
user

interface

- abstraction : hiding the important details which are not needed. like for an ATM user it is not
neccesary for him to know the code and details about the program running behind
the software in order to operate the ATM machine.

q. which software to use for C programs?


C related software.
whenever you install C related software there will be 2 things in picture :

(i) compiler

chapters Page 4
(i) compiler
(ii) library

- what is library and why library?

program

#include<stdio.h>
voidmain(){
aryan
printf ("aryan");
}

concept : printf

whatever you provide inside " " will be displayed on screen as it is.

when compiler is compiling the program, it will look for the code behind the printf, we did not write
the code of printf but we are simply using it as a template and the code of printf is written inside
the header file- #include<stdio.h>

so, here we are using printf through header file (#include<stdio.h>) as an interface.

library : collection of these header files like 'printf'

so as a user we are able to use these pre-built codes through library as an interface

topic - variable
being able to change (vary)

variables are just like the containers in the kitchen.

tea rice sugar

(a) (b) (c)

rice sugar tea

chapters Page 5
(a) (b) (c)

every container have


- types
- capacity

every memory
block has an
2186 address

3189

memory

randomly storing the data : 10

every memory
10 block has an
2186 address

3189

memory

the problem here is how we will retrieve the data?


this box doesnt have any name and there are lakhs of the blocks in the memory.

so in order to store the data we have to name it like "a = 10;"


here'a' is a variable that is basically an idenitifer.

identifier (name of variable)


a
chapters Page 6
a

10 data

1084
address

concept - garbage value


the garbage value is a random value at an address in the memory of a computer. whenever a
variable is defined without giving any value to it, it contains the leftover values from the
previous program.

types of data
(i) Google maps

source Delhi
text type data
destination GOA

(ii) ATM machine

pin no. 1234 numeric type data

data can be of different type.

data

numeric text
- adress
- mail
numbers without numbers with - name
decimal decimal
12, -13, +12 12.1, 13.2,-9.8

chapters Page 7
o/p

screen

i/p
CPU keyboard

whatever we enter from the keyboard it will be the sequence of 0 and 1's

A : 0000 1111 (binary number)


value : 15

but we have to tell the program ki humein 0000 1111 ko "A" ki tarah deal karna hai
ya "15" ki tarah aur iske lie aate hai data types.

example :
(i) int a = 20;

identifier (name of variable)


a

20 data

1084
address

'int' means integer, number without decimal point.

(ii) float b = 9.8;

identifier (name of variable)


a

chapters Page 8
9.8 data

1084
address

'float' means number with decimal point.

(iii) char C = "@";

identifier (name of variable)


a

@ data

1084
address

to store one symbol 'char' data type is used.


so these keywords char, float, int are reserved words or keywords.

chapters Page 9
chapter - 2 (data types and operators)
Monday, July 29, 2024 6:22 PM

data types and operators

data types

primitive derived user defined


- integer - arrays - structure
- character - pointers - union
- floating point - string - enum
- boolean and other - type def

concept : integer
(a) short int (minimum 2 byte) :
(i) signed integer : -12, +12 (negative and positive)
int a = 20; (or) signed int a = 20; is same.
(if you do not write signed or unsigned then compiler will take it as by default signed)

(ii) unsigned integer : 12 (starts from 0)


unsigned int age = 20;
in case of unsigned you have to mention, ex : age cannot be negative.

(b) int :
(i) signed integer : -12, +12
(ii) unsigned integer : 12

(c) long int :


(i) signed integer : -12, +12
(ii) unsigned integer : 12

(d) long long int :


(i) signed integer : -12, +12
(ii) unsigned integer : 12

(ii) floating point

(a) float
(i) signed integer : -12.20, +12.20
(ii) unsigned integer : 12.20
chapters Page 10
(ii) unsigned integer : 12.20

(b) double
(i) signed integer : -12, +12
(ii) unsigned integer : 12

(c) long double


(i) signed integer : -12, +12
(ii) unsigned integer : 12

decimal number system binary number system


possible values we can generate : (0,1)

1 digit : 1bit
(i) 1st 2nd
10 10 (i) one bit - 0/1 : 2 possible values can be generated.

(ii) two bit - 0/1 : 2 and 0/1 : 2


102
22=4 possible values can be generated

(ii) 1st 2nd 3rd (iii)eight bit -28 possible values can be generated
10 10 10

103

unsigned

2bits 4bits
__ __ __ __ __ __
0 0
0 1 24 = 16 values
1 0 range : 0 to (16-1)15
1 1

4 values
range : 0 to (4-1)3

range: integer
(i) unsigned range of int

chapters Page 11
(i) unsigned range of int

int : 2byte int : 1byte int : 4byte


216 possible values 28 possible values 232 possible values
65,536 256 4294967296

range : 0 to 65,535 range : 0 to 255 range : 0 to 4294967296


0 to 216-1 0 to 28-1 0 to 232-1

(ii) signed range of int

int : 2byte int : 1byte int : 4byte


216 possible values 28 possible values 232 possible values
65,536 256 4294967296

range : -32,768 to 32,767 range : -128 to 127 range : -2147483648 to 2147483647


-215 to 215-1 -27 to 27-1 -231 to 231-1

for n bits

unsigned signed
0 to 2n-1 -2n-1 to 2n-1-1

facts about printf


(i) whatever you provide in double quotes printf will print as it is

#include<stdio.h> a
void main (){
int a = 20; 20
printf ("a");
} 1084

output : a

so, the value is not printing here.

(ii) whatever you provide in double quotes printf will consider it as textual data by format.

#include<stdio.h>
void main (){
printf ("10+10");
}

chapters Page 12
output : 10+10

so, the addition is not printing here.


we have to specify the format, for this we need format specifier

concept : format specifier


(i) %d - format specifier for integer

#include<stdio.h> a
void main (){
int a = 10; 10
printf ("the value
is %d",a); 1084
}

output : the value is 10

printf ("the value is %d",a); the value that which needs to


be printed is in a. (after comma tells us)

some data in integer form is to be printed.

#include<stdio.h> a b c
void main (){
int a = 10, b = 20, c; 10 20 G
c = a+b;
printf ("the sum of %d and %d is %d",a,b,c) 1084 2096 3184

}
c
output : the sum of 10 and 20 is 30
G 30

3184

initialization

#include<stdio.h> a b
void main (){
int a = 10, int b ; 10 G
c = a+b;
printf ("the sum of %d and %d is %d",b,a,c) 1084 2096

chapters Page 13
output : the sum of 20 and 10 is 30

(ii) %u - format specifier for unsigned short int and integer


(iii) %ld - format specifier for long integer
(iv) %lld - format specifier for long long integer

cyclic property

integer %d

65535 0 1 -1 0 1
2 2
65534 -3 -2 3
-4 4

unsigned int signed int


%u %d

32768 32767 -32768 32767

#include<stdio.h> a
void main (){ it is available in signed circle
int a = -4; -4
printf ("%d",a);
} 1084

by default signed

output : -4

#include<stdio.h> a
void main (){ it is not available in unsigned circle so
unsigned int a = -1; 65535 it will move anticlockwise and will take
printf ("%u",a); the value available at -1 in unsigned
} 1084 circle

output : 65535

#include<stdio.h> i

chapters Page 14
#include<stdio.h> i
void main (){ it is not available in unsigned circle so
unsigned int i = -1; 65535 it will move anticlockwise and will take
printf ("%d",i); the value available at -1 in unsigned
} 1084 circle

output : -1

-1 0 1
2
-3 -2 3
-4 4

we are printing %d (signed) but the


value assigned as unsigned so we
signed int have to move twice in the signed
%d circle to reach 65535 and the value
available at 65535 in the signed
circle is -1

-32768 32767

#include<stdio.h> a
void main (){
int a = 32768; -32768
printf ("%d",a);
} 1084

by default signed

output : -32768

#include<stdio.h> a
void main (){
int a = -32770; 32766
printf ("%d",a);
} 1084

by default signed

output : 32766

concept : scanf
scanf : to read input

chapters Page 15
o/p

screen

i/p
CPU keyboard

(i) scanf ("%d") - the first thing we tell scanf is about format, in which format we are about to
give data.

(ii) scanf ("%d",&a); - the second thing we tell scanf is the address, where and in which variable
we want to save data.

#include<stdio.h> a
void main (){
int a;
printf ("enter a number:" ");
scanf ("%d",&a); 1084
printf ("the value is %d",a);
} a

output :
10
enter a number : 10
the value is 10 as we enter the value 10, 10 will be
saved at location a 1084

#include<stdio.h>
void main (){
int a,b,sum; a b sum
printf ("enter 2 numbers:"); 30
scanf ("%d,%d",&a,&b); 10 20 G
sum = a+b;
printf ("the sum of %d and %d is %d",a,b,sum);
}
output :
enter 2 number : 10 20
the sum of 10 and 20 is 30

chapters Page 16
the sum of 10 and 20 is 30

note : it is not neccesary to write printf before scanf but just to make the program user
friendly we use printf

range: char(character)%c

unsigned signed
0 to 255 -128 to 127

decimal number to binary are possible but how symbols are converted into binary?
program

#___ 0100....
voidmain(){ 0011....
int a = 10; ..
--- 10 : 0000 1010 ..
--- .
--- 0010....
---
}

for that we need a character system

english assigned number

@ 001___0000
information information

both are information but their representations are different.


for character system there is ASCII code (american standard code for information interchange)

chapters Page 17
255 0 1 -1 0 1
2 2
254 -3 -2 3
-4 4

unsigned char signed char


%u %d

+128 +127 -128 +127

#include<stdio.h> c
void main (){
char c = 65; 0100 0001
printf ("%d"c");
}
value stored

output : but asked for %d, 65 is in %d circle.


65

#include<stdio.h> c
void main (){
char c = 128; -128
printf ("%d"c");
}
value stored

chapters Page 18
char c = 128; -128
printf ("%d"c");
}
value stored
by default signed but asked for %d, -128 is in %d circle.

output :
-128

#include<stdio.h> c
void main (){
char c = 132; -124
printf ("%d"c");
}
value stored
by default signed but asked for %d, -124 is in %d circle.

output :
-124

#include<stdio.h> c
void main (){
char c = -191; 65
printf ("%c"c"); 256-191=65
}
value stored
by default signed asked for %C (unsigned), 65 is A is in %c system.

output : A

concept : escape sequence

(i) \n : it moves the cursor from its current position to the beginning of the next line

#include<stdio.h> #include<stdio.h>
void main (){ void main (){
printf ("hello everyone"); printf ("hello everyone\n");
printf ("how are you"); printf ("how are you");
} }

output : hello everyone how are you output : hello everyone


how are you
chapters Page 19
how are you

(ii) \t: it moves the cursor to next available frame.

#include<stdio.h>
void main (){
printf ("hello/t");
printf ("pankaj");
}
1st frame 2nd frame
h e l l 0 _ p a n k a j ________________

8 columns = 1 frame 8 columns = 1 frame

8 columns

25 rows

3 spaces are there.

topic : operators

10 + 20=30
10 x 20=200

10, 20 (operand)
+ , X (operators)

there is a value associated with every operations

operators

unary binary ternary


(1 operand) (2 operand) (3 operand)
+23 10 + 20
chapters Page 20
unary binary ternary
(1 operand) (2 operand) (3 operand)
+23 10 + 20
-17 10 x 20

C program : collection of statements a

(i) declaration : int a; G


(ii) declaration + initialization: int a = 10;
(iii) assignment statement : a=10;
a

10

(iv) expression: statement having some value

5+10=15, 15 is a value hence it is a expression


int a; it is not a expression because it does not have any value, instead it is a statement.

(i) every expression is a statement.


(ii) every statement is not a expression.

any value is a valid expression and expression is a statement, so a value always represents a
C statement.

valid C program

#include<stdio.h>
void main (){
5+2;
10.2;
}

(i) assignment operator (=)


LHS = RHS
it is a binary operator (=) R to L

it updates the value of a variable.

#include<stdio.h> a
void main (){
chapters Page 21
it updates the value of a variable.

#include<stdio.h> a
void main (){
int a; G
a = 10 + 3 x 2;
}
a
16
G

updated the value of variable

expression

valid statements:

#include<stdio.h>
expression
void main (){
int a;
a = 10 + 3 x 2;
}

#include<stdio.h>
void main (){
int a;
a = 30; literal/constant
}

#include<stdio.h> a b
void main (){
int a, b =20; G 20
a = b; variable
}
a
20
G

Lvalue = R value

R value can be
- expression
- literal/constant
- variable
chapters Page 22
- variable

L value cannot be
- expression
- literal/constant
- must be a variable

(ii) arithmatic operator (+ ,- ,x ,/ ,%)


priority / precedence

- high (X, /, %) L to R
- low (+, -) L to R

priority means how to parenthesize and not how to evaluate

example :
- 8/2+3
(8/2)+3 as the priority of / is more than +

what if both operators are of same priority?


- 8/4 x 2
so, when 2 operators are of same priority (or) multiple occurence of same operator we use associativity

associativity
(i) left to right .
(ii) right to left.

(iii) modulus operator (%)


it is a binary operator

a%b : what is the remainder when a is divided by b?

#include<stdio.h>
void main (){
printf ("%d",12%5);
}

output : 2

note : both operand for % must be integer type otherwise compiler will give errors.

chapters Page 23
#include<stdio.h>
void main (){
printf ("%d",2%6);
}

output : 2

- #include<stdio.h>
void main (){
a b c
int a,b,c;
a = b = c = 4;
printf ("%d %d %d", a, b, c); G G G
}
a b c

(i) a=b=c=4
G G G
a=b=(c=4) 4

a b c

(ii) a=b=4 4
G G4
a=(b=4)

a b c

(iii) a=4 4
G4 4
a=4

a b c

4 4 4

- #include<stdio.h>
void main (){ L value cannot be constant/literal
int a,b,c;
a = b = 4 = c;
printf ("%d %d %d", a, b, c);
}

chapters Page 24
result of any operator depends upon operand

(i) 4/2 = 2
4 : int type
2 : int type
result : int type

(ii) 4.0/2 = 2.0


4.0 : floating type (double)
2 : int type
result : floating type (double)

(iv) relational operators

they all are binary operators


- a<b : is a less than b?
- a>b : is a greater than b?
- a<=b : is a less than (or) equal to b?
- a=>b : is a greater than (or) equal to b?
- a==b : is a equal to b?
- a!=b : is a not equal to b?
you can categorise them in true or false.

priority / precedence

- high (<, <=, >,=>) L to R


- low (==, !=) L to R

- #include<stdio.h>
void main (){
printf ("%d", 10<20);
}
output : 1

the statement is true hence the output is 1

- #include<stdio.h>
void main (){
printf ("%d", 0>0);

chapters Page 25
printf ("%d", 0>0);
}
output : 0

the statement is false hence the output is 0

- #include<stdio.h>
void main (){
printf ("%d", 10<=10);
}
output : 1

there are two statements


(i) 10<10 : false
(ii) 10=10 : true
hence, the statement is true.

- #include<stdio.h>
void main (){
printf ("%d", 20<=10);
}
output : 0

there are two statements


(i) 20<10 : false
(ii) 20=10 : false
hence, the statement is false.

- #include<stdio.h>
void main (){
printf ("%d", 10==10);
}
output : 1

(i) 10==10 : true


hence, the statement is true.

- #include<stdio.h>
void main (){
printf ("%d", 20==10);
}
chapters Page 26
}
output : 0

(i) 20==10 : true


hence, the statement is false.

- #include<stdio.h>
void main (){
printf ("%d", 10!=10);
}
output : 0

(i) 10!=10 : false


hence, the statement is false.

- #include<stdio.h>
void main (){
printf ("%d", 20!=10);
}
output : 1

(i) 20!=10 : true


hence, the statement is true.

the value (or)output of every relational operator is either 0 or 1

note : unary operator have always higher priority than binary operators

chapters Page 27
priority operators associativity

high +, - (unary)

X , / , % (arithmatic)
l to R
+ , - (binary)

< , <= , > , => (relational)


== , !=

low = (assignment) R to l

- #include<stdio.h>
void main (){
int a;
a = 10<5==3!=4<7>2;
printf ("%d",a);

a=10<5==3!=4<7>2
a=(10<5)==3!=4<7>2

a=0==3!=(4<7)>2
a=0==3!=1>2

a=0==3!=(1>2)
a=0==3!=0

a=(0==3!)=0
a=0!=0

a=(0!=0)
a=0

output : 0

- #include<stdio.h>
void main (){
int i;
i = printf("pankaj")

chapters Page 28
i = printf("pankaj")
printf ("%d",i);
}

output : pankaj6

int i;
i = printf("pankaj")
printf ("%d",i);
(i) first printf will print pankaj
(ii) now pankaj have 6 symbols and 6 will be assigned to i

- #include<stdio.h>
void main (){
printf ("%d",printf("GATE2025"));
}
output : GATE20258

- #include<stdio.h>
void main (){
int a;
a= printf ("%d",printf("%d", printf("GATE2025")));
}

output : GATE202581

(i)printf ("%d",printf("%d",printf("GATE2025")))
- GATE2025 (8 symbols printed)

(ii)printf ("%d",printf("%d, 8))


- 8 (1 symbol printed)

(iii)printf("%d",1)
-1

- #include<stdio.h>
void main (){
int a;
a= printf ("%d",printf("\n%d", printf("GATE20242025")));
printf ("%d",a);
}
output : GATE20242025
chapters Page 29
output : GATE20242025
1231

(i)printf ("%d",printf("%d",printf("GATE20242025")))
- GATE20242025 (12 symbols printed)

(ii)printf ("%d",printf("\n%d, 12))


- 12 (2 symbol printed + 1 symbol of \n)

(iii)printf("%d",3)
-1

(v) logical operators

(a) logical AND (&&)


binary
(b) logical OR (||)
(c) logical not (!) unary

(a) logical AND (&&)

the output value is 1 if both operands are non-zero (true) otherwise the output value is 0.

a b a&&b output value

F F F 0
F F F 0
T F F 0
T T T 1

- #include<stdio.h>
void main (){
printf ("%",12 && 13.8);
}

output : 1

- #include<stdio.h>
void main (){
printf ("%",A && 3.2);
}

output : 1
A : 65 (ASCII)
chapters Page 30
A : 65 (ASCII)

- #include<stdio.h>
void main (){
printf ("%",0 && 3);
}

output : 0

- #include<stdio.h>
void main (){
printf ("%",13 && 0.0);
}

output : 0

(b) logical OR (||)

the output value is 1 if at-least one operand is non-zero (true) otherwise the output value is 0.

a b a||b output value

F F F 0
F T T 1
T F T 1
T T T 1

- #include<stdio.h>
void main (){
printf ("%",12 || 7.0);
}

output : 1

- #include<stdio.h>
void main (){
printf ("%",-12 || 13.7);
}

output : 1

- #include<stdio.h>

chapters Page 31
- #include<stdio.h>
void main (){
printf ("%",12.2 || 0);
}

output : 1

- #include<stdio.h>
void main (){
printf ("%",0 || 0.0);
}

output : 0

note : just like relational operators, the value (output) of every logical operator is also 0 or 1.

(c) logical NOT (!)

It is also called a negation operator, which means it takes the input operand and negates it

!5 = !(non-zero) = !true = false = 0


!5 = 0

!0 = !(zero) = !false = true = 1


!0 = 1

short circuit evaluation in AND operator

- #include<stdio.h>
void main (){
int a; this will never be evaluated
a = 0 && ___ ;

if the 1st operand is zero for logical AND, then the result is 0 irrespective of the value of
second operator.

- #include<stdio.h>
void main (){
int a;

chapters Page 32
int a;
a = 0 && printf("hello");
printf("%d",a);
}
output : 0
will never be evaluated

short circuit evaluation in OR operator


- #include<stdio.h>
void main (){
int a; this will never be evaluated
a = 5 || ____ ;
}

output : 1

if the 1st operand is non-zero for logical OR, then the second operand will not be evaluated.

(vi) modify operators

modify operators

increment(++) decrement(- -)

pre-increment post-increment pre-decrement post-decrement


++variable variable++ - - variable variable - -

pre- increment post- increment pre- decrement post- decrement


#include<stdio.h> #include<stdio.h> #include<stdio.h> #include<stdio.h>
void main (){ void main (){ void main (){ void main (){
int a=5, b; int a=5, b; int a=5, b; int a=5, b;
b = ++a ; b = ++a ; b=--a; b=a--;
printf ("%d", a); printf ("%d", a); printf ("%d", a); printf ("%d", a);
printf ("%d", b); printf ("%d", b); printf ("%d", b); printf ("%d", b);
} } } }

b = ++a ; b = a++ ; b=--a; b=a--;


(i) first increase the value of (i) first use the value of variable (i) first decrease the value of (i) first use the value of variable
variable - b=a variable - b=a
- a = a+1 5 6 G 5 - a=a-1 5 4 G 5
a b a b

chapters Page 33
(i) first increase the value of (i) first use the value of variable (i) first decrease the value of (i) first use the value of variable
variable - b=a variable - b=a
- a = a+1 5 6 G 5 - a=a-1 5 4 G 5
a b a b
(ii) used the updated value. (ii) then updated value. (ii) used the updated value. (ii) then updated value.
(in expression/assignment) (in expression/assignment) (in expression/assignment) (in expression/assignment)
- b=a - a = a+1 - b=a G 4 - a=a-1 5 4
G 6 5 6
a a a a

output : 6 6 output : 6 5 output : 4 4 output : 5 4

note : if you using modify operator more than once in a sequence point then it is compiler dependent.

- #include<stdio.h>
void main (){
int a=0, b=2, c;
c = a++ && - - b;
printf ("%d", a, b, c);
}
c = a++ && - - b;

(i) a++ 0 2 G
- use the value.
a b c
- then increase it.
c = 0 && - - b 1 0 2 G

short circuit evaluation a b c


c=0 1 2 0

output : 1 2 0 a b c

- #include<stdio.h>
void main (){
a = pf("pankaj")||pf("rawan") && pf("hai")
printf ("%d", a);
}

a=pf("pankaj")||pf("rawan")&& pf("hai")

a=pf("pankaj")||pf("rawan")&& pf("hai") pankaj


6

a=6||pf("rawan")&& pf("hai") short circuit evaluation

a=1
output : pankaj1

chapters Page 34
priority operators associativity

high +, - (unary)

X , / , % (arithmatic)

+ , - (binary)
l to R
< , <= , > , => (relational)
== , !=

&&
|| (logical)

low = (assignment) R to l

binary number
0 to 1 0 = 2x + 0 = 2x
x (2 times of old value + 0)
if we add 0

0 to 1 1 = 2x + 1 = 2x
x (2 times of old value + 1)
if we add 1

(v) bitwise operators : perfomed on bits

(a) bitwise AND (&)


(b) bitwise OR(|)
(c) bitwise XOR (^) (binary)
(d) bitwise left shift (<<)
(e) bitwise right shift (>>)
(f) bitwise not (~) (unary)

(a) bitwise AND (&)

the output value is 1 if both operands are non-zero (true) otherwise the output value is 0.

chapters Page 35
a b a& b

0 0 0
0 1 0
1 0 0
1 1 1

- #include<stdio.h>
void main (){ 7 : 0000 0000 0000 0111
int a =7, b = 10, c; 10 : 0000 0000 0000 1010
c = a& b o/p: 0000 0000 0000 0010
printf ("%d",c);
}
output : 2

(b) bitwise OR(|)

the output value is 1 if one operand is non-zero (true) otherwise the output value is 0.

a b a |b

0 0 0
0 1 1
1 0 1
1 1 1

- #include<stdio.h>
void main (){ 7 : 0000 0000 0000 0111
int a =7, b = 10, c; 10 : 0000 0000 0000 1010
c = a |b o/p: 0000 0000 0000 1111
printf ("%d",c);
}
output : 15

(c) bitwise XOR (^)

the output value is 0 if both operands are same otherwise the output value is 1.

chapters Page 36
a b a ^b

0 0 0
0 1 1
1 0 1
1 1 0

- #include<stdio.h>
void main (){ 7 : 0000 0000 0000 0111
int a =7, b = 13, c; 13 : 0000 0000 0000 1101
c = a^b o/p: 0000 0000 0000 1010
printf ("%d",c);
}
output : 10

(d) bitwise left shift (<<) (binary operator)

X << N 'N' times

shift 'X' left side

5 << 1 '1' time

shift '5' left side

- #include<stdio.h>
void main (){
int a, b;
a = 5;
b = a << 1
printf ("%d%d",a,b);
}

5: 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
a << 1 shifting to left side for one time

10 : 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0

chapters Page 37
10 : 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0

0 filled space with 0


(popped out)

- #include<stdio.h>
void main (){
int a, b;
a = 5;
b = a << 2;
printf ("%d%d",a,b);
}

5: 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
a << 2 shifting to left side for two time

first time :
10 : 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0

0 filled space with 0


(popped out)

second time :

20 : 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0

0 filled space with 0


(popped out)

in general :

a<<2 is a x 2 x 2
a x 22
5 x 22
5 x 4 = 20

(e) bitwise right shift (>>)

X >> N 'N' times

chapters Page 38
shift 'X' right side

5 >> 1 '1' time

shift '5' right side

- #include<stdio.h>
void main (){
int a, b;
a = 10;
b = a >>1
printf ("%d%d",a,b);
}

10 : 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
a >> 1 shifting to right side for one time

5: 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1

filled space with 0 0


(popped out)

- #include<stdio.h>
void main (){
int a, b;
a = 10;
b = a >> 2;
printf ("%d%d",a,b);
}

10 : 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
a << 2 shifting to right side for two time

first time :
5: 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1

filled space with 0 0


(popped out)

chapters Page 39
second time :

2: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0

filled space with 0 1


(popped out)

in general :
a>>2 is a/22
10/4 = 2

note :
%d : integer
%0 : octal
%x : hexadecimal

literal
decimal
int a = 31;
prefix (compiler)

int a = 027

written in octal

- #include<stdio.h>
void main (){
int a = 027 (2x81)+(7x80)
printf ("%d,a); 16+7=23
}

output : 23

prefix (compiler)

int a = 0xace

written in hexadecimal

- #include<stdio.h> a = 10
void main (){ b = 11 (10x162)+(12x161)+(14x160)
c = 12
chapters Page 40
- #include<stdio.h> a = 10
void main (){ b = 11 (10x162)+(12x161)+(14x160)
int a = 0xace c = 12 (10x256)+(12x16)+14
printf ("%d,a); d = 13 2560+192+14 = 2766
} e = 14

output : 2766

- in 2's complement we focus on 0's

11110110
= -23 - 20 - 1
= -8 -1 -1

= -10

(f) bitwise not (~)

- #include<stdio.h>
void main (){
int a=48, b;
b = ~a;
printf ("%d,b);
}
output : -49
48 : 0000 0000 0011 0000
~a : 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1

written in 2's complement

= -25 -24 -1 = -25 -24 -1


= -32 -16 -1 (or) = -(25+24+1)
= -49 = -(32+16+1)
= -(48+1)
= -(a+1)

- #include<stdio.h>
void main (){
int a=-10;
printf ("%d,~a);
}

output : +9
chapters Page 41
output : +9

= -(a+1)
= -(-10+1)
= -(-9)
= +9

questions :

- #include<stdio.h>
void main (){
int a=028; (octal 0 to 7)
printf ("%d,a);
}

output : compiler error

- #include<stdio.h>
void main (){
int a, b, c;
a = -1 > 1; integer have both negative and positive
b = -1L > 1; integer have both negative and positive
c = -1u > 1; unsigned integer have only positive, so -1 will be a large positive number
printf ("%d%d%d,a,b,c);
}
output : 0 0 1

- #include<stdio.h>
void main (){
int a;
a = printf("pankaj")>>1;
printf ("%d,a);
}
output : pankaj3

a = printf("pankaj")>>1;
a = 6>>1;
6/21 = 3
a=3

- #include<stdio.h>
void main (){
int i=-1, j=-1, k=0, l=2, m;
chapters Page 42
int i=-1, j=-1, k=0, l=2, m;
m = i++ && j++ && k++ || l++;
printf ("%d %d %d %d,i, j, k, l, m);
}
output : 0 0 1 3 1

m = i++ && j++ && k++ || l++; -1 -1 0 2 G

i j k l m

m = (((-1 && -1) && 0) || 2); 0 0 1 3 G

i j k l m

m = ((1 && 0) || 2);


m = (0|| 2);

m=1 0 0 1 3 1

i j k l m

- #include<stdio.h>
void main (){
int x=10, y=5, d, q;
p = x>9;
q = x>3 && y!=3;
printf ("%d %d ,p , q);
}

output : 1 1
- p = x>9;
p = 10>9;
p=1

- q = x>3 && y!=3;


q = 10>3 && 5!=3;
q = 1 && 1;
q=1

(g) ternary operator (?:)

format : exp1 ? exp2 : exp3

evaluated first and if its value is non-zero

chapters Page 43
evaluated first and if its value is non-zero
otherwise the value (o/p) is exp3
(true) then the value (o/p) is exp2

example :

- #include<stdio.h>
void main (){
int a;
a = 10 ? 20 : 30;
printf ("%d, a);
}
output : 20
a = 10 ? 20 : 30;

a = 1 ? 20 : 30; (true)

- #include<stdio.h>
void main (){
int a;
a = 5<2 ? 20 : 30;
printf ("%d, a);
}
output : 30

a = 5<2 ? 20 : 30;
a = 0 ? 20 : 30;

- #include<stdio.h>
void main (){
int a;
a = 20>100 ? 100 : !2 != 3>50 ? 300 : 400;
printf ("%d, a);
}
output : 400

nested
(i) jitne colons(:) utne question(?)
(ii) right to left, colon apne nearest free question mark ke sath attach hoga.

a = 20>100 ? 100 : !2 != 3>50 ? 300 : 400;

chapters Page 44
a = 20>100 ? 100 : !2 != 3>50 ? 300 : 400;
exp1

a = 20>100 ? 100 : !2 != 3>50 ? 300 : 400;


exp2

a = 20>100 ? 100 : !2 != 3>50 ? 300 : 400;


exp3

a = 20>100 ? 100 : !2 != 3>50 ? 300 : 400;


a = 0 ? 100 : !2 != 3>50 ? 300 : 400;

a = !2 != 3>50 ? 300 : 400;

a = !2 != 3>50 ? 300 : 400;


exp1

a = !2 != 3>50 ? 300 : 400;


exp2

a = !2 != 3>50 ? 300 : 400;


exp3

a = !2 != 3>50 ? 300 : 400;


a = (!2 != (3>50)) ? 300 : 400;
a = (0 != 0) ? 300 : 400;
a = 0? 300 : 400;
a = 400

(h) sizeof
- unary
- works during compile time
- memory in byte
- returns in unsigned int because size cannot be negative

- #include<stdio.h>
void main (){
int a=2, b=4;
printf ("%d, size of (3)); integer type(integer).
}

chapters Page 45
output : 4

- #include<stdio.h>
void main (){
int a=2, b=4;
printf ("%d, size of (10/2+3)); (int/int + int)(expression)
}

output : 4

- #include<stdio.h>
void main (){
int a=2, b=4;
printf ("%d, size of (a)); integer type (variable)
}

output : 4

- #include<stdio.h>
void main (){
int a=2, b=4;
printf ("%d, size of (int)); integer type (data type)
}

output : 4

size of can be
- data type
- literal
- variable
- expression

chapters Page 46
chapter - 3 (flow control statement)
Saturday, August 10, 2024 12:35 AM

flow control statement

statements that control the flow of execution of statements.

(i) selection statements


- if
- else if
- if-else if else
- switch statement

(ii) iterative statements (repititon)


loops :
- for loop
- while loop
- do while loop

(iii) jump statement


- continue
- break
- exit
- return

(i) selection statements

(a) if
the if in C is the decision-making statement. It consists of the test condition in if block or
body. If the given condition is true only then the if block will be executed.

if(condition/expression) if there are no brackets after


{ (condition/expression) then by
// if body
// Statements to execute if condition is true default the scope is till first semi
} scope of 'if' colon.

with bracets - without bracets -


if (condition/expression){ if (condition/expression) if (condition/expression){
s3; s3; s3; }
s4; s4; s4;
}

chapters Page 47
s3; s3; s3; }
s4; s4; s4;
}

- #include<stdio.h>
void main (){
s1;
s2;
if (condition/expression){
s3; Statements to execute if condition is true
s4;
}
s5;
s6;
}

true : S1, S2, S3, S4, S5, S6


false : S1, S2, S5, S6

- #include<stdio.h> - #include<stdio.h>
void main (){ void main (){
s1; s1;
s2; s2;
if (true){ if (false){
s3; s3;
s4; s4;
} }
s5; s5;
s6; s6;
} }

- #include<stdio.h>
void main (){
pf ("1");
if (2<3){ true
pf ("2");
pf ("3");
}
pf ("4");
}

output : 1 2 3 4

chapters Page 48
- #include<stdio.h>
void main (){
pf ("hey");
if (!20)
pf ("bhagwan"); no brackets
pf ("bacha lo");
pf ("is rawan se");
}

output : heybachaloisrawanse
pf ("hey");
if (!20){ zero(false)
pf ("bhagwan");}
pf ("bacha lo");
pf ("is rawan se");

- #include<stdio.h>
void main (){
if (){ syntax error
pf ("bhagwan");}

- #include<stdio.h>
void main (){
int i=4;
if (i<2); no brackets but semi colon is instanlty after if
pf ("pankaj");} if (i<2){
;
}

output : pankaj

write a program to read a number and if the number is even then print "pankaj"

- #include<stdio.h>
void main (){
int a; to store a number (int) we took a variable "a"
printf ("enter a number"); to take the input from keyboard
scanf ("%d,&a"); to store a number in the location "a"
if (a%2 ==0){ if a is divisible by 2 that gives remainder 0 (even no.)
printf ("pankaj");}
}

chapters Page 49
last bit of even : 0
last bit of odd : 1

another way :

- #include<stdio.h>
void main (){
int a; to store a number (int) we took a variable "a"
printf ("enter a number"); to take the input from keyboard
scanf ("%d,&a"); to store a number in the location "a"
if (a&1==0){ if a anding 1 that is equal to 0 (even no.)
printf ("pankaj");}
}

a=3 a=2
- #include<stdio.h> - #include<stdio.h>
void main (){ void main (){
int a; int a;
printf ("enter a number"); printf ("enter a number");
scanf ("%d,&a"); scanf ("%d,&a");
if (3&1==0){ if (2&1==0){
printf ("pankaj");} printf ("pankaj");}
} }

3 : 0000 0000 0000 0011 2 : 0000 0000 0000 0010


1 : 0000 0000 0000 0001 1 : 0000 0000 0000 0001
0000 0000 0000 0001 0000 0000 0000 0000
if (3&1==0){ if (2&1==0){
printf ("pankaj");} printf ("pankaj");}

if (1==0){ if (0==0){
printf ("pankaj");} printf ("pankaj");}

if (0){ if (1){
printf ("pankaj");} printf ("pankaj");}

output : nothing output : pankaj

(b) if-else
It is an extension of the 'if' in C that includes an 'else' block along with the already existing
if block.

chapters Page 50
if (condition/expression) {
// code executed when the condition is true
}
else {
// code executed when the condition is false
}

- #include<stdio.h>
void main (){
s1;
s2;
if (condition/expression){
s3; Statements to execute if condition is true
s4;
}
else {
s5; Statements to execute if condition is false
s6;
}
s7;
s8;
}

true : S1, S2, S3, S4, S7, S8


false : S1, S2, S5, S6, S7, S8

- #include<stdio.h> - #include<stdio.h>
void main (){ void main (){
s1; s1;
s2; s2;
if(true){ if (false){
s3; s3;
s4; s4;
} }
else { else {
s5; s5;
s6; s6;
} }
s7; s7;
s8; s8;
} }

(i) either code 1 (or) code 2 will execute.


(ii) both will not execute.

chapters Page 51
(ii) both will not execute.
(iii) exactly one of them will execute.
(iv) you cannot use else without if.

- #include<stdio.h>
void main (){
if (!2+3) if (!2+3){
printf ("1"); printf ("1")}
printf ("2"); printf ("2"); error
else else
printf ("3"); printf ("3");
} }
else cannot be without if, else should be after the scope of if ends.
there cannot be statements between if and else.

write a program to take a integer and if the number is even then print "1", otherwise print "0".

- #include<stdio.h>
void main (){
int a; to store a number (int) we took a variable "a"
printf ("enter a number"); to take the input from keyboard
scanf ("%d,&a"); to store a number in the location "a"
if (a&1==0){ if a anding 1 that is equal to 0 (even no.)
printf ("1");}
else {
printf ("0");}
}

(c) if-else-if

one of the conditions controlling the if is true, the statement associated with that if is executed,
and the rest of the C else-if ladder is bypassed.

if none of the conditions is true, then the final else statement will be executed.

if (condition/expression)
statement;
else if (condition/expression)
statement;
.
.
else
statement;

chapters Page 52
- #include<stdio.h>
void main (){
s1;
s2;
if (condition/expression){
s3; Statements to execute if condition is true
s4;
}
else if (condition/expression){
s5; Statements to execute if condition is true
s6;
}
else {
s7; Statements to execute if condition is false
s8;}
}

- #include<stdio.h> - #include<stdio.h>
void main (){ void main (){
s1; s1;
s2; s2;
if(true){ if(false){
s3; s3;
s4; s4;
} }
else if (con/exp) { else if (true) {
s5; s5;
s6; s6;
} }
else { else {
s7; s7;
s8;} s8;}
} }
output : s1, s2, s3, s4 output : s1, s2, s5, s6

- #include<stdio.h>
void main (){
s1;
s2;
if(false){
s3;
s4;
}
else if (false) {
s5;

chapters Page 53
}
else if (false) {
s5;
s6;
}
else {
s7;
s8;}
}
output : s1, s2, s7, s8

code: largest among two numbers

- #include<stdio.h> - #include<stdio.h>
void main (){ void main (){
int a,b,max; int a,b,max;
printf ("enter a number") printf ("enter a number")
scanf ("%d %d, &a, &b") scanf ("%d %d, &a, &b")
max = a>b?a:b if (a>b)
printf ("%d", max); max = a;
} else
max = b;
}

code: largest among three distinct number

- #include<stdio.h> - #include<stdio.h>
void main (){ void main (){
int a,b,c; int a,b,c, largest;
printf ("enter a number") printf ("enter a number")
scanf ("%d %d %d, &a, &b, &c") scanf ("%d %d %d, &a, &b, &c")
if (a>b && a>c){ largest=(a>b && a>c)?a:b>c?b:c
printf("%d is the largest, a);
else if (b>c){
printf("%d is the largest, b);
else { a is greater than b and
printf("%d is the largest, c); a is greater than c then
the answer is a

b is greater than c then the


answer is b otherwise c

(ii) iterative statements (repititon)


(a) for loop
starts updating
from 1 till 5

chapters Page 54
starts updating
from 1 till 5

for (roll = 1; roll <=5; roll = roll+1)


{
scope printf(''aryan")
}
updating
the roll no.

checks if 1<=5,
starts yes then it will
from 1 go to printf

for roll = 1; roll<=5 for (roll = 1; roll <=5; roll = roll+1)


{
printf executes printf(''aryan")
}
after
output : aryan printf, it
updates
the roll no.

checks if 2<=5,
starts yes then it will
from 2 go to printf

for roll = 2; roll<=5 for (roll = 2; roll <=5; roll = roll+1)


{
printf executes printf(''aryan")
}
after
output : aryan printf, it
updates
the roll no.

checks if 3<=5,
starts yes then it will
from 3 go to printf

for roll = 3; roll<=5 for (roll = 3; roll <=5; roll = roll+1)


{
printf executes printf(''aryan")
}
after
output : aryan printf, it
updates
the roll no.

chapters Page 55
checks if 4<=5,
starts yes then it will
from 4 go to printf

for roll = 4; roll<=5 for (roll = 4; roll <=5; roll = roll+1)


{
printf executes printf(''aryan")
}
after
output : aryan printf, it
updates
the roll no.

checks if 1<=5,
starts yes then it will
from 5 go to printf

for roll = 5; roll<=5 for (roll = 5; roll <=5; roll = roll+1)


{
printf executes printf(''aryan")
}
after
output : aryan printf, it
updates
the roll no.

checks if 1<=5,
starts no, then it will
from 6 stop executing

for roll = 6; roll<=5 for (roll = 6; roll <=5; roll = roll+1)


{
printf(''aryan")
output : aryan }

final output : aryan aryan aryan aryan aryan

(1) (2)
(4)
format : for (initialization; condition; inc/dec)
{
(3) code
}

(2), (3), (4) repeats


chapters Page 56
(2), (3), (4) repeats

loop ke ek baar chalne ko iteration kehte hai

(1) (2)
(4)
format : for (expression 1; expression 2; expression3)
{
(3) code
}

iteration :
exp 1 : 1 time
exp 2 : true = code executes then goes to exp3
updates the value
exp 2 : true = code executes then goes to exp3
updates the value
.
.
repetition : exp(2) - code - exp (3)

- #include<stdio.h>
void main (){
int i=1;
for (7; i<=3; 13)
{
printf ("2");
i=i+2;
}

it is an expression
{ it is an expression

int i=1;
for (7; 1<=3; 13)
{ 1<=3
printf ("2"); (a)2
i=i+2; (b) i = 1+2 = 3
}
output : 2

it is an expression
{ it is an expression

int i=3;
chapters Page 57
it is an expression
{ it is an expression

int i=3;
for (7; 3<=3; 13)
{ 3<=3
printf ("2"); (a)2
i=i+2; (b) i = 3+2 = 5
}
output : 2

it is an expression
{ it is an expression

int i=5;
for (7; 5<=3; 13)
{ 5<=3
printf ("2");
i=i+2;
}

final output : 22

- #include<stdio.h>
void main (){
char ch;
for (ch=1; ch; ch++)
printf ("aryan");
printf("end");
}

{
ch = 1,2,3.....127,-128,-129..-1,0(false) - (255)
char ch;
for (ch=1; ch; ch++)
printf ("aryan"); scope till first semi-colon
by default signed printf("end");
}
signed : 0 to 255.

output : aryan(255times)end

- #include<stdio.h>
void main (){
char ch;
for (ch=1; ch; ch+2)
printf ("aryan");
}
chapters Page 58
}

{
ch = 1,3,5.....127,-129..-1,1, 3... (0 skipped)
char ch;
for (ch=1; ch; ch+2)
printf ("aryan"); scope till first semi-colon
by default signed }
signed : 0 to 255.

output : aryan(infinite times in odd)

- #include<stdio.h>
void main (){
int i =1;
for (printf("1"); i<=5; printf("3"))
{
printf ("2");
i++
}
}

{
int i =1;
for (printf("1"); 1<=5; printf("3"))
{
printf ("2");
i++
}
}

output : 123

{
int i =2;
for (printf("1"); 2<=5; printf("3"))
{
printf ("2");
i++
}
}
output : 23

chapters Page 59
{
int i =3;
for (printf("1"); 3<=5; printf("3"))
{
printf ("2");
i++
}
}

output : 23

{
int i =4;
for (printf("1"); 4<=5; printf("3"))
{
printf ("2");
i++
}
}

output : 23

{
int i =5;
for (printf("1"); 5<=5; printf("3"))
{
printf ("2");
i++
}
}

output : 23

{
int i =6;
for (printf("1"); 6<=5; printf("3"))
{
printf ("2");
i++
}
}

output : 1

final output : 12323232323

chapters Page 60
note : all 3 expressions are optional

- #include<stdio.h>
void main (){
int i =-1;
for (i++; i++; i++)
{
printf ("pankaj");
}
}

{ i++ :
int i =-1; (a) use the value
for (-1; i++; i++) -1
{ (b) update the value
printf ("pankaj"); 0
}
output : nothing

- #include<stdio.h>
void main (){
int i =-1;
for (i++; ++i; i++)
{
printf ("pankaj");
}
}

{ i++ :
int i =-1; (a) use the value
for (-1; ++i; i++) -1
{ (b) update the value
printf ("pankaj"); 0
}
output : pankaj (infinite times-odd) ++i : i++ :
(a) update the value (a) use the value
i : 1, 3, 5...0(skipped) 1
1
(b) use the value (b) update the value
1 2

- #include<stdio.h>
chapters Page 61
- #include<stdio.h>
void main (){
char i =-1;
for (i++; ++i; i++)
{
printf ("pankaj");
}
}

{ i++ :
char i =-1; (a) use the value
for (-1; ++i; i++) -1
{ (b) update the value
printf ("pankaj"); 0
}
output : pankaj (infinite times-odd) ++i :
(a) update the value
i : 1, 3, 5...0(skipped)
1
(b) use the value
1

i++ :
(a) use the value
1
(b) update the value
2

- #include<stdio.h>
void main (){
for (i=1; i<=n; i+2)
{
printf ("pankaj");
}
}
{
for (i=1; i<=n; i+2)
{
printf ("pankaj");
}
}
output : n
2

chapters Page 62
- #include<stdio.h> - #include<stdio.h>
void main (){ void main (){
for (i=1; i<=n; i x 2) for (i=1; i<=100; ix2)
{ {
printf ("pankaj"); printf ("pankaj");
} }
} }

i = 1 : pankaj (2 0) i = 1 : pankaj
i = 2 : pankaj (2 1) i = 2 : pankaj
i = 4 : pankaj (2 2) i = 4 : pankaj
i = 8 : pankaj (2 3) i = 8 : pankaj
i = 16 : pankaj (24) i = 16 : pankaj
i = 32 : pankaj (2 5) i = 32 : pankaj
i = 64 : pankaj (26) i = 64 : pankaj
. i = 128 X
.
i = n(2k)
last value of i : 2 k

printf will print k+1 times

what is k?

2k<=n
log (2k)<=logn
k log 2<=logn

k<= logn
log2

k<=log2n

k = log2n
1 + log2n

nested loop :

- #include<stdio.h>
void main (){
for (i=1; i<=3; i++)
{
for (j=1; j<=4; j++) code for outer loop

chapters Page 63
for (i=1; i<=3; i++)
{
for (j=1; j<=4; j++) code for outer loop
{
printf ("pankaj");
} code for inner loop
}
}

outer loop

for (i=1; 1<=3; i++) i=1


{
for (j=1; j<=4; j++) j=1 : pankaj
{ j=2 : pankaj
inner loop
printf ("pankaj"); j=3 : pankaj
} j=4 : pankaj
} j=5:X
outer loop

for (i=2; 2<=3; i++) i=2


{
for (j=1; j<=4; j++) j=1 : pankaj
{ j=2 : pankaj
inner loop
printf ("pankaj"); j=3 : pankaj
} j=4 : pankaj
} j=5:X

outer loop

for (i=3; 3<=3; i++) i=3


{
for (j=1; j<=4; j++) j=1 : pankaj
{ j=2 : pankaj
inner loop
printf ("pankaj"); j=3 : pankaj
} j=4 : pankaj
} j=5:X

outer loop

for (i=4; 4<=3; i++) will not work


{
for (j=1; j<=4; j++)
{
inner loop
printf ("pankaj");
}
}

chapters Page 64
output : pankaj pankaj pankaj pankaj
pankaj pankaj pankaj pankaj
pankaj pankaj pankaj pankaj

- #include<stdio.h>
void main (){
for (i=1; i<=3; i++)
{
for (j=1; j<=4; j++)
{
printf ("%d%d%d,i,j");
}
printf("\n");
}
}

outer loop

for (i=1; 1<=3; i++) i=1


{
i=1 1<=3 j=1
for (j=1; j<=4; j++) j=1 1<=4
{ j=2 printf ("%d%d%d,i,j");
inner loop
printf ("%d%d%d,i,j"); 11
j=3
} j=4 j=2
2<=4
printf("\n"); j=5:X printf ("%d%d%d,i,j");
} 12
j=3
output : 11121314 3<=4
printf ("%d%d%d,i,j");
outer loop
13
j=4
for (i=2; 2<=3; i++) i=2
4<=4
{ printf ("%d%d%d,i,j");
14
for (j=1; j<=4; j++) j=1
{ j=2 . . .
inner loop
printf ("%d%d%d,i,j"); j=3 . . .
} j=4 . . .
printf("\n"); j=5:X
i=3 3<=3 j=1
} 1<=4
output : 21222324 printf ("%d%d%d,i,j");
31
outer loop
j=2
2<=4
for (i=3; 3<=3; i++) i=3 printf ("%d%d%d,i,j");
32
{
j=3
for (j=1; j<=4; j++) j=1 3<=4
{ j=2 printf ("%d%d%d,i,j");

chapters Page 65
32
{
j=3
for (j=1; j<=4; j++) j=1 3<=4
{ j=2 printf ("%d%d%d,i,j");
inner loop 33
printf ("%d%d%d,i,j"); j=3
j=4
} j=4 4<=4
printf("\n"); j=5:X printf ("%d%d%d,i,j");
} 34

output : 31323334

final output : 11121314


21222324
31323334

- #include<stdio.h>
void main (){
for (i=1; i<=3; i++)
{
for (j=1; j<=n; j++)
{
printf ("pankaj");
}
}
}

i=1
n times

i=2
n times

i=3
n times

final output : 3n times

- #include<stdio.h>
void main (){
for (i=1; i<=n; i++)
{
for (j=1; j<=3; j++)
{
printf ("pankaj");
}
}
}
chapters Page 66
}

i=n i=n i=n


-j=1 -j=1 -j=1
-j=2 -j=2 -j=2
-j=3 -j=3 -j=3

final output : 3n times

- #include<stdio.h>
void main (){
for (i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
{
printf ("pankaj");
}
}
}

i=n i=n i=n


-j=n - j = n . . . . . .. . . . - j = n

final output : (nxn)n2 times

- #include<stdio.h>
void main (){
for (i=1; i<=n; i++)
{
for (j=1; j<=n; j=j x 2)
{
printf ("pankaj");
}
}
}

i=n i=n i=n


- j = 1 + log2n - j = 1 + log2n . . . - j = 1 + log2n

final output : n(1 + log2n )

chapters Page 67
- #include<stdio.h>
void main (){
for (j=128; j>=1; j=j/2)
{
printf ("pankaj");
}
}

output : log2128 j= 128


7+1 = 8times j= 64
j= 32
j= 16
j= 8
j= 4
j= 2
j= 1

- #include<stdio.h> (dependent loop)


void main (){
for (i=1; i<=3; i++)
{ i=1 1<=3 j=1
1<=1
for (j=1; j<=i; j++) pankaj
{
printf ("pankaj"); i=2 2<=3 j=1
1<=2
} pankaj
} j=2
2<=2
pankaj

i=3 3<=3 j=1


final output : 1<=3
pankaj pankaj
j=2
pankaj pankaj 2<=3
pankaj pankaj pankaj pankaj
j=3
3<=3
pankaj

(b)while loop

- #include<stdio.h>
void main (){
while (expression/condition)
{
code
} true/non-zero(code executes)
chapters Page 68
while (expression/condition)
{
code
} true/non-zero(code executes)
}
false/zero(code does not execute)

- #include<stdio.h> - #include<stdio.h>
void main (){ void main (){
for (i=1; i<=n; i++) i=1
{ ≡ while (i<=n)
printf ("pankaj"); {
} printf ("pankaj");
} i = i+1
}

- #include<stdio.h>
void main (){
int i=1
while (++i<5)
{
printf ("%d,i");
}

int i=1 int i=2 int i=3 int i=4


while (2<5) while (3<5) while (4<5) while (5<5)
{ { { {
printf ("%d,i"); printf ("%d,i"); printf ("%d,i"); printf ("%d,i");
} } } }
output : 2 output : 3 output : 4 output : X

final output : 234

note
- the scope of while loop is also till first semi-colon.
- in for loop and while loop we pre-check the condition.

- #include<stdio.h>
void main (){
while ( ) it is mandatory to give
{ expression/condition in while loop
_
_
chapters Page 69
_
}

do while loop
in do while loop we dont have any pre-conditions, so even if the expression/condition is false,
it will execute the code at-least once.

- #include<stdio.h>
void main ()
do {
code
} while (0);
code executes once

even if the condition is false the


code will execute at-least once

for loop while loop while loop

number of iterations are number of iterations are not post-checking of condition


known in advance known in advance but some
criteria/conditions is there code will execute at-least one
for (i=1; i<=10, i++) time irrespective of wether the
{ while ( ) condition is true or false.
_ {
_ _ do {
} _ code
} } while (0);

code for sum code for product

- #include<stdio.h> - #include<stdio.h>
void main (){ void main (){
int n, i, sum; int n, i, prod;
printf ("enter a number"); printf ("enter a number");
scanf ("%d", &n); scanf ("%d", &n);
sum = 0; prod= 1;
for (i=1; i<=n; i++) for (i=1; i<=n; i++)
sum = sum + i; prod= prod x i;
printf("%d", sum); printf("%d", prod);
}
chapters Page 70
for (i=1; i<=n; i++) for (i=1; i<=n; i++)
sum = sum + i; prod= prod x i;
printf("%d", sum); printf("%d", prod);
} }

break and continue


(a) break
whenever break is encountered in a loop, it terminates the current loop.

- #include<stdio.h>
void main (){
for (i=1; i<=10; i++)
{
printf("hello");
break;
}
printf("end");
}

{
for (i=1; i<=10; i++) for i=1
{
printf("hello"); hello executes for i=1
break; break terminates the loop
}
printf("END");
}

output : helloEND

- #include<stdio.h>
void main (){
for (i=1; i<=10; i++)
{
if (i%4==0)
break;
printf("%d",i);
}

{
for (i=1; i<=10; i++)
{
if (i%4==0)
{
chapters Page 71
if (i%4==0)
{
break;
}
printf("%d",i);
}
output : 123

(b) continue

whenever continue is encountered it skip the remaining portion of current iteration and
continue with next iteration.

- #include<stdio.h>
void main (){
for (i=1; i<=10; i++)
{
continue;
printf("%d",i);
}

{
for (i=1; i<=10; i++)
{
continue; this skip to the next iteration so printf
printf("%d",i); will never execute.
}

- #include<stdio.h>
void main (){
for (i=1; i<=10; i++)
{
if (i%4==0)
continue;
printf("%d",i);
}

{
for (i=1; i<=10; i++)
{
if (i%4==0)
{
continue;
}
printf("%d",i);

chapters Page 72
printf("%d",i);
}

output : 123567910

- #include<stdio.h>
void main (){
for (i=1; i<=10; i++)
{
for ((i+j)%3==0)
{
continue;
printf("%d%d",i,j);
}
printf("\n");
}

{
for (i=1; i<=10; i++) i=1 i=2 i=3 i=4
{ j=1 j=1 j=1 j=1
for (j=1; i<=4; j++) (1+1)%3==0 (2+1)%3==0 (3+1)%3==0 (4+1)%3==0
{ 2%3==0 3%3==0 4%3==0 4%3==0
for ((i+j)%3==0) output : 11 output : X output : 31 output : 41
{
continue; i=1 i=2 i=3 i=4
} j=2 j=2 j=2 j=2
printf("%d%d",i,j); (1+2)%3==0 (2+2)%3==0 (3+2)%3==0 (4+2)%3==0
} 3%3==0 4%3==0 5%3==0 6%3==0
printf("\n"); output : X output : 22 output : 32 output : 42
}
i=1 i=2 i=3 i=4
j=3 j=3 j=3 j=3
final output :
(1+3)%3==0 (2+3)%3==0 (3+3)%3==0 (4+3)%3==0
111314
4%3==0 5%3==0 6%3==0 7%3==0
2223
output : 13 output : 23 output : X output : X
313234
414244
i=1 i=2 i=3 i=4
j=4 j=4 j=4 j=4
(1+4)%3==0 (2+4)%3==0 (3+4)%3==0 (4+4)%3==0
5%3==0 6%3==0 7%3==0 8%3==0
output : 14 output : X output : 34 output : 44

topic - switch statement

chapters Page 73
keyword : used to create selection statement with multiple choices
multiple choices are provided with another keyword (case)

switch (n) {

case 1: code we want to execute if the value of n is 1


break;

case 2: code we want to execute if the value of n is 1


break;

default: code we want to execute if the cases dosent match


break;

switch (2x5+3) {
case 1: block of statements
break;

case 13: block of statements this block will execute.


break;

default: block of statements


break;
}

important points about switch statement :


(i) break is optional.
(ii) expression : evaluates to be integer.
(iii) position of default does not matter, it can be anywhere (start, mid, end)
(iv) default is optional.
(v) duplicate case labels are not allowed.
(vi) case labels cannot be variables (a,b,c) and can only contains constant/literals.

- once the case label is matched, all the cases will be of no use and the code will execute sequentially

switch (1) {
case 1: block of statements
break;

case 13: block of statements


break;

case 18: printf("aryan")


break;
chapters Page 74
break;

switch (1) { switch (1) {


case 1: case :

case 13: case :


sequentially
case 18: printf("aryan") case : printf("aryan")
break; break;

output : aryan

- whatever code you want to execute must be present in after the colons of the case label and before the
break.

switch (i+3) {
case 1: printf("1");
printf("one");
break;
printf ("pankaj"); it never gets printed (ignored)

case 13: printf("2");


break;
}

- switch is based on expression/condition so it cannot be empty.

switch ( ) { cannot be empty


case 1: block of statements
break;

- if default executes, the rest of case lables will be of no use and code will execute sequentially.

int i = 3;
switch (i+3) {
case 5: printf("5");
break;

default: print ("0");

case 7: printf("7");
break;

int i = 3; int i = 3;
switch (6) { switch (6) {
case 5: printf("5");
chapters Page 75
int i = 3; int i = 3;
switch (6) { switch (6) {
case 5: printf("5"); case 5: printf("5");
break; break;

default: print ("0"); default: 0

case 7: printf("7"); case: 7


break; break;

output : 07

chapters Page 76
chapter - 4 (functions and storage classes)
Wednesday, August 14, 2024 5:12 AM

functions and storage classes

functions : functions are designed to perform the specific task.


built in functions : code written by dennis ritchie and we are currenlty using them.
(i) printf
(ii) scanf ()
software team
(i) only one person is working (ii) every person is working

CS/IT MECH.
CS/IT EE CIVIL MECH.
EE

CIVIL

- no proper resource utilisation - proper resource utilisation


- more time - less time

functions are designed to perform the specific task.


there should be variable to
for example :
hold upcoming values.
#include<stdio.h> int mul (int x, int y) mul(a,b)
void main () { { here we are sending
the a and b values
int a=10, b=20, ans; int temp ;
ans = mul(a,b); temp = x * y 20
printf ("%d",ans); return temp ;
} } 10 Y
temp to store the result of X
200 multipication
ans variable to store the temp
200 result
ans

#include<stdio.h>
void main () {
printf ("%d",a);
}
compiler error because there is no variable a, compiler
will not understand without declaration.

chapters Page 77
note : compilation is done before execution, it is done from top to bottom but execution starts from
main.

concept - forward declaration

void main () { compilation error


printf ("hello"); error during compile time.
}

compiler don't know about printf we have to provide the information regarding printf so we have to
give the forward declaration to compiler.

to avoid compilation error :

#include<stdio.h> forward declaration for printf


void main () {
printf ("hello");
}

function of the header


#include<stdio.h> int mul (int x, int y)
void main () { {
int a=10, b=20, ans; int temp ;
ans = mul(a,b); temp = x * y body of the function
printf ("%d",ans); return temp ;
} }
compiler don't know about this function
so have to give compiler the information
regarding the function.

#include<stdio.h>
int mul (int, int) forward declaration (no memory is allocated here)
void main () {
int a=10, b=20, ans;
ans = mul(a,b);
printf ("%d",ans);
}

arguments

note : if we want to use a function before its definition to avoid compilation error, you
provide forward declaration, the forward declaration is for information purpose.

- arguments : jo values function leta hai


- return values : jis type ki value return krta h

chapters Page 78
but, if we are defining the function before main then the forward declaration is not needed.

#include<stdio.h>
int mul (int x, int y) we dont need the forward declaration
{
int temp; we have defined the function here
temp = x * y;
return temp ;
}
void main () {
int a=10, b=20, ans;
ans = mul(a,b);
printf ("%d",ans);
}

compiler no error execution


compilation only then only
checks the error

by default the return type of function is int

#include<stdio.h> int mul (int x, int y)


mul (int, int) {
void main () { int temp ;
int a=10, b=20, ans; temp = x * y
ans = mul(a,b); return temp ;
printf ("%d",ans); }
}

#include<stdio.h> #include<stdio.h>
no forward declaration no forward declaration
void main () { void main () {
int a=10, b=20, ans; int a=3;
ans = mul(a,b); double b;
printf ("%d",ans); b = f(a);
} printf ("%lf",ans);
}

compiler will store the return compiler will store the return
type of mul() as int type of mul() as int
compiler got int so compiler compiler got
int mul (int x, int y) is happy because it matched double f (int x) double so compiler
{ with information it stored. { is not happy and
int temp ; double y = 2.38; will give error
chapters Page 79
compiler got int so compiler compiler got
int mul (int x, int y) is happy because it matched double f (int x) double so compiler
{ with information it stored. { is not happy and
int temp ; double y = 2.38; will give error
temp = x * y return ; because it does not
return temp ; } match with
} information it
stored.
so, this confusion and mis-information can be avoided with forward declaration.

[Link] we compile a program without main () ?


yes, we can compiler a program without main.

[Link] we execute a program without main?


no, we cannot execute without main, the execution starts with main.

(i) if a function return value


store - use.
does not store - no use.

#include<stdio.h> #include<stdio.h>
void main () { void main () {
printf ("hello"); int i;
} i = printf ("hello");
printf ("%d,i");
print value does not save here so we
}
cannot use the value returned by printf
print value does save here in i so we can
use the value returned by printf
output : hello
output : hello5

there should be variable to


for example :
hold upcoming values.
#include<stdio.h> int mul (int x, int y) mul(a,b)
void main () { { here we are sending
the a and b values
int a=10, b=20, ans; int temp ;
mul(a,b); temp = x * y 20
} return temp ;
} 10 Y
temp to store the result of X
no error but there is no variable 200 multipication
to store the value returned. temp

chapters Page 80
concept - how function works

#include<stdio.h> int ADD (int x, int y)


void main () { {
int a=10, b=20, ans; int temp ;
ans = ADD (a,b); temp = x + y ;
printf ("%d", ans); return temp ;
} }

void main () {
int a=10, b=20, ans; main ()
ans = ADD (a,b);
printf ("%d", ans); 10 20 G ADD () add performed and
stored into temp
}
ADD function called a b c 10 20 30
x y temp
int ADD (intx , int y)
{ ADD () main ()
int temp ;
temp = x + y ; 10 20 10 20 G
return temp ;
x y temp a b ans
}

now the value stored and temp returned to ans


and the activation record of ADD is cleared.

now the main is also cleared once the


operations are done.

ADD ()

10 20 30
x y temp
main () main ()

10 20 30 10 20 G 30
a b ans a b ans

system stack

final output : 30

concept : formal arguments and actual arguments

#include<stdio.h> int ADD (int x, int y) formal arguments


void main () { {
int a=10, b=20, ans; int temp ;
chapters Page 81
#include<stdio.h> int ADD (int x, int y) formal arguments
void main () { {
int a=10, b=20, ans; int temp ;
ans = ADD (a,b); temp = x + y ;
printf ("%d", ans); return temp ;
} }

actual arguments

swapping function :

#include<stdio.h> void SWAP (int x, int y)


void main () { {
int a=10, b=20, ans; int temp ;
printf ("before swap"); temp = x ;
printf ("a=%d, b=%d", a,b); x=y;
swap (a,b); y = temp ;
printf ("a=%d, b=%d", a,b); }
}

10 20 10 20

a b x y temp
temp = x ; but as soon as the
function ends the
20 10
memory returned
x y temp from stack and x, y
and temp does not
x=y; exist anymore.
20 10
so, a and b does not
x y temp change!
y = temp;
all changes are done
20 10 at formal arguments
and not in actal
x y temp arguments.

note : changes performed on formal arguments will not be reflected back to actual arguments
(parameters are passed by value) - call by value

chapters Page 82
topic : storage classes

(i) scope : part of code in which a variable is visible (where we can directly access the variable)

(ii) lifetime : duration (active/alive)


(iii) default value : if we do not initialise a variable then what is its default value.
(iv) storage area : where a variable is stored.

scope :

(a) block (b) file (c) multiple files

{ void f1 () { scope of variable : multiple files


__ (local t0 f1) int a;
__ }
__ void f2 () {
(local t0 f2) int b;
}
}

auto / local

(i) scope : block in which they are defined/declared.

(ii) lifetime : block in which they are defined/declared.


(iii) default value : garbage.
(iv) storage area : stack.

#include<stdio.h>
void main () {
int a=10;
++ a ;
{
int a = 20;
++a;
printf ("%d", a);
}
++a ;
printf ("%d" a);
}

chapters Page 83
void main () {
int a=10;
++ a ;
{
int a = 20;
++a; a a
printf ("%d", a);
}
++a ;
printf ("%d" a);
}

11 21
10 20 11 20

12
11 21

output : 12 and 21

note : by default local scope is more preferred

in a same scope, declaring the same variable twice is not valid.

void main () {
chapters Page 84
void main () {
int a;
int a; invalid
}

#include<stdio.h>
void main () {
int a=10;
++ a ;
{
++a;
printf ("%d", a);
}
++a ;
printf ("%d" a);
}

void main () {
int a=10;
++ a ;

{
++a; a
main sub scope printf ("%d", a);
scope }
++a ;
printf ("%d" a);
}

11 12
10 11

chapters Page 85
13
12

output : 12 and 13

note : main scope variable is accesible within sub-scope

#include<stdio.h>
void main () {
int a=10;
++ a ;
{
++a;
printf ("%d", a);
{
int b = 5;
++a ;
printf ("%d" a+b);
}
++b ;
printf ("%d%d" a,b);
}
++a ;
printf ("%d%d" a);
}

#include<stdio.h>
void main () {
int a=10;
++ a ; all modify operators are working on
{ main scope variable.
++a; 12
printf ("%d", a);
{
int b = 5; we can access "a" here because this sub-
chapters Page 86
printf ("%d", a);
{
int b = 5; we can access "a" here because this sub-
++a ; scope is a part of main scope.
printf ("%d" a+b); 13+5=18
}
++b ; invalid because this sub-sub
printf ("%d%d" a,b); scope is trying to access b of
} sub-scope.
++a ;
printf ("%d%d" a);
}

note : sub-scope variables are not accessible within main scope

auto : they are created automatically when we enter the block in which they are declared and destructed
automatically when we exit the block.

register variable

(i) scope : block in which they are defined/declared.

(ii) lifetime : block in which they are defined/declared.


(iii) default value : garbage.
(iv) storage area : CPU register / stack.

register int a;

requests
if grants if rejects

CPU register int a;


(it is limited) (treated as local)

- allocated only - if not available


if available

static variable

(i) scope : block in which they are defined/declared.

(ii) lifetime : program.

chapters Page 87
(iii) default value : 0
(iv) storage area : static area.
(i) value persists between different function calls.
(ii) no re-declaration.
(iii) they are created only once in program.

#include<stdio.h>
void fun() {
static int i = 0;
++i ;
printf ("%d", i);
}
void main (){
fun();
fun();
fun();
}

void fun() {
static int i = 0; (works on compile time, decision before
++i ; execution time)
printf ("%d", i);
}
0
void main (){
fun(); i
fun();
fun();
}

(i) fun()
++i 0 1

(ii) fun()
++i
1 2 value persist throughout the program
i

(iii) fun()
++i
2 3
i

output : 123

chapters Page 88
difference between local variable and static variable

void fun() { void fun() {


int a = 0; static int x = 0; (works on compile time,
++a ; ++x ; decision before execution
printf ("%d", a); printf ("%d", x); time)
} }
void main (){ void main (){ 0
fun(); fun(); x
fun(); fun();
fun(); fun();
} }

(i) fun() (i) fun()


int a = 0 0 ++a 0 1

++i a a

(ii) fun()
++a no redeclaration because value
0 1 1 2 persist throughout the program

a a

(ii) fun() (iii) fun()


int a = 0 ++a
0 2 3

++i a a

output : 1123
0 1 value does persist.
a

(iii) fun()
int a = 0 0

++i a

0 1
a

output : 111
value does not persist.

chapters Page 89
#include<stdio.h>
void fun() {
int a = 0;
static int b = 10;
++a ;
++b ; the scope of b is within the block
printf ("%d%d",a,b);
}
void main (){
int a = 10;
++a ;
++b ; error, not available
printf ("%d%d",a,b);
}

#include<stdio.h>
void fun() {
int a ;
static int b = a ; you cannot initialise the static variable by a
printf ("%d%d",a,b); variable (always initialise by literals)
}

note : static variable works on compile time.

global variable

(i) scope : external.

(ii) lifetime :
(iii) default value : 0
(iv) storage area : static area.

global variable is not inside any function, variables are outside all the functions in a global
variable.

#include<stdio.h> at this point


int x ; compiler knows compilation
void fun() { what is x top to bottom
++x ;
printf ("%d",x); 1
}
void g(){
++x ;
printf ("%d",x); 2

chapters Page 90
void g(){
++x ;
printf ("%d",x); 2
}
void main(){
f();
g();
++x ;
printf ("%d",x); 3

#include<stdio.h>

void fun() {
++x ; no information, compiler dont know about what is x
printf ("%d",x); trying to use something which is not defined.
}
int x = 10;
void g(){
++x ;
printf ("%d",x);
}
void main(){
f();
g();
++x ;
printf ("%d",x);

#include<stdio.h>
void fun() {
extern int x; forward declaration
++x ; hence, no error.
printf ("%d",x);
}
void g(){
++x ;
printf ("%d",x);
}
void main(){
f();
g();
++x ;
printf ("%d",x);
}
local forward declaration:

chapters Page 91
#include<stdio.h>
void fun() {
extern int x; to avoid compilation error
++x ; (no memory created)
printf ("%d",x);
}
void g(){
extern int x; to avoid compilation error
++x ; (no memory created)
printf ("%d",x);
}
int x = 10; the "x" i am using is of global.
void main(){
++x ;
f();
g();
++x ;
printf ("%d",x);
}

instead of local forward declaration, i can do it globally.

#include<stdio.h>
extern int x ; global forward declaration for global variable.
void fun() {
extern int x; it is basically re-declaration, so compiler will ignore this information
++x ; because global declaration will give the information to compiler.
printf ("%d",x);
}
void g(){
++x ;
printf ("%d",x);
}
void main(){
++x ;
f();
g();
++x ;
printf ("%d",x);
}
int x = 10;

static + global : global :

#include<stdio.h> #include<stdio.h>
static int i = 10;
chapters Page 92
static + global : global :

#include<stdio.h> #include<stdio.h>
static int i = 10; int i = 10;
void fun() { void fun() {
______ ______
______ ______
} }
void g(){ void g(){
______ ______
______ ______
} }
void main(){ void main(){
______ ______
______ ______
______ ______
} }

linkage

no linkage external
linkage
block internal
linkage
multi- file
{
__ file
{ {
__ __ __
} {
__ __
__
static variable } }
}
{
int x;
__
global
}
static int x;
global
static global : cannot use in multiple environment

#include<stdio.h>
static int i ;
globally redeclaration is allowed
static int i ;
int main ()

this "x" variable can be mainpulated by these functions only, only these functions have the right to
modify data in the variable.

void f(){ void g(){


chapters Page 93
this "x" variable can be mainpulated by these functions only, only these functions have the right to
modify data in the variable.

void f(){ void g(){


___ ___
} }
X

void h(){ void i(){


___ ___
} }

note : global assignment is not possible, only functions can change the value.

#include<stdio.h>
static int a ;
globally redeclaration is allowed
static int a ;
static int a = 90;
static int a = 100; multiple initialization are not allowed, you can only initialize once.

chapters Page 94
chapter - 5 (recursion)
Sunday, August 18, 2024 6:22 AM

recursion

whenever an operation is defined in terms of itself.


a teacher ask to student how many students behind you are including you?

...

this student this student this student


will ask the will ask the will return the
student behind student behind value 1 as no
him to count him to count one is behind
him

7 6 5 4 3 2 1
...

returns 7 to teacher

small work done.

when n is small when n is large

- we can answer directly - we cannot answer directly


- easy to solve - not easy to solve
- no recursion is required - recursion is required

if (n is small) {
we can answer directly
easy to solve
no recusrion is required
}
else {
input is large
cannot be answered directly
recursion is required
}

chapters Page 95
}

(i) code for n ≥ 1

- i/p: n=2
o/p : hihi

- i/p: n=3
o/p : hihihi

- i/p: n=10
o/p : hihihihihihihihihihi

har recusrive call kuch kaam khud karta hai aur baaki ka kaam recusrion karta hai

#include <stdio.h>
void fun (int n) {
kuch kaam khud karta hai
if (n==1)
(when n is small)
{
printf ("hi");
return; to return to the function
}
baaki ka kaam recusion karta hai
else {
(when n is large, eg. n=1000)
printf ("hi");
fun (n-1);
}
}

to print (n-1) times.

(ii) code for n > 0


given a number of k digits, program have to give the output as sum.

- i/p: n=125 n = d1d2...dk


o/p : 8
d1 + d2 + ...+dk
- i/p: n=9
o/p : 9

- i/p: n=2538
o/p : 18

- i/p: n=1
o/p : 1
chapters Page 96
o/p : 1

#include <stdio.h>
int sum_of.digits (int n) {
if (n>0 && n<=9)
return n;
}
else {
last = n%10;
remain = n/10;
last + sum_of.digits (n/10);
}
}

example :

sum_of.digits(2379)

n%10 + sum_of.digits(2379)(n/10)

9 + sum_of.digits(237) 2379 % 10 = 9 (we got last digit)


2379 / 10 = 237 (we got first three digits)

7 + sum_of.digits(23) 237 % 10 = 7 (we got last digit)


237 / 10 = 23 (we got first two digits)

3 + sum_of.digits(2) 23 % 10 = 3 (we got last digit)


23 / 10 = 2 (we got first digit)

now it will return values in recusrive manner.

12
21
9 + sum_of.digits(237)

5 12
7 + sum_of.digits(23)

5
3 + sum_of.digits(2)

chapters Page 97
final output : 21

#include <stdio.h>
void f (int n) {
if (n = = 0)
return n;
}
else {
printf ("%d", n);
f(n-1);
}
void main {
f(3);
}
}

f(0) return
f(1)
check { condition false
condition f(0) called (ii) printf ("%d",1)
if (n = = 0) (iii)f(0)
return n; (iv)

}
f(2)
else {
condition false
(ii)printf ("%d", n); (ii) printf ("%d",2)
f(1) called
(iii)f(n-1); (iii)f(1)
(iv) } (iv)

void main { f(3)


(i) f(3);
condition false
} f(2) called (ii) printf ("%d",3)
} (iii)f(2)
(iv)

f(3) called
main
(i) f(3)

chapters Page 98
f(0) return
f(1) f(1)
condition false condition false
(ii) printf ("%d",1) (ii) printf ("%d",1)
(iii)f(0) (iii)f(0)
(iv) output : 3 (iv)

f(2) f(2)
condition false condition false
(ii) printf ("%d",2) (ii) printf ("%d",2)
(iii)f(1) (iii)f(1)
(iv) output : 2 (iv)

f(3) f(3)
condition false condition false
(ii) printf ("%d",3) (ii) printf ("%d",3)
(iii)f(2) (iii)f(2)
(iv) output : 1 (iv)

main main
(i) f(3) (i) f(3)

f(2)
condition false
(ii) printf ("%d",2)
(iii)f(1)
(iv)

f(3) f(3)
condition false condition false
(ii) printf ("%d",3) (ii) printf ("%d",3)
(iii)f(2) (iii)f(2)
(iv) (iv)

main main
(i) f(3) (i) f(3)

output : 321

chapters Page 99
f(0) return
f(1)
condition false printf is waiting
check { f(0) called
(ii) f(0)
condition
if (n = = 0) (iii)printf ("%d",1) for f(0) to complete
return n; (iv) the task
}
f(2)
else {
(ii)f(n-1) f(1) called condition false printf is waiting
(ii) f(1)
(iii)printf ("%d", n); for f(1) to complete
(iii)printf ("%d",2)
(iv) } (iv) the task
void main { f(3)
(i) f(3);
f(2) called condition false printf is waiting
} (ii) f(2)
for f(2) to complete
} (iii)printf ("%d",3)
(iv) the task

f(3) called
main
(i) f(3)

f(0) return
f(1) f(1)
condition false condition false
(ii) f(0) (ii) f(0)
(iii)printf ("%d",1) (iii)printf ("%d",1)
(iv) output : 1 (iv)

f(2) f(2)
condition false condition false
(ii) f(1) (ii) f(1)
(iii)printf ("%d",2) (iii)printf ("%d",2)
(iv) (iv) output : 2
f(3) f(3)
condition false condition false
(ii) f(2) (ii) f(2)
(iii)printf ("%d",3) (iii)printf ("%d",3)
(iv) (iv)

main main
(i) f(3) (i) f(3)

chapters Page 100


f(2)
condition false
(ii) f(1)
(iii)printf ("%d"2)
(iv)

f(3) f(3)
condition false condition false
(ii) f(2) (ii) f(2)
(iii)printf ("%d",3) (iii)printf ("%d",3)
(iv) output : 3 (iv)

main main
(i) f(3) (i) f(3)

output : 123

check { recursion tree


condition
if (n = = 0)
f(3)
return n;
} f(2) pf(3) (waiting for f(2) to execute)
else {
(ii)f(n-1) f(1) pf(2) (waiting for f(1) to execute)
(iii)printf ("%d", n);
f(0) pf(1) (waiting for f(0) to execute)
(iv) }
void main {
(i) f(3);
}
}

approach : top to bottom and left to right

f(3) 2 f(3)

1 f(2) pf(3) f(2) pf(3)

f(1) pf(2) pf(2)

f(0) pf(1)

chapters Page 101


1 f(2) pf(3) f(2) pf(3)

f(1) pf(2) pf(2)

f(0) pf(1)

f(3)

pf(3)

output : 123

note : statements written after recusrive call execute in opposite order of call.

check { recursion tree


condition
if (n = = 0)
return n; f(3)
} pf(3) pf(3) (waiting for f(2) to execute)
output : 3
else {
(ii)printf ("%d", n); f(2)
(iii)f(n-1)
(iv)printf ("%d", n); output : 2 pf(2) pf(2) (waiting for f(1) to execute)
(v) } f(1)
void main {
(i) f(3); output : 1 pf(1) pf(1) (waiting for f(0) to execute)
}
f(0)
}

f(3)
3 pf(3) pf(3) 1

f(2)
output : 321123
2 pf(2) pf(2) 2

f(1)

1 pf(1) pf(1) 3

f(0)

chapters Page 102


check {
condition
if (n = = 0)
return n;
}
else {
(ii)f(n-1)
(iii)printf ("%d", n);
(iv)f(n-1)
(v) }
void main {
(i) f(3);
}
}

recursion tree
f(3)

f(2) pf(3) f(2)


f(1) f(1) f(1) f(1)
pf(2) pf(2)
f(0) f(0) f(0) f(0) f(0) f(0) f(0) f(0)

pf(1) pf(1) pf(1) pf(1)

f(3)

f(2) pf(3) f(2)


f(1) f(1) f(1) f(1)
3
pf(2) pf(2)
f(0) f(0) f(0) f(0) f(0) f(0) f(0) f(0)
2 2
pf(1) pf(1) pf(1) pf(1)

1 1 1
1

chapters Page 103


output : 1213121

decimal to binary program

2 23 rem
2 11 1
2 5 1
2 2 1 binary : 10111
2 1 0
0 1

#include<stdio.h>
void main f(int n){
if (n is small)
{
easy case (1 and 0)
we can answer directly
no recusrion is needed
}
else
{
we cannot answer directly
recusrion is needed
}
}

#include <stdio.h>
void f (int n) {
if (n = = 0 || n = = 1) 2 23 rem
{ 2 11 1 quotient : n/2
printf ("%d", n); 2 5 1 recursion ispar lagana hai
return; 2 2 1
} 2 1 0
else { 0 1
f(n/2);
printf ("%d", n%2); remainder to be printed in reverse order
}
}

power function program

ab

chapters Page 104


ab
a,b >0

ab using recursion

310 = 3 x 3 x 3 x 3 x 3 x 3 x 3 x 3 x 3 x 3

310 = 3 x 39
f(a,b) = a x f(a, b-1)

ab = a x ab-1

#include <stdio.h>
int f (int a, int b) {
if (b = = 1)
return a;
else {
return a x f(a, b-1);
}
}

write a recursive code to print the octal equivalent of given no. (n)

#include<stdio.h>
void fun (int n) {
if (n<=0 && n>=7)
printf ("%d", n);
else{
f(n/8)
printf ("%d", n%8);
}
}

chapters Page 105


chapter - 6 (arrays)
Thursday, August 15, 2024 6:13 PM

arrays and address

(i) address

(a) absolute address


(b) relative address

(a) absolute address :

A-106, krishna nagar


mathura-281004 (U.P.)

(b) relative address


we are calculating address using another address
A-101 A-102 A-103 A-104 A-105 A-106

the resident in A-101 telling us about


A-106 with respect to himself.

(i) address of operator ( &)


int a = 10; a
&a= 2036 10
(memory location
2036 &a
2036)
(standing outside the memory block a)

(ii) value at operator (*)


int a = 10; a
&a= 2036 10 *(&a)(entered into the memory block a)
(memory location
2036 &a
2036)
(standing outside the memory block a)

*(&a) = value at (memory location 2036)


*(&a) = 10

chapters Page 106


now, a= 10 and also, *(&a) = 10
so basically, *&a = a
*&a
(value at) and (address of) cut each other.

q. why we use array?

- array is a group of similar elements


- collection of homogenous types of elements.

example :

int a,b,c = int a[3]

a,b,c are 3 a is group of


variables of 3 variables of
integer type. integer type

(i) the memory of variables can be different but in array the elements are stored sequentially one
after another.

int a,b,c int a[3];

a b c
----4byte---- ----4byte---- ----4byte----
2036 3196 1980 1000 1004 1008

memory of variables can in array the elements are stored sequentially


be different one after another.

starting address

(ii) all 3 elements are represented by same name/entity.

int a,b,c; int a[3]; - group/collection of 3 elements.


a = 10; ____ ____ ____
b = 30;
c = 300; a is array of 3 elements

(iii) all the elements of array are contiguously stored.

(iv) for array there is a unique identification that we call indexes, and in C index
chapters Page 107
(iv) for array there is a unique identification that we call indexes, and in C index
always starts from 0.

int a[4];
10 300

0 1 2 3

a[0] = 10; a [0]


a[2] = 300;
group name index no.

(iv) array name represents the constant address of first elements of array.

int a[4]

a
1000 1004 1008 1012
1000
array name holds the
constant address of first
element of array

(v) if values are not assigned for array, the default value will be garbage.

void main () { a[0] a[1] a[2] a[3]


int a[4];
printf ("%d", a[0]);
G G G G
local
the default value for
local is garbage

(vi) initialization and declaration, also elements are stored sequentially one after another.

int a; declaration int a[4]; declaration


int a = 1; initialization int a[4] = {10,20,30,40}; initialization

10 20 30 40

chapters Page 108


10 20 30 40

a[0] a[1] a[2] a[3]

(vii) the concept of garbage ends even if we initialise a single value in array, the rest of array
will be filled with 0.

int a[4] = {10,20};

10 20 0 0

a[0] a[1] a[2] a[3]

(viii) the array "[]" is invalid because it cannot be empty, the group size is 0.

int a[] error

(ix) compiler will assume the number of elements if we initialise it.


int a[] = {10,20,30} int a[3] = {10,20,30}
array size 3

(x) whenever you create the array mention the size.

- int a[2+2];

- int a[2 x 3];

- int a[4 x size of(int)];

(xi) it is not meaningful to create array of 0 size because behaviour is undefined.

(xi) array_name is the constant address of first element of array so it cannot be Lvalue of
any assignment statement, because Lvalue must be some variable.

chapters Page 109


array_name = 10; Lvalue = Rvalue constant

1000 = 10;
must be some expression
variable variable

void main () { void main () {


int a[4]; {10, 20, 30}; int a[4];
printf ("%d", a[0]); a = {10, 20, 30}
printf ("%d", a[0]);
store information of array

array_name cannot be L value it is invalid

(xii) array_name is the constant address of first element of array we cannot use modify operators.
2++; invalid

array_name++ a[2]++ valid


++array_name
invalid
array_name - -
- - array_name

int a[4] = {10, 20, 30, 40};


collection of variables collection of values

(xiii) %u is preferred for array because address cannot be negative hence it is unsigned.

void main () {
int a[4]; {10, 20, 30,40};
printf ("%u", a); 10
printf ("%d", &a[0]); 10

10 20 30 40

a[0] a[1] a[2] a[3]

(xiv) name of array does not represent an address with 2 operators.

chapters Page 110


(xiv) name of array does not represent an address with 2 operators.
(a) address of operator (&a)
(a) size of operator

void main () {
int a[4]; {10, 20, 30,40};

10 20 30 40

a[0] a[1] a[2] a[3]

&a : address of whole array : 100 (16byte)


a[0] a[1] a[2] a[3]

10 20 30 40

100 104 108 112

address of whole array is also the starting address of an array.

a : address of first element : &a[0] : 100 (4byte)


a[0] a[1] a[2] a[3]

10 20 30 40

100 104 108 112

array name represent the constant address of first element of array.

concept : what is a?
numerical value of a is 100

a+1

chapters Page 111


a+1

if a is a value (or) simple variable if a is address pointer variable


int a = 100; - address arithmetic
pf ('%d",a+1); address + value = address
101 value+ address = address
value + value = value address+ address = invalid
address : kiska address hai and uska
size kya hai?
if declaration of array is having n-dimension then,
(a) element - anywhere in program, you provide exactly n-dimensions.
(b) address - anywhere in program, you provide less than n-dimensions.

what are dimensions?

one dimension two dimension three dimension


a[] a[][] a[][][]

int a [4] = {10,20,30,40}; int a [2][3] = {10,20,30,40}; int a [3][3][3] = {10,20,30,40};


a : 0 dimensional a : 0 dimensional a : 0 dimensional

element? element? element?


no, because we no, because we no, because we
provided 0 dimension provided 0 dimension provided 0 dimension

address? address? address?


yes, because we provided yes, because we yes, because we provided
less than 1 dimension less than 2 dimension less than 3 dimension

a[0] : 1 dimensional a[0] : 1 dimensional

element? element?
no, because we no, because we
provided 0 dimension provided 1 dimension

address? address?
yes, because we provided yes, because we provided
less than 2 dimension less than 3 dimension

a[1][1] : 2 dimensional a[0][1] : 2 dimensional

element? element?
yes, because we no, because we
provided 2 dimension provided 2 dimension

chapters Page 112


element? element?
yes, because we no, because we
provided 2 dimension provided 2 dimension

address? address?
no, because it is yes, because we provided
exactly 2 dimensional less than 3 dimension

a[0][0][0] : 3 dimensional

element?
yes, because we provided
less than 3 dimension

address?
no, because it is
exactly 3 dimensional

int a [4] = {10,20,30,40};


a+1:

(a) what is a?
- a is address because it is 0 dimensional and array is of 1 dimension.

(b) whose address?


- a : array name is the constant address of first element.
&a[0] : 100

(c) size of a[0]?


- 4 bytes.

a+1

&a[0]+1x4
= 100 + 4
= 104

(a+1) = (memory location 100 + 1x(size of))


(a+1) = (memory location 100 + 4)
(a+1) = memory location 104
(a+1) = &a[1]

a[0] a[1] a[2] a[3] a[0] a[1] a[2] a[3]


chapters Page 113
a[0] a[1] a[2] a[3] a[0] a[1] a[2] a[3]

10 20 30 40 10 20 30 40

100 104 108 112 100 104 108 112

int a [4] = {10,20,30,40};


a+2:

(a) what is a?
- a is address because it is 0 dimensional and array is of 1 dimension.

(b) whose address?


- a : array name is the constant address of first element.
&a[0] : 100

(c) size of a[0]?


- 4 bytes.

a+2

&a[0]+2x4
= 100 + 8
= 108

(a+2) = (memory location 100 + 2x(size of))


(a+2) = (memory location 100 + 8)
(a+2) = memory location 108
(a+2) = &a[2]

a[0] a[1] a[2] a[3] a[0] a[1] a[2] a[3]

10 20 30 40 10 20 30 40

100 104 108 112 100 104 108 112

int a [4] = {10,20,30,40};

a+1
*(a+1) = value at(memory location 100 + 1x(size of)) a[0] a[1] a[2] a[3]
*(a+1) = value at(memory location 100 + 4)
chapters Page 114
a+1
*(a+1) = value at(memory location 100 + 1x(size of)) a[0] a[1] a[2] a[3]
*(a+1) = value at(memory location 100 + 4)
*(a+1) = value at(memory location 104)
*(a+1) = value at(&a[1]) 10 20 30 40
*(a+1) = a[1]
*(a+1) = 20 100 104 108 112

*(&a[1]) entered inside

*(&a[1])
standing outside

int a [4] = {10,20,30,40};

a+2
*(a+2) = value at(memory location 100 + 2x(size of)) a[0] a[1] a[2] a[3]
*(a+2) = value at(memory location 100 + 8)
*(a+2) = value at(memory location 108)
*(a+2) = value at(&a[2]) 10 20 30 40
*(a+2) = a[2]
*(a+2) = 30 100 104 108 112

*(&a[2]) entered inside

*(&a[2])

standing outside

note :
*(a+i) = a[i]
- addition is commutative
a[i] = *(a+i)
a[i] = *(i+a)
a[i] = i[a]

but,
int 4[a] = {10,20,30,40};
in declaration, it is not valid.

void main () {
int a[5]= {10, 20, 30,40,50};

a[0] a[1] a[2] a[3] a[4]

chapters Page 115


int a[5]= {10, 20, 30,40,50};

a[0] a[1] a[2] a[3] a[4]

10 20 30 40 50

1000 1004 1008 1012 1016

pf("%u", a); address of first element : &a[0] : 1000


pf("%u", &a); address of whole array: &a : 1000

pf("%u", a+1); &a[0]+1 x 4 = &a[0]+ 4 = &a[1] = 1004

pf("%u", &a+1); &a+1 x 20 = 1000+ 20 = 1020


pf("%u", *(a+1); *(&a[0]+1 x 4) = *(1000+ 4) = *(1004) = 20

concept : 2D array

int a[2][3]

a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]


a[0] ka a[0] ka a[0] ka a[1] ka a[1] ka a[1] ka
1st 2nd 3rd 1st 2nd 3rd
element element element element element element

a[0] a[1]

void main () {
int a[2][3]; {10, 20, 30,40,50,60};

chapters Page 116


a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]

10 20 30 40 50 60

1000 1004 1008 1012 1016 1020

a[0] a[1]

pf("%u", a); address of first element : &a[0] :(12byte) 1000


pf("%u", a[0]); &a[0][0] : 4 byte 1000

pf("%u", &a); address of whole array: &a : 24 byte 1000


pf("%u", a+1); &a(address of first element) +1 x 12 (size of a[0]) = 1012
pf("%u", &a+1); &a(address of whole array) +1 x 24 (size of whole array) = 1024

pf("%u", a[0]+1); &a[0][0] +1 x 4 (size of a[0][0]) = 1004 (&a[0][1])

a[0] : address (less than n-dimesion)

void main () {
int a[2][3]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

1 2 3 4 5 6 7 8 9 10 11 12
a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]
1000 1004 1008 1012 1016 1020 1024 1028 1032 1036 1040 1044
a[0] a[1] a[2]
a

pf("%u", a); address of first element : &a[0] :(12byte) 1000


pf("%u", &a); address of whole array: &a : (48 byte) 1000

pf("%u", a[0]); &a[0][0] : (4 byte) 1000

pf("%u", a+1); &a[0] +1 x 16 (size of array) = &a[0][4] : 1016


pf("%u", a[0]+1); &a[0][0] +1 x 4 (size of array) = &a[0][1] : 1004

pf("%u", &a+1); &a +1 x 48 (size of whole array) = 1048

chapters Page 117


pf("%u", *a); *&a = a[0] : a[0][0] = 1000

it is address due to less than 2-Dimension

pf("%u", **a); *&a = *(a[0]) = *&a[0][0] = a[0][0] = 1

it is address due to less it is element due to exactly


than 2-Dimension 2-Dimension

pf("%u", *a+1); *&a+1 = (a[0]+1) = a[0][0]+1 = a[0][1] = 1004

it is address due to less


than 2-Dimension

void main () {
int a[2][3]; {1, 2, 3,4,5,6};

a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]

1 2 3 4 5 6

100 104 108 112 116 120

a[0] a[1]

pf("%u", a[0]); &a[0][0] : 100


pf("%u", a[0]+1); &a[0][0]+1 = a[0][1] = 104

pf("%u", *(a[0]+1); *&a[0]+1 = a[0]+1 = a[0][0]+1 = a[0][1] = 2

it is address due to less it is element due to exact


than 2-Dimension 2-Dimension

pf("%u", *(a[0]+2); *&a[0]+2 = a[0]+2 = a[0][0]+2 = a[0][2] = 3

it is address due to less it is element due to exact


than 2-Dimension 2-Dimension

chapters Page 118


*(a[0]+j) = a[0][j]

pf("%u", a[1]+1); &a[1][0]+1 = a[1][1] = 116

pf("%u", *(a[1]+1); *&a[1]+1 = a[1]+1 = a[1][0]+1 = a[1][1] = 5

it is address due to less it is element due to exact


than 2-Dimension 2-Dimension

pf("%u", *(a[1]+2); *&a[1]+2 = a[1]+2 = a[1][0]+2 = a[1][2] = 6

it is address due to less it is element due to exact


than 2-Dimension 2-Dimension

*(a[i]+j) = a[i][j]

void main () {
int a[2][3][2]; {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]


a[0][0][0] a[0][0][1] a[0][1][0] a[0][1][1] a[0][2][0] a[0][2][1] a[1][0][0] a[1][0][1] a[1][1][0] a[1][1][1] a[1][2][0] a[1][2][1]

1 2 3 4 5 6 7 8 9 10 11 12
1000 1004 1008 1012 1016 1020 1024 1028 1032 1036 1040 1044

a[0] a[1]

pf("%u", a); &a[0] : 1000 (24 byte)


pf("%u", a[0]); &a[0][0] = 1000 (8 byte)

pf("%u", a[0][0]); &a[0][0][0] = 1000 (4 byte)

pf("%u", &a); 1000 (48 byte)

pf("%u", a+1; &a[0]+1 : 1000+1 x 24= 1024 (a[1])


pf("%u", a[0][0]+1); &a[0][0][0]+1 = 1000+1x4=1004 (a[0][0][1])
pf("%u", &a+1); 1000 + 1 x 48 = 1048
pf("%u", *a); *&a[0] = a[0] = a[0][0] = 1000 (8 byte)

chapters Page 119


pf("%u", **a+1); *(*&a[0]+1) = *(a[0][0]+1) = *&a[0][0] = a[0][0][0]+1 = a[0][0][1] = 1004
pf("%u",***a+1); **(*&a[0]+1) = **(a[0][0]+1) = *(*&a[0][0]+1) = *(a[0][0][0]+1) = 1+1 =2

topic : declaration and initialization


(i) int a [] ; invalid
(ii) int a [] = {10, 20, 30}; valid

if only declaration is there without initialization, it is mandatory (compulsory) to provide the size
of each dimension.

(iii) int a[2][3]; valid

in case, we are initializing an array, there is flexibility that you can omit the size of ist dimension.

(iv) int a [][2] = {10, 20, 30}; valid


optional

no other dimension is having such flexibility.


(v) int a [x][3] = {1, 2, 3, 4, 5, 6}; valid
6 elements.
x*3=6
x = 6/3 = 2
so, compiler will assume
int a [2][3] = {1, 2, 3, 4, 5, 6};

(vi) int a [x][3] = {1, 2, 3, 4}; valid


4 elements.
x*3=4
x = 4/3 = 1.33 = 2
so, compiler will assume
int a [2][3] = {1, 2, 3, 4};
by default, the rest of 2 arrays will be filled with 0

chapters Page 120


chapter - 7 (pointers)
Friday, August 23, 2024 3:50 AM

pointers

it is a special variable that are used to store the addresses of other variables.

int *p

- p is a pointer to integer.
- p have the address of integer variable.
- p can store the address of some integer variable.
x p
int x = 10;
int *p; 10 2036 stored in p
p = &x
2036 4032

p = &x p is storing the address of x


address of x

p = memory location 2036


*p = value at (memory location 2036) = 10

int **q

- q is a pointer to pointer to integer.


- q have the address of pointer to integer.
- q can store the address of some pointer.
x p q
int x = 10;
int *p; 10 1036 2016 stored in q
int **q;
p = &x 1036 2016 3192
q = &p
stored in p
p = &x p is storing the address of x address of x

q = &p q is storing the address of p address of p

chapters Page 121


p = memory location 1036
*p = value at (memory location 1036) = 10
q = memory location 2016
*q = value at (memory location 2016) = 1036
**q = value at (memory location 1036) = 10

pf ("%u",p); 1036 (memory location 1036)

pf ("%u", *p); 10 (value at (memory location 1036))

pf ("%u",q); 2016 (memory location 2016)


pf ("%u",*q); 1036 (value at (memory location 2036))
pf ("%u",**q); 10 (value at (memory location 1036))

concept : dereferencing
dereferencing : fetching value through address (value at operator lagate hai hum use de-referencing
bolte hai)
x p
int x = 10;
int *p; 10 1000

1000

MSB LSB

0000 0000 0000 0000 0000 0000 0000 1010

1000 1001 1002 1003

pf ("%u",*p); 10

dereferencing

(i) in the case of pointer, everything depends upon declaration.


int *p compiler 4 bytes uthayega (4 byte)
char *q compiler 1 byte uthayega (1 byte)
kitne byte uthayega compiler vo depend karta hai declaration par.

x p
chapters Page 122
kitne byte uthayega compiler vo depend karta hai declaration par.

x p
char x = 65;
char *p; (p is a pointer 65 1000
to character)
p = &x; 1000

1 byte ki value uthakar deni hai

0100 0001
pf ("%d",*p); 10
1000

a[0] a[1] a[2] a[3]


int [4] = {10, 20, 30, 40};
int *p; (p contains the 10 20 30 40
address of 4 byte)
p = &a[1]; 100 104 108 112
p = p+1;

p contains the address of a[1] = 104 104

p = p+1; p
address + value = address
value + address = address
address value

p = 104 + 1 x4 104 108


p = 108 p

a[0] a[1] a[2] a[3]


int [4] = {10, 20, 30, 40};
int *p; (p contains the 10 20 30 40
address of 4 byte)
p = &a[0]; 100 104 108 112
p = p++;
p =++p;
p contains the address of a[0] 100

p = p++;
(i) use the value (useless)
chapters Page 123
(i) use the value (useless)
(ii) increase the value
p = p+1
p = 100 + 1 x 4
p = 104 100 104

p
p =++p;
(i) increase the value
p = p+1
p = 104 + 1 x 4
p = 108 104 108

(ii) use the value p

difference between updation and updation + storing

a[0] a[1] a[2] a[3]


int [4] = {10, 20, 30, 40};
int *p =a; (p contains the 10 20 30 40
address of 4 byte)
int *p = a 100 104 108 112
int *p
p=a
p contains the address of a 100

only updation updation + storing


p+2 p = p+2
100 + 1 x 8 = 108 p = 100 + 1 x 8
100 p = 108
p 100 108

int *p; declaration


p = a; initialization
int *p=a declaration+initialization

a[0] a[1] a[2] a[3]


chapters Page 124
a[0] a[1] a[2] a[3]
int [4] = {10, 20, 30, 40};
int *p =a; (p contains the 10 20 30 40
address of 4 byte)
int *p = a 100 104 108 112
int *p
p=a

100 p contains the address of a[0]


p

pf ("%d", *p); value at (memory location 100) : 10


pf ("%d", *(p+1)); value at (memory location 100+1x4)
value at (memory location 100+4)
value at (memory location 104) : 20
pf ("%d", *(p+2)); value at (memory location 100+2x4)
value at (memory location 100+8)
value at (memory location 108) : 30
pf ("%d", *(p+3)); value at (memory location 100+3x4)
value at (memory location 100+12)
value at (memory location 112) : 40
p : p+0
*(p+i) = p[i]

pf ("%d", p[0]); 10
pf ("%d", p[1]); 20

pf ("%d", p[2]); 30
pf ("%d", p[3]); 40

equivalence between arrays and pointers :

arrays : elements store contigiously


pointer : +1 karne par next element

difference between using modify operators on array and pointers :

chapters Page 125


arrays pointers

int a[4] = {10, 20, 30, 40} int a[4] = {10, 20, 30, 40}

a++ ; int *p = a;
++a ; p++ ; p = p+1
invalid
a--; ++p ;
--a; address value

invalid, because array_name is valid, because we can update P.


the constant address of its first
element.

a[0] a[1] a[2] a[3]


int [4] = {10, 20, 30, 40};
int *p =&a[3]; 10 20 30 40
--p;
100 104 108 112

112 p contains the address of a[3]


p

--p;
(i) decrease the value
p=p-1
p = 112 - 1x4
p = 108
112 108

(ii) use the value p

modifying pointers :

(i) pointer + pointer : invalid


(ii) ++ptr, --ptr, ptr++, ptr-- : valid
(iii) ptr + 3 : valid (moving 3 locations in forward direction)

(iv) ptr - 3 : valid (moving 3 locations in backward direction)

when the subtraction of pointers are valid?


ptr 1 - ptr 2 : logically invalid, only when both of them are pointing to elements of same array.

chapters Page 126


ptr 1 - ptr 2 : logically invalid, only when both of them are pointing to elements of same array.

a[0] a[1] a[2] a[3]


int [4] = {10, 20, 30, 40};
int *p *q; 10 20 30 40
p = &a[3];
q= &a[0]; 100 104 108 112

112 p contains the address of a[3]


p

100 q contains the address of a[0]


q

(actual difference)
p-q=
integer size

112 - 100 12
= = 3
4 4

no. of elements between these 2 address are 3

priority between ++ and *

++ and * : same priority (right to left associativity)

a[0] a[1] a[2] a[3]


int [4] = {10, 20, 30, 40};
int *p = &a[0]; 10 20 30 40
++*p;
*p++; 100 104 108 112
pf ("%d", *p);

100 p contains the address of a[0]


p

(i) ++*p

chapters Page 127


++ (*p)
++ (value at (memory location 100))

++ (10)
11

a[0] a[1] a[2] a[3] a[0] a[1] a[2] a[3]


11
10 20 30 40 11 20 30 40

100 104 108 112 100 104 108 112

(ii) *p++
*(p++)
(i) use the value (useless)
(ii) increase the value
p = p+1
p = 100 + 1x4 104 p contains the address of a[1]
p = 104
*p p

value at (memory location 104)

20

int [4] = {10, 20, 30, 40};


int *p = &a[0];
pf ("%d", ++*p++);

cannot use multiple modify


operator withing a sequence point.

a[0] a[1] a[2] a[3]


int [4] = {10, 20, 30, 40};
int *p = &a[0]; 10 20 30 40
++*p++;
100 104 108 112

chapters Page 128


100 p contains the address of a[0]
p

++(*(p++))

(i)p++ (2nd time)


(used)
(i) use the value (useless).
(ii) increase the value.

(ii)++(*(p))
(i) increase the value
++(value at (memory location 100))
++10
11

(ii) use the value


printed '11'.

104 p contains the address of a[1]


p due to post increment

topic : complex declarations


declaration involves :
(i) () : functions
(i) (left to right)
(ii) [] : arrays

(iii) * : pointers
(ii) (right to left)
(iv) identifier

(v) data type

declaration hamesha identifier ka hi hota hai.

(i) int *(p[3])

(a) (p[3]) : p is an array of 3


(b) int*(p[3]) : p is an array of 3 pointer to integer

chapters Page 129


(b) int*(p[3]) : p is an array of 3 pointer to integer

p is an array of 3 pointer to integer

p[0] p[1] p[2] a[0] a[1] a[2]

100 104 108 10 20 30

100 104 108

(ii) int *p[3] : int (*p)[3]

(a) (p) : p is an pointer


(b) int (*p)[3] : p is an pointer to array of 3 integer
(address of an array of 3 integer)

p is an pointer to array of 3 integer

p a[0] a[1] a[2]

100 10 20 30

100 104 108

&a = 100

(iii) int *p
p is a pointer to integer
p stores the address of integer

(iv) char *p
p is a pointer to character
p stores the address of character

int [4] = {10, 20, 30, 40};


int *p = {a+1, a, a+2}
chapters Page 130
int *p = {a+1, a, a+2}
int **q;
q=p;

p[0] p[1] p[2] a[0] a[1] a[2] a[3]

a+1 a a+2 10 20 30 40

&p[0] q contains the address of p[0] 100 104 108 112

p[0] p[1] p[2] a[0] a[1] a[2] a[3]

&a[1] &a[0] &a[2] 10 20 30 40

&p[0] q contains the address of p[0] 100 104 108 112

(i) ++q
(a) increase the value
q = q+1
q = &p[0]+1
q = &p[1] &p[1] q contains the address of p[1]
(b) use the value q

(i) *++q : *(++q)


(a) increase the value
q = q+1
q = &p[1]+1
q = &p[2] &p[2] q contains the address of p[2]
(b) use the value q
(no meaning, no print)

(ii) pf("%d", *++*q);


(a) *q : *&p[2]
p[2]
&a[2]
108

chapters Page 131


(b) ++*q : ++*&p[2]
++p[2]
++&a[2]
&a[2]+1
&a[3]

(c) *++*q : *++*&p[2]


*&a[3]
a[3] element
40

swap function
parameters passed by address (call by reference)

void swap (int*, int*) void swap (int*p, int*q)


void main () { {
int a = 10, b = 20; int temp;
swap (&a &b); temp = *p;
pf ("%d%d, a,b); *p = *q;
to hold the addresses
} *q = temp;
we have pointer here
}

we are passing addresses here

p q temp a b temp
1000 2000 10 20
1000 2000

p q temp a b temp
temp = *p; 2000 1000 20 10
2000 1000

p q temp a b temp
*p = *q; 2000 1000 20 10
2000 1000

p q temp a b temp
*q = temp; 2000 1000 20 10
2000 1000

changes made in formal arguments reflected on actual arguments because we are swapping
addresses here, we worked on actual argument.

chapters Page 132


addresses here, we worked on actual argument.

void fun (int*) void fun(int*p)


void main () { {
int a [4] = {10, 20,30,40}; ++p;
fun (a); ++*p;
pf ("%d%d, a[0], a[1]); }
}

a[0] a[1] a[2] a[3]

10 20 30 40

100 104 108 112

&a[0] p contains the address of a[0]


p

(i) ++p;
(a) increase the value
p = p+1
p = &a[0] + 1
p = &a[1] &a[1] p contains the address of a[1]

(b) use the value. p

(i) ++*p; ++(*p)


p = *&a[1]
p = a[1]
p = 20

(a) increase the value a[0] a[1] a[2] a[3]


++20 21
21 10 20 30 40

(b) use the value. 100 104 108 112

pf ("%d%d, a[0], a[1]); 10, 21

void fun (int*) void fun(int*p)


void main () { {
int a [4] = {10, 20,30,40}; *++p;
chapters Page 133
void fun (int*) void fun(int*p)
void main () { {
int a [4] = {10, 20,30,40}; *++p;
fun (a); }
pf ("%d%d, a[0], a[1]);
}

a[0] a[1] a[2] a[3]

10 20 30 40

100 104 108 112

&a[0] p contains the address of a[0]


p

(i) *p++; *(p++)


(a) use the value.
useless

(b) increase the value


p = p+1
p = &a[0] + 1
p = &a[1]

- *(&a[1])
*&a[1]
a[1]
20

pf ("%d%d, a[0], a[1]); 10, 20

void fun (int*) void fun(int*p)


void main () { {
int a [4] = {10, 20,30,40}; ++ *p++;
fun (a); }
pf ("%d%d, a[0], a[1]);
}

a[0] a[1] a[2] a[3]

10 20 30 40
chapters Page 134
a[0] a[1] a[2] a[3]

10 20 30 40

100 104 108 112

&a[0] p contains the address of a[0]


p

++ *p++; ++(*(p++))

(i) p++
(2nd time)
(a) use the value (used)
useless
(ii) increase the value.

(ii) ++(*p)
++(*&a[0])
++(a[0])
++(10)
11

104 p contains the address of a[1]


p due to post increment

void fun (int*) void sum(int*p, int n)


void main () { {
int a [4] = {10, 20,30,40}; int s = 0
sum (a, 4); for (i=0; i<n; i++);
pf ("%d%d, a[0], a[1]); {
p[i] = *(p+i)
} s = s + p[i];
you have to pass the printf ("%d", sum);
number of elements also, }
sum till 4.

jab bhi aap ek array pass kar rahe hai jisme no. of element of array par kaam ho raha hai toh
batana padega.

void fun (int*) void fun(int*p)


void main () { {
int a [2][3] = {10, 20,30,40, 50, 60}; ++p;
fun (a[0]); *p++;
chapters Page 135
void fun (int*) void fun(int*p)
void main () { {
int a [2][3] = {10, 20,30,40, 50, 60}; ++p;
fun (a[0]); *p++;
pf ("%d%d, a[0][0], a[0][1], a[0][2]); *++p;
} p--;
*p = 100;
}

a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]

10 20 30 40 50 60

100 104 108 112 116 120

a[0] a[1]

a
100 p contains the address of a[0]
p

(i) ++p
(a) increase the value
p=p+1
p = &a[0][0] + 1
p = &a[0][1]
104 p contains the address of a[0][1]
(b) use the value p
useless

(ii) *p++
*(p++)
(a) use the value
useless
(b) increase the value
p = p+1
p = &a[0][1] + 1 108 p contains the address of a[0][2]
p = &a[0][2]
p
- *p
*&a[0][2]
24

chapters Page 136


(iii) *++p
*(++p)
(a) increase the value
p = p+1
p = &a[0][2] + 1
p = &a[1][0]
112 p contains the address of a[1][0]
p
(b) use the value
useless

- *p
*&a[1][0]
40

(iv) p - -
p--
(a) use the value
useless
(b) decrease the value
p = p-1
p = &a[1][0] - 1 (112-4) 108 p contains the address of a[0][3]
p = &a[0][3] (108)
p
-p
&a[0][3]

a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]

(v) *p = 100 100


10 20 30 40 50 60
*&a[0][3] = 100
(108) = 100
100 104 108 112 116 120

a[0] a[1]

pf ("%d%d, a[0][0], a[0][1], a[0][2]); 10, 20, 100

int *p; (4 byte)


p++ : 4 byte se increment
chapters Page 137
p++ : 4 byte se increment

char*q; (1 byte)
q++ : 1 byte se increment

concept : typecasting

typecasting : process of converting one data type to another data type by the programmer using the
casting operator during program design.

x p
int a = 300;
char *p; 300 1000
p = (char*)&a;
1000
typecasting
MSB LSB
(maan le character
ka address hai)
0000 0000 0000 0000 0000 0001 0010 1100
printf ("%d", *p); 44
1000 1001 1003
1002

we are using char so only one byte will be dereferenced

x p
int a = 160;
char *p; 160 1000
p = (char*)&a;
1000
typecasting
MSB LSB
(maan le character
ka address hai)
0000 0000 0000 0000 0000 0001 1010 0000
printf ("%d", *p); -96
1000 1001 1003
1002

we are using char so only one byte will be dereferenced

chapters Page 138


128+32=160
in cyclic property, 160 is not available
the range is -128 to 127

so, the value will be in negative and we will focus on 0's

-26 -25 -24 -23 -22 -2-1 -20 1


1 0 1 0 0 0 0 0

-25 -23 -22 -2-1 -20 1


-64-16-8-4-2-1
-96

concept : function pointer (pointer to function)

int (*p)()
p is a pointer to fucntion that takes no argument and it returns a integer value.
int add(int, int)

return argument
type : int list

- int *p(int, int)


p is a pointer to function that takes two integer argument and it returns an integer value.

- int *p(char*)
p is a pointer to function that takes pointer to character as an argument and it returns an integer
value.

- int *p(int*)
p is a pointer to function that takes pointer to integer as an argument and it returns an integer
value.

- can we pass a value to a function?


yes.

int a = 10, b = 20, result;


result = add(a,b);

- can we pass address to a function?

chapters Page 139


- can we pass address to a function?
yes.

int a [4] = {10, 20, 30, 40};


fun (a);

advantage : we can pass one function as an argument to an another function.

declaration of pointer :

int a = 10; float b = 9.8;


int *p; float *ptr;

how to declare a pointer to function?


use function pointer.

example : int add (int, int);


int (*p) (int, int); p is a pointer to function that takes two integers as argument as
returns its value.

#include <stdio.h>
int add (int, int); forward declaration
int main () {
p = &add;
printf ("%d", (*p)(10,20); calling the function using pointer to function
return 0;
}
int add (int x, int y)
{ body of the function
return x + y;
}

note : it is important to write the definition of function

chapters Page 140


subtraction product
#include <stdio.h> #include <stdio.h>
int sub (int, int); int add (int, int);
int main () { int main () {
p = &sub; p = &prod;
printf ("%d", (*p)(10,20); printf ("%d", (*p)(10,20);
return 0; return 0;
} }
int add (int x, int y) int add (int x, int y)
{ {
return x - y; return x * y;
} }

all are valid

(i) p = &add : (*p)(10,20);


(ii) p = add : (*p)(10,20);
(iii) p = add : (p)(10,20)
(iv) p = &add : (p)(10,20)

int [4] = {10, 20, 30, 40};


int *p = {a+3, a+2, a+1,a}
int y;
y = - - p[0] - p[1];
pf("%d",y); 0
pf("%d",*p[0]); 30

p[0] p[1] p[2] p[3] a[0] a[1] a[2] a[3]

&a[3] &a[2] &a[1] &a[0] 10 20 30 40

100 104 108 112

y = - - p[0] - p[1];

(i) - - p[0]
p[0] = p[0] - 1
p[0] = &a[3]-1
p[0] =&a[2]
p[0] p[1] p[2] p[3] a[0] a[1] a[2] a[3]
&a[2]
&a[3] &a[2] &a[1] &a[0] 30 40
chapters Page 141
p[0] p[1] p[2] p[3] a[0] a[1] a[2] a[3]
&a[2]
&a[3] &a[2] &a[1] &a[0] 10 20 30 40

100 104 108 112

(ii) p[0]-p[1]
&a[2] - &a[2]

(actual difference)
p-q=
integer size

108 - 108 0
= = 0
4 4

no. of elements between these 2 address are 0

q.1. which of the following are invalid?


(a) int b[][4]; invalid, because of no initialization

(b) int b[]; invalid, because of no initialization


(c) int b[2][][3]= {1, 2, 3, 4}; invalid, because you cannot omit second array

(d) int b[][2][3]= {1, 2, 3, 4}; valid

(1) only declaration (without initialization)


it is mandatory/compulsory to provide size of each dimension.

(2) initialization
there is flexibility only for first dimension, you can omit size of first dimension but this
flexibility is only valid for first dimension.

q.2. which of the following are valid?


p : int arr [4] = {10, 20, 30, 40}; q : int arr [4] = {10, 20, 30, 40};
printf ("%d", *arr++); int *ptr = arr;
printf ("%d", *ptr++);
array_name is the constant address of
first element, we cannot use modify we can use modify operators on pointer
operators on it hence, it is valid.

chapters Page 142


q.3. which of the following are valid?
p : int a[5] = {10, 20, 30, 40, 50}; q : int 5[a] = {10, 20, 30, 40, 50};
printf ("%d", 4[a]); printf ("%d", a[2]);

it is valid because 4[a] = a[4] invalid, because declaration must be


like a[5] not 5[a]

q.4. what will be the output?


int a[5] = {10, 15, 20, 25, 30}
printf ("%u", *(a+2)+6); 26
printf ("%u", *(a+*(a+1)-12); 25

(a) *(a+2)+6 (b) *(a+*(a+1) - 12)


*a[2] + 6 *(a + *(a[1]) - 12)
20 + 6 *(a + 15 - 12)
26 *(a + 3)
*(&a[0] + 3)
*(&a[3])
25

q.5. what will be the output?


int a[5] = {5, 3, 1, 2, 4};
int *p[5] = {a, a+1, a+3, a+2, a+4}; 2
printf ("%u%u", p[3][1], *(*(p+4)-2); 1

int *p[5] = {&a[0], &a[1], &a[3], &a[2], &a[4]};

p [3][1] *(*(p+4)-2)

*(p[3] + 1) *(*(p[4]) - 2)

*(&a[2]+ 1) *(*(&a[4]) - 2)

*(&a[3]) *(*&a[4]- 2)

*&a[3] *(a[4] - 2)

chapters Page 143


a[3] *(a[2])

2 1

a[i][j] = *(a[i]+j)

q.6. what will be the output?


int a[5] = {5, 3, 1, 2, 4};
int *p[5] = {a+3, a+1, a, a+2, a+4};
int **ptr = p+3
printf ("%u%u%u", ptr-p, *ptr-a, **ptr); 3 2 1

int *p[5] = {a+3, a+1, a, a+2, a+4};


int *p[5] = {&a[3], &a[1], &a[0], &a[2], &a[4]} ;

ptr = p+3
ptr = &p[0]+3
ptr = &p[3]

(i) ptr-p (ii) *ptr-a (iii) **ptr


&p[3] - &p[0] &p[3] - &a[0] **&p[3]
3 &a[2] - &a[0] *a[2]
2 *&a[2]
a[2]
1

q.7. what will be the output?


void func (int (*ptr)[2]))
{
**ptr+=1;
ptr++;
**ptr x =3;
}
void main ()
{
int arr [2][2] = {0, 1, 2, 3};
func (arr); &arr[0]
printf ("%d%d" a[0][0], a[0][1]);
}

**ptr+=1; ptr++; **ptr x =3;


chapters Page 144
}

**ptr+=1; ptr++; **ptr x =3;


arr[0][0] = a[0][0]+1 ptr = ptr + 1 **ptr = **ptr x 3
arr[0][0] = a[0][1] ptr = &arr[0] + 1 &arr[1][0] = &arr[1][0] x 3
ptr = &arr[1] arr[1][0] = 2 x 3
&arr[0] &arr[1] 6

ptr ptr

q.8. correct or not?


int a[3][2] = {1, 3, 5, 7, 9, 11};
int *ptr = a;
incorrect because declaration says that pointer will have the address
of integer but here we are assigning the address of whole array to
pointer.

q.9. what is output?


int a[3][2] = {1, 3, 5, 7, 9, 11};
int *ptr = a[1];
++*ptr++;
printf("%d", *ptr);

++*ptr++;

(i) ptr++
useless

(ii) ++ (*ptr)
++(*&a[1])
++(a[1])
++(a[1][0])
++ 5
6

(iii) using post increment.


++*ptr++;
6++;
7

chapters Page 145


q.10. what is output?
int a=5, b=10, c=15;
int *p[3] = {&a, &b, &c};
printf("%d", *p[*p[1]-8];

- *p[*p[1]-8]
*p[*&b-8]
*p[b-8]
*p[10-8]
*p[2]
* &c
15

q.11. what is output?


int a[] = {10, 20, 30, 40, 50};
int *p[] = {a, a+3, a+4, a+1, a+2};
int **ptr = p;
ptr++;
printf("%d%d", ptr-p, **ptr);

int **ptr = p; ptr++;


&p[0] &p[1]

ptr ptr

int *p[] = {&a[0], &a[3], &a[4], &a[1], &a[2]};


ptr-p **ptr
&p[1] - &p[0] **&p[1]

1 *a[3]

*&a[3]

a[3]
40

q.12. what is output?


void f(int*);

chapters Page 146


void f(int*);
void main () {
int a[2][2] = {1, 2, 3, 4, 5, 6};
f(a[1]); &a[1][0]
pf ("%d%d", a[1][1], a[1][2]);
}
void f(int*p) {
p--;
*p = *p x *p ;
p--;
*p = *p x *p ;
}

a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]

1 2 3 4 5 6

100 104 108 112 116 120


p
a[0] a[1]
a

(a) p - - ; a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]


(i) p = p - 1
9
p = &a[1][0] - 1 1 2 3 4 5 6
p = 112 - 1x4
p = 108
100 104 108 112 116 120
p = &a[0][2] p

(b) *p = *p x *p ;
*p = 3 x 3;
*p = 9;

(c) p - - ; a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]


(i) p = p - 1
p = &a[0][2] - 1 4
1 2 3 4 5 6
p = 104 - 1x4
p = 104
p = &a[0][1] 100 104 108 112 116 120
p
(d) *p = *p x *p ;
*p = 2 x 2;
*p = 4;

chapters Page 147


a[1][1]: 5
a[1][2]: 6

q.13. what is output?

int a[4][5] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16,17,18,19,20};
pf ("%d", *(*(a+**a+2)+3));

- *(*(a+**a+2)+3)
*(*(&a[0]+**&a[0]+2)+3)
*(*(&a[0]+*a[0]+2)+3)
*(*(&a[0]+*&a[0][0]+2)+3)
*(*(&a[0]+a[0][0]+2)+3)
*(*(&a[0]+1+2)+3)
*(*(&a[0]+3)+3)
*(*(&a[3])+3)
*(*(&a[3])+3)
*(*&a[3])+3
*(a[3])+3
*(&a[3][0])+3
*(&a[3][0])+3
*&a[3][0]+3
a[3][0]+3
16+3
19

q.14. what is output?

int f (int* a, int n) {


if (n<=0)
return 0;
else if (*a%2==0)
return *a+f(a+1, n-1);
else
return *a-f(a+1, n-1);
}
void main () {
int a [] = {12, 7, 13, 4, 11, 6};
pf("%d", f(a,6);
}

a[0] a[1] a[2] a[3] a[4] a[5]

12 7 13 4 11 6
chapters Page 148
a[0] a[1] a[2] a[3] a[4] a[5]

12 7 13 4 11 6

100 104 108 112 116 120

if (n<=0)
return 0;
else if (*a%2==0)
return *a+f(a+1, n-1);
else
return *a-f(a+1, n-1);
15
if (12%2==0) 12+f(104, 5); 12+f(104, 5);

3
if (7%2==0) 7-f(108, 4); 7-f(108, 4);

4
if (13%2==0) 13-f(112, 3); 13-f(112, 3);

9
if (4%2==0) 4+f(116, 2); 4+f(116, 2);

5
if (11%2==0) 11-f(120, 1); 11-f(120, 1);

6
if (6%2==0) 6+f(120, 0); 6+f(120, 0);

char a = 60; a
int *p;
p = int*&a;
60
pf ("%d",*p);

it is character hence 60 is stored in one byte


but we are trying to fetch integer which means 4 byte from memory but the rest of values are
unknown (garbage values).

chapters Page 149


note : pointer and data must be of same type.

topic : void pointer

void *p : there will be a address in p


int *p : there will be a integer address in p

char *p : there will be a character address in p

a
void *p;
int a = 10; 10
float b = 4.32;
p = &a;
pf ("%d", *p);
p

compiler will shout here because compiler does not know how much byte it have to pick
up, it won't be able to dereference.

we should not dereference any void pointer.

pf ("%d", *(int*)p);

compiler will not shout here because compiler knows how much byte it have to pick up,
it will be able to dereference.

so, typecasting is the solution to dereference void pointer.

points to remember -
(i) we can not dereference a void pointer directly.
(ii) first typecast then only dereference.

concept : arithmatic operators on void pointer.


do not perform any artihmatic operation on void pointers.

chapters Page 150


int *p; char *p; void *p;
__ __ __
__ __ __
__ __ __
__ __ __
p = p+2 p = p+2 p = p+2
2 x size of (int) 2 x size of (char) 2 x size of (?)

topic : wild pointer


wild pointer is an uninitialized pointer.

void main () x p
{
int *p; 12 garbage
int x = 12;
*p = 36;
1016
}
but there is a possibility of 1016 being there in p

x p
36
12 1016

1016

so here unintenionally we changed the value of our program.

void main ()
{ garbage
int *p;
}

p ke andar bhi ek garbage value hogi that is being treated as address.

topic : dangling pointer


pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer.
such a situation can lead to unexpected behavior in the program and also serve as a source of bugs in
C programs.
chapters Page 151
C programs.

int *fun () void main ()


{ {
int a = 10; int *p;
int *q = &a; p = fun();
return q; *p;
} }
a q
10 1012 fun fun
1012 q q
a a
10 1012 10 1012

1012 1012

main main

p p
1012 1012

once the value is returned,


activation record clears up
p
1012

p is pointing to fun, but activation record is cleared up and hence p is not pointing to any valid
address.

note : local variable ka address return nahi karna chaiye because return karne se activation record
clear ho jata hai but if we use static then it remains throughout program.

int *fun () void main ()


{ {
static int a = 10; int *p;
int *q = &a; p = fun();
return &q; pf ("%d",*p);
} }

does not save in activation record


chapters Page 152
does not save in activation record

topic : null pointer


the null pointer is the pointer that does not point to any location but NULL.
- to differentiate valid pointer/address from invalid.

why 0 is treated as null?


because, operating system guarantees that few first bytes of address in the memory will not be
allocated to any program.

int *p = (int*)0;
here we are initialising pointer with null, esi location jo exist nahi karti.

to check if pointer is null :

ptr == NULL;

Uses of NULL Pointer in C :

(i) to initialize a pointer variable when that pointer variable hasn’t been assigned any valid memory
address yet.

(ii) to check for a null pointer before accessing any pointer variable. By doing so, we can perform
error handling in pointer-related code, e.g., dereference a pointer variable only if it’s not NULL.

(iii) to pass a null pointer to a function argument when we don’t want to pass any valid memory
address.

(iv) a NULL pointer is used in data structures like trees, linked lists, etc. to indicate the end.

chapters Page 153


chapter - 8 (dynamic memory allocation)
Saturday, August 24, 2024 9:52 AM

dynamic memory allocation

types :

(i) malloc ()
(ii) calloc ()
(iii) re-alloc ()
(iv) free ()

(i) malloc () : memory allocation


used to dynamically allocate a single large block of memory with the specified size. It returns
a pointer of type void which can be cast into a pointer of any form. It doesn’t Initialize memory
at execution time so that it has initialized each block with the default garbage value initially.
- cheaper and not reliable.
- garbage values.

Syntax of malloc() in C :
(void*)malloc (unsigned int);

(void*) malloc (10);

10bytes
10 byte ka block allocate

malloc address return karta hai aur us address ko store karne ke lie we need pointer.
example :

assume we are storing 5 integers in the system where integer are of 2 bytes.
total bytes required : 5 x 2 = 10(bytes allocated)

int*p = malloc (10);


scanf ("%d", p+0); 10 10 20 30 40 50
scanf ("%d", p+1); 20
scanf ("%d", p+2); 30 1000 1002 1006 1008 1010
scanf ("%d", p+3); 40
scanf ("%d", p+4); 50 p

malloc return karega address 1000


if i run the same program on a system where the integer are of 4 byte then the behaviour is undefined
chapters Page 154
if i run the same program on a system where the integer are of 4 byte then the behaviour is undefined
because in this program we are storing 5 integer in 10 bytes while in case of 4 bytes/integer we need 20
bytes to store 5 integer.

the solution is; instead of allocating integer bytes we will write "[size of (int)]" so now it becomes
compiler or system dependent.

int*p = malloc (5 x size of (int))

int*p = malloc (10);


scanf ("%d", p+0); 10 10 20 30 40 50
scanf ("%d", p+1); 20
scanf ("%d", p+2); 30 1000 1004 1008 1012 1016
scanf ("%d", p+3); 40
scanf ("%d", p+4); 50 p

malloc return karega address 1000

in this, a piece of code is repeating

int*p = malloc (10);


scanf ("%d", p+0); 10
scanf ("%d", p+1); 20
scanf ("%d", p+2); 30
scanf ("%d", p+3); 40
scanf ("%d", p+4); 50
yahan hum for loop ka use kar sakte execute karane ke lie

for (i=0; i<5; i++)


scanf ("%d" p+i); 5 values read karega

int*p = malloc (10);


for (i=0; i<5; i++) 10 20 30 40 50
scanf ("%d" p+i);
1000 1004 1008 1012 1016

p[0] : (memory location 1000)


*(p[0]) = value at (memory location 1000)

printf ("%d", *(p+0)); 10


printf ("%d", *(p+1)); 20
printf ("%d", *(p+2)); 30
printf ("%d", *(p+3)); 40

chapters Page 155


printf ("%d", *(p+3)); 40
printf ("%d", *(p+4)); 50

in this, a piece of code is repeating


printf ("%d", *(p+0)); 10
printf ("%d", *(p+1)); 20
printf ("%d", *(p+2)); 30
printf ("%d", *(p+3)); 40
printf ("%d", *(p+4)); 50

yahan hum for loop ka use kar sakte print karane ke lie

for (i=0; i<5; i++)


5 values print karega
printf ("%d" *(p+i));

malloc (how much bytes we need)

if available if not available

returns the
returns the
starting address of block null pointer
(not a valid address)

int n, i;
int *p;
printf ("enter the number of elements");
scanf ("%d", &n);
p = malloc (size of (int) x n); if p is not equal to null then only the code will run means p is
if (p!=null) not pointing to any invalid address. (block is available)
{
for (i=0; i<n; i++)
scanf ("%d", p+i);
for (i=0; i<n; i++)
scanf ("%d", p[i]);
}

(ii) calloc() : contiguous allocation


“calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of
blocks of memory of the specified type.

it is very much similar to malloc() but has two different points and these are:
chapters Page 156
it is very much similar to malloc() but has two different points and these are:
(i) it initializes each block with a default value ‘0’.
(ii) it has two parameters or arguments as compare to malloc().
- expensive and more reliable
- initialised with "0" (garbage value se jada known values acchi hoti hai)

Syntax of calloc() in C

calloc (no. of block, size of each block)

(iii) re-alloc() : re-allocation


“realloc” or “re-allocation” method in C is used to dynamically change the memory allocation of a
previously allocated memory. In other words, if the memory previously allocated with the help of
malloc or calloc through some pointer is insufficient, realloc can be used to dynamically re-
allocate memory. re-allocation of memory maintains the already present value and new blocks
will be initialized with the default garbage value.
- grow aur shrink kar sakte hai hum isse.e
Syntax of calloc() in C
ptr = realloc (ptr, newsize);

example : (grow)

int *p = malloc (5 x size of (int))


for {
___
___
}

p = realloc (p, 10 x size of (int))

int *p = malloc (5 x 4) = 20 byte ka block mil gaya

10 20 30 40 50

1000 1004 1008 1012 1016

now, i want to store 10 elements instead of 5

p = realloc (p, 10 x 4) = 40 byte ka block chaiye toh agar memory available hai continiously toh vo hume
allocate ho jayegi

chapters Page 157


if available
10 20 30 40 50

1000 1004 1008 1012 1016 1020 1024 1028 1032 1036

otherwise, ek pura 40 byte ka new block alloacte


hoga aur previous block ka data copy ho jayega
which means there is no data loss

10 20 30 40 50 60 70 80 90 100

1000 1004 1008 1012 1016 1020 1024 1028 1032 1036

example : (shrink)
int *p = malloc (5 x size of (int))
for {
___
___
}

p = realloc (p, 2 x size of (int))

int *p = malloc (5 x 4) = 20 byte ka block mil gaya

10 20 30 40 50

1000 1004 1008 1012 1016

int *p = malloc (2 x 4) = 8 byte ka block ho gaya

10 20

1000 1004

it is not a data loss because we delibaterly decided to shrink the memory


- we do not need the elements.
chapters Page 158
- we do not need the elements.

it is the programmer's responsibility to allocate the memory, that means to de-allocate the memory after
the work has done is also the responsibility of the programmer.

concept : de-allocation
why we need to de-allocate?
void fun ( )
{
int *p = malloc (200 x size of (int))
for {
___
___
return;
}

v0id main ( )
{
fun ();
fun ();
fun ();
fun ();
fun ();
}

(i) fun () called for first time how much bytes operation system allocated : 800 bytes
int *p = malloc (200 x size of (int))

800 bytes allocated p is pointing to first section

p heap

(ii) fun () called for second time how much bytes operation system allocated : 1600 bytes
int *p = malloc (200 x size of (int))

we cannot access this block anymore as p is a local variable and


800 bytes allocated once the function gets terminated p is not pointing anymore to
this section of memory.

800 bytes allocated p is pointing to second section

p heap

chapters Page 159


(iii) fun () called for fifth time how much bytes operation system allocated : 3200 bytes
int *p = malloc (200 x size of (int))

we cannot access this block anymore as p is a local variable and


800 bytes allocated once the function gets terminated p is not pointing anymore to
this section of memory.
we cannot access this block anymore as p is a local variable
800 bytes allocated and once the function gets terminated p is not pointing
anymore to this section of memory.
.
.
.
.

no more bytes available p is pointing to fifth section

p heap
no more bytes available in the memory because we used 3200 bytes according to operating system, meanwhile we
cannot access any of those blocks. this concept is called as memory leakage means we are not using the memory
and we have not de-allocated it yet. here comes the concept of free ()

concept : free()

“free” method in C is used to dynamically de-allocate the memory. the memory allocated using functions
malloc() and calloc() is not de-allocated on their own. hence the free() method is used, whenever the
dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it.

Syntax of free () in C
free (ptr);

void fun ( )
{
int *p = malloc (200 x size of (int))
for {
___
___
free (p);
return;
}

v0id main ( )
{

chapters Page 160


{
fun ();
fun ();
fun ();
fun ();
fun ();
}

chapters Page 161


chapter - 9 (strings)
Saturday, August 24, 2024 10:37 AM

strings

sequence of characters terminated by null character ('/0')


string is stored as an array of characters.

the difference between a character array and a string is that the string in C is terminated with a
unique character ‘\0’.

ASCII code of null : 0


format specifier for string : %s

double quotes ke andar by default string hoti hai.

"aryan" a r y a n /0
null will be added by the compiler

string is stored as an array of characters :

char name [10] = "aryan" (char : 1 byte)


a r y a n /0
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

name

printf ("%s", name); aryan

(i) we can individually change the elements(content) of an array.


(read + write)

char name [10] = "aryan" (char : 1 byte)


a r y a n /0
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

name

name [1] = 'u';

chapters Page 162


name [1] = 'u';

a ur y a n /0
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

n[1]

(ii) flexibility to omit the first dimension of an array if initialised.


char name [ ] = "aryan"

compiler will count;


char name [6] = "aryan"

(iii) behaviour is undefined if you provide same count in initialization as string characters.

char name [5] = "aryan"


so, always provide the size of an array one more then the string size.

(iv) if we are providing everything explicitly by own then we also have to provide null.

char name [ ] = {'a', 'r', 'y', 'a', 'n'};


printf("%s", name); aryan#$%?

provide null also

char name [ ] = {'a', 'r', 'y', 'a', 'n', '/0'};


printf("%s", name); aryan

(v) the address of string is passed to printf, printf checks the content and prints the symbols till null.

#include <stdio.h>
void main () {
char name [ ] = "aryan";
printf("%s", name);
}

a r y a n /0
1000 1001 1002 1003 1004 1005

name : &n[0] : 1000


&n[1] : 1001
&n[2] : 1002

chapters Page 163


&n[2] : 1002
&n[3] : 1003
&n[4] : 1004
&n[5] : 1005

printf("%s", name+2); &n[0]+2 : &n[2] : 1002 : yan

another way to store string :


through pointer to character.

#include <stdio.h>
void main () {
char *ptr = "aryan";
printf("%s", ptr);
}

(i) string is the address of its first character.

a r y a n /0
100 101 102 103 104 105

ptr

char *ptr = "aryan";

iska address ptr ke andar jaa raha hai

(ii) we cannot individually change the elements(content) in pointer.


(read only area)

a r y a n /0
100 101 102 103 104 105

ptr

ptr [1] = 'u';


chapters Page 164
ptr [1] = 'u';
we cannot change the content individually

(iii) modify operators are valid in the case of pointers as it just points to next or previous address.
char *ptr = "aryan";

a r y a n /0
100 101 102 103 104 105

(pointer pointing to address 100)


ptr

++ptr : ptr = ptr + 1

a r y a n /0
100 101 102 103 104 105

(pointer pointing to address 101)


ptr+1

format specifier (%S) is optional

char name [10] = "aryan"


printf ("%s", name); aryan
printf ("%s",&n[0]); aryan
printf (name); aryan
printf (&name[0]); aryan

all four will have the same output

the difference between character array and pointer:

character array pointer

(i) char name [] = "aryan"; (i) char *ptr = "aryan";


name = "iram"; ptr = "iram";

this is invalid because we cannot char *ptr = "aryan";


assign a complete new string to an
array. it can only change individual a r y a n /0
element.
chapters Page 165
this is invalid because we cannot char *ptr = "aryan";
assign a complete new string to an
array. it can only change individual a r y a n /0
element. 100 101 102 103 104 105

(pointer pointing to address 100)


ptr
(ii) char name [10] = "aryan";
printf ("%s", name);a
char *ptr = "iram";

i r a m /0
address 200 201 202 203 204

(iii) char arr 1 [] = "hello" (now pointer is pointing to address 200)


ptr

h e l l 0 /0 so, in this case we can assign a


100 101 102 103 104 105
complete new string but we cannot
change the individual element.
(arr 1 have the starting address
arr1 100) in different times there can be different
character address. the previous array
will be in garbage.

char arr 2 [] = "hello" (ii) char name [10] = "aryan";


printf ("%s", ptr+2);
h e l l 0 /0
200 201 202 203 204 205

address
(arr 2 have the starting address
arr2 200)

array representation mai array banta (iii) char *p = "hello";


hi banta hai. char *q = "hello";

h e l l 0 /0
100 101 102 103 104 105

both pointers are pointing to the same location.

h e l l 0 /0
q 100 101 102 103 104 105

(p and q both are pointing to address 100)


p

pointer representation mai dono ek hi address ko point kar rahe.


chapters Page 166
pointer representation mai dono ek hi address ko point kar rahe.

how strings are implemented :

char arr1 [] = "aryan"


char arr2 [] = "iram"
char arr3 [] = "pranjal"

we can create a 3d array of it

char arr [3][8] = {"aryan", "iram", "pranjal"};


have to take atleast 8 because "pranjal" have 7 characters.

a r y a n /0 i r a m /0 p r a n j a l /o
n[0][0] n[0][1] n[0][2] n[0][3] n[0][4] n[0][5] n[0][6] n[0][7] n[1][0] n[1][1] n[1][2] n[1][3] n[1][4] n[1] [5] n[1][6] n[1][7] n[2][0] n[2][1] n[2][2] n[2][3] n[2][4] n[2][5] n[2][6] n[2][7]

name [0] name [1] name [2]


name

printf ("%s", name[0]); aryan


(address of n[0][0])

printf ("%s", name[0]+2); yan


(address of n[0][0]+2 : n[0][2])

printf ("%s", *(name[0]+2)); y


(value at(n[0][0]+2) : value at (n[0][2])

name [0] = "aakash" : invalid


it is an array name, we cannot assign a new string to an array.

name [0][0] = 'u' : valid


we can assign a new individual character to an array.

how multiple strings are stored using array :


using array of pointer to character.

char *p = "aryan"
char *q = "iram"
char *r = "pranjal"
3 pointer to character

chapters Page 167


3 pointer to character
they are of same type and for same type element we use array.

char*p[3] = {"aryan", "iram", "pranjal"}


array of 3 pointer to character

char*p[3] = {"aryan", "iram", "pranjal"}


strings stored in read area only.

52 100 a r y a n /0

100 101 102 103 104 105


p[0]

56 200 i r a m /0

p[1] 200 201 202 203 204

60 300 p r a n j a l /o

p[2] 300 301 302 303 304 305 306 307

basically p[3] is an array of address

(i) p = &p[0] = memory location 52


(ii) *p = *&p[0] = value at (memory location 52) = 100 (address of character 'a' in "aryan")
- printf("%s", *p); aryan

(i) p+1 = &p[0]+1 = &p[1] = memory location 56


(ii) *p+1 = *&p[0]+1 = *&p[1] = value at (memory location 56) = 200 (address of character 'i' in
"iram")
- printf("%s", p[1]); iram

(i) p[1]+2 = &p[1][0]+2 = &p[1][2] = address of 'a' in "iram"


- printf("%s", p[1]+2); am

p[0] = "iram"
strings stored in read area only.

chapters Page 168


200
52 100 a r y a n /0

100 101 102 103 104 105


p[0]

56 200 i r a m /0

p[1] 200 201 202 203 204

60 300 p r a n j a l /o

p[2] 300 301 302 303 304 305 306 307

p[0] is now pointing to address 200.

concept: pre-defined functions that works on strings.


(i) strlen
(ii) strcat
(iii) strcmp
(iv) strcpy

(i) strlen
the strlen() function in C calculates the length of a given string till null. it doesn’t count the null character
‘\0’. strlen() function is defined in string.h header file. return type is size_t ( which is generally unsigned
int )
syntax of C strlen()
size_t strlen(const char* str); unsigned int strlen (const char*)

char name [10] = "aryan" string length cannot be negative


int i ;
address we cannot modify original string
i = strlen (name)

char *ptr = "aryan"


int i ;
i = strlen (ptr) address

working of strlen () :

chapters Page 169


working of strlen () :

#include <stdio.h>
#include <string.h>
void main () {
int i;
char *ptr = "aryan"
i = strlen (ptr)
printf("%d", i);
}

a r y a n /0

100 101 102 103 104 105

ptr

(i) counter : 0 a r y a n /0

*p = null? no 100 101 102 103 104 105

counter increase to 1
p++ = p = p+1 ptr
(points to next address 101)

(ii) counter : 1 a r y a n /0
*p = null? no
100 101 102 103 104 105
counter increase to 2
p++ = p = p+1 ptr
(points to next address 102)

(iii) counter : 2
*p = null? no a r y a n /0

counter increase to 3 100 101 102 103 104 105

p++ = p = p+1
(points to next address 10) ptr

(iv) counter : 3
*p = null? no a r y a n /0
counter increase to 4
100 101 102 103 104 105
p++ = p = p+1
(points to next address 14) ptr

(v) counter : 4
*p = null? no
counter increase to 5 a r y a n /0

p++ = p = p+1 100 101 102 103 104 105

(points to next address 105)


ptr
(vi) counter : 5
a r y a n /0
*p = null? yes

chapters Page 170


ptr
(vi) counter : 5
a r y a n /0
*p = null? yes
100 101 102 103 104 105

counter stops.
ptr

we can create a for loop for this

int mystrlen (const char *p) {


int count = 0;
while (*p! = '\0') (until the value at p is not null)
{
count++;
p++;
}
return count ;
}

(ii) strcpy
strcpy is a C standard library function that copies a string from one location to another. It is defined in
the string.h header file.

function takes two arguments:


(i) a destination buffer where the copied string will be stored
(ii) a source string that will be copied.
the function copies the entire source string, including the null terminator, into the destination buffer.

it copies the string pointed by source pointer to the buffer/array pointer by destination pointer.

syntax:

char* strcpy(char* destination, const char* source);

(i) destination: pointer to the destination character array where the content is to be copied.
(ii) source: pointer to the source character array which is to be copied.
(iii)return value: a pointer to the destination string is returned after the strcpy() function copies the
source string.

example :

char arr [10] ; (we created the array but did not initialise it)
arr = "aryan"; (we cannot initialise the array like this)
so, the solution is strcpy
chapters Page 171
so, the solution is strcpy

strcpy (arr, "aryan");


"arr" array mai "aryan" string copy karni hai

somewhere in memory

char arr [10]; a r y a n /0

strcpy (arr, "aryan")


copies till null
printf ("%s", arr); aryan
a r y a n /0

note : do not try to store string in a pointer because pointer ko address milta hai

char *ptr;
strcpy (ptr, "aryan");

we can overwrite the string in array

char arr [20] = "imaryan";


strcpy (arr, "iram");
printf("%s", arr);

char arr [20] = "aryan";


a r y a n /0

strcpy (arr, "iram");

i r a m /0

i m a r y a n /0

i r a m /0 a n /0

chapters Page 172


printf("%s", arr); iram

(iii) strcat

strcat() function appends the string pointed to by src to the end of the string pointed to by dest. It
will append a copy of the source string in the destination string. plus a terminating null character.

the initial character of the string(src) overwrites the null-character present at the end of the
string(dest).

It is a predefined string handling function under string library <string.h> in c

syntax:
char *strcat(char *dest, const char *src);

(i) dest: this is a pointer to the destination array, which should contain a C string, and should be
large enough to contain the concatenated resulting string.
(ii) src: this is the string to be appended. this should not overlap the destination.

example :

char arr [20] = "aryan"


strcat (arr, "ishere");

char arr [20] = "aryan";


a r y a n /0

strcat (arr, "ishere");


i s h e r e /0 it will find null of the arr to append

a r y a n /0

a r y a n i s h e r e /0

chapters Page 173


note : if you dont want overflow then provide the size in which we can append the new string.

concept : problem with scanf

scanf ("%s", arr); aryan ishere


printf("%s", arr); aryan

scanf by default first white space par ruk jata hai

that is why we use "^"

scanf ("%[^\n]s", arr); aryan ishere


printf("%s", arr); aryan ishere

now white space is also allowed, and ab \n par rukega.

- we can also give it the set of characters that we want to allow in our string
scanf ("%[a-z, A-Z]s");

concept : gets () and puts ()

(i) gets () : reads the character and stores them as a string in str. (white space allowed)
syntax

int *gets (char *str);

str : pointer to a block of memory (array of char) where the string read is copied as string.
(ii) puts () : prints the string character until null.
syntax

int puts (char *str);

str : string to be printed.

(q). char s [20] = "aryanishere" (q). char s [20] = "pankajsharma"


printf ("%s", s+s[2]-s[3]); printf ("%s", s+s[2]-s[3]);

s+s[2]-s[3] a:x s+s[2]-s[3] k:x


s+y-a b : x+1 s+n-k l : x+1
s+x+24 - x c : x+3 s+x+3 - x m : x+2
s+24 . s+3 n : x+3
s[24] . s[3]
chapters Page 174
s+y-a b : x+1 s+n-k l : x+1
s+x+24 - x c : x+3 s+x+3 - x m : x+2
s+24 . s+3 n : x+3
s[24] . s[3]
error. y : x+24 kajsharma.

(q). printf ("%s",ptr+B-A);


ptr+B-A A:x
ptr + x+1-x B : x+1
ptr+1
ptr [1]

(q). char arr [] = "pankajsharma" (q). char arr [10] = "aryan";

printf ("%s", &arr[6]); sharma printf ("%d", strlen(arr)); 6


printf ("%s", &6[arr]); sharma printf ("%d", sizeof(arr)); 10
printf ("%s", arr+6); sharma
printf (&arr[6]); sharma

printf (&6[arr]); sharma

printf (arr+6); sharma

printf ("%d", sizeof("pankaj")); 7

printf ("%d", sizeof("pankaj\0")); 8

chapters Page 175


chapter - 10 (structure and union)
Saturday, August 24, 2024 9:53 AM

structure and union

topic : structure

structure in is a user-defined data type that can be used to group items of possibly different
types into a single type.

"struct" keyword is used to define the structure in the C programming language.

the items in the structure are called its member and they can be of any valid data type.
additionally, the values of a structure are stored in contiguous memory locations.

how to represent a student in a software?

student
- int roll
- char name
- etc

(i) struct is the keyword used to create user defined data type. just like primitive data type; int, float,
char. from now on a new data type exsist : struct student.

struct student { struct student {


int roll; int roll;
char name [20]; char name [20]="aryan";
} }

no memory is allocated, this is the information for the compiler that whenever i create the student type
structure there will be (int roll and char name) in it as members. we cannot initialise it also because there
no memory to store the value.

(ii) memory will only allocate when we create the variable for the data type.

struct student {
int roll;
char name [20];
} roll
void main {
int ;
chapters Page 176
} roll
void main {
int ;
struct student s1;
name
}
s1 variable is a group of two members
- roll s1
- name

struct student {
int roll;
char name [20];
}
void main {
int ; 10 20
struct student s1s2;
[Link] = 10;
[Link] = 20; name name
[Link] = "aryan";
}
s1 s2

[Link] = "aryan";
this will not execute because array name cannot be lvalue.

so, we have discussed the solution before

strcpy ([Link], "aryan");


strcpy ([Link], "iram");

10 20

aryan iram

s1 s2

• : it is a membership operator, to access a particular member of any structure we need the membership
operator.

chapters Page 177


concept : global vs optional vs typedef
global : defined the template optional: template is optional, iske variable typedef: typedef is a keyword that is used
globally, iske variable hum ek hi baar bana sakte hai to provide existing data types
hum kisi bhi fun mai that are defined globally. with a new name.
bana sakte hai
struct student {
struct student { int roll;
int roll; char name [20];
char name [20]; struct student {
}pankaj;
} int roll;
void main {
void main { char name [20];
pankaj s1, s2;
int ; }s1, s2, s3;
}
struct student s1s2; void main {
} cannot create variables here
struct student template name changed to
void fun (){ }
pankaj.
struct student s1s2;
}

(iii) the order of initialisation must be as same as the orde of members.


struct student {
int roll;
char name [20];
}
void main {
int ;
struct student s = {10, "aryan"};
{[Link], [Link]} = {10, "aryan"};
{[Link], [Link]} = {10, "aryan"};
}

(iv) we can copy the data of one structure to another.


struct student {
int roll;
char name [20];
}
void main {
struct student s1 = {10, "aryan"};
struct student s2 = s1;
struct student s3 = {"aryan"};
}

chapters Page 178


10 10

aryan aryan

s1 s2

(v) the values of a structure are stored in contiguous memory locations.

10
roll. name
10 pankaj

aryan

topic : union

the union is a user-defined data type that can contain elements of the different data types just like
structure. but unlike structures, all the members in the C union are stored in the same memory
location. Due to this, only one member can store data at the given instance.

(i) union is they keyword that is used to define the data type.
union pankaj {
char i;
int j;
}

(ii) in case of structures all members get individual (separate) memory space but all members of union
share common memory area.

union B{ max (1,4) : 4 byte


char i; 1 byte sabse bade member ke size ke equal
int j; 4 byte memory allocate hoti hai
chapters Page 179
union B{ max (1,4) : 4 byte
char i; 1 byte sabse bade member ke size ke equal
int j; 4 byte memory allocate hoti hai
}
void main {
union B b;
printf ("%d", sizeof(b)); 4
}

topic : scoping
(i) static scoping (lexical scoping) : scope related decision are taken on compile time.
(ii) dynamic scoping : scope related decision are taken on run time.

where to look for the value of a?

int a ;
{
{
{
printf ("%d", a);
}
sub sub scope
}
sub scope
}
main scope

consider the program in a hypothetical language that allows global variables and a choice of static and
dynamic scoping.

what is printed under static scoping?

int i ; in c :
program main () { int i ; global variable
i = 10; void main () {
call f(); i = 10;
} call f(); called the function f()
procedure f (1) { }
int i = 20; void f(1) {
call g(); int i = 20; f have its own local variable 20
} call g(); called the function g()
procedure g () { }
print (i); void g() {
} print ("%d",i); (f(1) terminated, it was a local
} variable. so the value of i will be 10.

chapters Page 180


what is printed under dynamic scoping? (value nahi mil rahi toh parent function mai search)

int i ; in c :
program main () { int i ; global variable
i = 10; void main () {
call f(); i = 10;
} call f(); called the function f()
procedure f (1) { }
int i = 20; void f(1) {
call g(); int i = 20; f have its own local variable 20
} call g(); called the function g()
procedure g () { }
print (i); void g() {
} print ("%d",i); g() does not have its own i, it will look
} in the parent function, so 20 will be
printed)

in dynamic scoping, function can access all the parent variables.

main()

f() g()
local : b local : a
can access :
d() main : a
local : c
can access :
l() f() : b
local : e main : a
can access :
f() : b accessibility is different because it is not fixed for every function, it is
d() : c decided on run time (kaunsa function kisko call kar raha).
main : a

in static scoping, function can access own variables as well as global variables

main()

chapters Page 181


main()

f() g()

d() accessibility is same because it fixed for every d()


function, it is decided on compile time.
local : a local : a

chapters Page 182


PYQs__Part_I_no_anno
Friday, November 8, 2024 8:01 AM

PYQs__Part
_I_no_anno

class pyq's Page 183


Page 2
Friday, November 8, 2024 8:02 AM

class pyq's Page 184


Page 3
Friday, November 8, 2024 8:02 AM

class pyq's Page 185


Page 4
Friday, November 8, 2024 8:02 AM

class pyq's Page 186


Page 5
Friday, November 8, 2024 8:02 AM

class pyq's Page 187


Page 6
Friday, November 8, 2024 8:02 AM

class pyq's Page 188


Page 7
Friday, November 8, 2024 8:02 AM

class pyq's Page 189


Page 8
Friday, November 8, 2024 8:02 AM

class pyq's Page 190


Page 9
Friday, November 8, 2024 8:02 AM

class pyq's Page 191


Page 10
Friday, November 8, 2024 8:02 AM

class pyq's Page 192


Page 11
Friday, November 8, 2024 8:02 AM

class pyq's Page 193


Page 12
Friday, November 8, 2024 8:02 AM

class pyq's Page 194


Page 13
Friday, November 8, 2024 8:02 AM

class pyq's Page 195


Page 14
Friday, November 8, 2024 8:02 AM

class pyq's Page 196


Page 15
Friday, November 8, 2024 8:02 AM

class pyq's Page 197


Page 16
Friday, November 8, 2024 8:02 AM

class pyq's Page 198


Page 17
Friday, November 8, 2024 8:02 AM

class pyq's Page 199


Page 18
Friday, November 8, 2024 8:02 AM

class pyq's Page 200


Page 19
Friday, November 8, 2024 8:02 AM

class pyq's Page 201


Page 20
Friday, November 8, 2024 8:02 AM

class pyq's Page 202


Page 21
Friday, November 8, 2024 8:02 AM

class pyq's Page 203


Page 22
Friday, November 8, 2024 8:02 AM

class pyq's Page 204


Page 23
Friday, November 8, 2024 8:02 AM

class pyq's Page 205


Page 24
Friday, November 8, 2024 8:02 AM

class pyq's Page 206


Page 25
Friday, November 8, 2024 8:02 AM

class pyq's Page 207


Page 26
Friday, November 8, 2024 8:02 AM

class pyq's Page 208


Page 27
Friday, November 8, 2024 8:02 AM

class pyq's Page 209


Page 28
Friday, November 8, 2024 8:02 AM

class pyq's Page 210


Page 29
Friday, November 8, 2024 8:02 AM

class pyq's Page 211


Page 30
Friday, November 8, 2024 8:02 AM

class pyq's Page 212


Page 31
Friday, November 8, 2024 8:02 AM

class pyq's Page 213


Page 32
Friday, November 8, 2024 8:02 AM

class pyq's Page 214


Page 33
Friday, November 8, 2024 8:02 AM

class pyq's Page 215


Page 34
Friday, November 8, 2024 8:02 AM

class pyq's Page 216


Page 35
Friday, November 8, 2024 8:02 AM

class pyq's Page 217


Page 36
Friday, November 8, 2024 8:02 AM

class pyq's Page 218


Page 37
Friday, November 8, 2024 8:02 AM

class pyq's Page 219


Page 38
Friday, November 8, 2024 8:02 AM

class pyq's Page 220


Page 39
Friday, November 8, 2024 8:02 AM

class pyq's Page 221


Page 40
Friday, November 8, 2024 8:02 AM

class pyq's Page 222


Page 41
Friday, November 8, 2024 8:02 AM

class pyq's Page 223


Page 42
Friday, November 8, 2024 8:02 AM

class pyq's Page 224


Page 43
Friday, November 8, 2024 8:02 AM

class pyq's Page 225


Page 44
Friday, November 8, 2024 8:02 AM

class pyq's Page 226


Page 45
Friday, November 8, 2024 8:02 AM

class pyq's Page 227


Page 46
Friday, November 8, 2024 8:02 AM

class pyq's Page 228


Page 47
Friday, November 8, 2024 8:02 AM

class pyq's Page 229


Page 48
Friday, November 8, 2024 8:02 AM

class pyq's Page 230


Page 49
Friday, November 8, 2024 8:02 AM

class pyq's Page 231


Page 50
Friday, November 8, 2024 8:02 AM

class pyq's Page 232


Page 51
Friday, November 8, 2024 8:02 AM

class pyq's Page 233


Page 52
Friday, November 8, 2024 8:02 AM

class pyq's Page 234


Page 53
Friday, November 8, 2024 8:02 AM

class pyq's Page 235


Page 54
Friday, November 8, 2024 8:02 AM

class pyq's Page 236


Page 55
Friday, November 8, 2024 8:02 AM

class pyq's Page 237


Page 56
Friday, November 8, 2024 8:02 AM

class pyq's Page 238


Page 57
Friday, November 8, 2024 8:02 AM

class pyq's Page 239


Page 58
Friday, November 8, 2024 8:02 AM

class pyq's Page 240


Page 59
Friday, November 8, 2024 8:02 AM

class pyq's Page 241


Page 60
Friday, November 8, 2024 8:02 AM

class pyq's Page 242


Page 61
Friday, November 8, 2024 8:02 AM

class pyq's Page 243


Page 62
Friday, November 8, 2024 8:02 AM

class pyq's Page 244


Page 63
Friday, November 8, 2024 8:02 AM

class pyq's Page 245


Page 64
Friday, November 8, 2024 8:02 AM

class pyq's Page 246


Page 65
Friday, November 8, 2024 8:02 AM

class pyq's Page 247


Page 66
Friday, November 8, 2024 8:02 AM

class pyq's Page 248


Page 67
Friday, November 8, 2024 8:02 AM

class pyq's Page 249


Page 68
Friday, November 8, 2024 8:02 AM

class pyq's Page 250


Page 69
Friday, November 8, 2024 8:02 AM

class pyq's Page 251


Page 70
Friday, November 8, 2024 8:02 AM

class pyq's Page 252


Page 71
Friday, November 8, 2024 8:02 AM

class pyq's Page 253


Page 72
Friday, November 8, 2024 8:02 AM

class pyq's Page 254

You might also like