C Programming 1
C Programming 1
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)
addition : ADD
subtraction : SUB
multipication : MUL
chapters Page 2
collections of these mnemonics are called assembly language.
(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.
(i) compiler
chapters Page 4
(i) compiler
(ii) 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.
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)
chapters Page 5
(a) (b) (c)
every memory
block has an
2186 address
3189
memory
every memory
10 block has an
2186 address
3189
memory
10 data
1084
address
types of data
(i) Google maps
source Delhi
text type data
destination GOA
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
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;
20 data
1084
address
chapters Page 8
9.8 data
1084
address
@ data
1084
address
chapters Page 9
chapter - 2 (data types and operators)
Monday, July 29, 2024 6:22 PM
data types
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)
(b) int :
(i) signed integer : -12, +12
(ii) unsigned integer : 12
(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
1 digit : 1bit
(i) 1st 2nd
10 10 (i) one bit - 0/1 : 2 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
for n bits
unsigned signed
0 to 2n-1 -2n-1 to 2n-1-1
#include<stdio.h> a
void main (){
int a = 20; 20
printf ("a");
} 1084
output : a
(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
#include<stdio.h> a
void main (){
int a = 10; 10
printf ("the value
is %d",a); 1084
}
#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
cyclic property
integer %d
65535 0 1 -1 0 1
2 2
65534 -3 -2 3
-4 4
#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
-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....
---
}
@ 001___0000
information information
chapters Page 17
255 0 1 -1 0 1
2 2
254 -3 -2 3
-4 4
#include<stdio.h> c
void main (){
char c = 65; 0100 0001
printf ("%d"c");
}
value stored
#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
(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");
} }
#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
25 rows
topic : operators
10 + 20=30
10 x 20=200
10, 20 (operand)
+ , X (operators)
operators
10
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;
}
#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
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
- high (X, /, %) L to R
- low (+, -) L to R
example :
- 8/2+3
(8/2)+3 as the priority of / is more than +
associativity
(i) left to right .
(ii) right to left.
#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
priority / precedence
- #include<stdio.h>
void main (){
printf ("%d", 10<20);
}
output : 1
- #include<stdio.h>
void main (){
printf ("%d", 0>0);
chapters Page 25
printf ("%d", 0>0);
}
output : 0
- #include<stdio.h>
void main (){
printf ("%d", 10<=10);
}
output : 1
- #include<stdio.h>
void main (){
printf ("%d", 20<=10);
}
output : 0
- #include<stdio.h>
void main (){
printf ("%d", 10==10);
}
output : 1
- #include<stdio.h>
void main (){
printf ("%d", 20==10);
}
chapters Page 26
}
output : 0
- #include<stdio.h>
void main (){
printf ("%d", 10!=10);
}
output : 0
- #include<stdio.h>
void main (){
printf ("%d", 20!=10);
}
output : 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)
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)
(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)
(iii)printf("%d",3)
-1
the output value is 1 if both operands are non-zero (true) otherwise the output value is 0.
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
the output value is 1 if at-least one operand is non-zero (true) otherwise the output value is 0.
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.
It is also called a negation operator, which means it takes the input operand and negates it
- #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
output : 1
if the 1st operand is non-zero for logical OR, then the second operand will not be evaluated.
modify operators
increment(++) decrement(- -)
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
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
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=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
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
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
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
- #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
- #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
second time :
20 : 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0
in general :
a<<2 is a x 2 x 2
a x 22
5 x 22
5 x 4 = 20
chapters Page 38
shift 'X' 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
- #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
chapters Page 39
second time :
2: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
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
11110110
= -23 - 20 - 1
= -8 -1 -1
= -10
- #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
- #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);
}
- #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
i j k l m
i j k l m
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
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.
chapters Page 44
a = 20>100 ? 100 : !2 != 3>50 ? 300 : 400;
exp1
(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
(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.
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;
}
- #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");}
} }
if (1==0){ if (0==0){
printf ("pankaj");} printf ("pankaj");}
if (0){ if (1){
printf ("pankaj");} printf ("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;
}
- #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;
} }
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
- #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;
}
- #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
chapters Page 54
starts updating
from 1 till 5
checks if 1<=5,
starts yes then it will
from 1 go to printf
checks if 2<=5,
starts yes then it will
from 2 go to printf
checks if 3<=5,
starts yes then it will
from 3 go to printf
chapters Page 55
checks if 4<=5,
starts yes then it will
from 4 go to printf
checks if 1<=5,
starts yes then it will
from 5 go to printf
checks if 1<=5,
starts no, then it will
from 6 stop executing
(1) (2)
(4)
format : for (initialization; condition; inc/dec)
{
(3) code
}
(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.
- #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
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
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
outer loop
outer loop
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
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
- #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
- #include<stdio.h>
void main (){
for (i=1; i<=n; i++)
{
for (j=1; j<=3; j++)
{
printf ("pankaj");
}
}
}
chapters Page 66
}
- #include<stdio.h>
void main (){
for (i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
{
printf ("pankaj");
}
}
}
- #include<stdio.h>
void main (){
for (i=1; i<=n; i++)
{
for (j=1; j<=n; j=j x 2)
{
printf ("pankaj");
}
}
}
chapters Page 67
- #include<stdio.h>
void main (){
for (j=128; j>=1; j=j/2)
{
printf ("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");
}
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
- #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);
} }
- #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
chapters Page 73
keyword : used to create selection statement with multiple choices
multiple choices are provided with another keyword (case)
switch (n) {
switch (2x5+3) {
case 1: block of statements
break;
- 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;
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)
- 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;
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;
output : 07
chapters Page 76
chapter - 4 (functions and storage classes)
Wednesday, August 14, 2024 5:12 AM
CS/IT MECH.
CS/IT EE CIVIL MECH.
EE
CIVIL
#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.
compiler don't know about printf we have to provide the information regarding printf so we have to
give the forward declaration to compiler.
#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.
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);
}
#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.
#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
chapters Page 80
concept - how function works
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
}
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
actual arguments
swapping function :
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)
scope :
auto / local
#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
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
#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);
}
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
register int a;
requests
if grants if rejects
static variable
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
++i a a
(ii) fun()
++a no redeclaration because value
0 1 1 2 persist throughout the program
a a
++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)
}
global variable
(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.
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);
}
#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;
#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.
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
...
7 6 5 4 3 2 1
...
returns 7 to teacher
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/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);
}
}
- 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)
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)
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)
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
f(3) 2 f(3)
f(0) pf(1)
f(0) pf(1)
f(3)
pf(3)
output : 123
note : statements written after recusrive call execute in opposite order of call.
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)
recursion tree
f(3)
f(3)
1 1 1
1
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
}
}
ab
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);
}
}
(i) address
example :
(i) the memory of variables can be different but in array the elements are stored sequentially one
after another.
a b c
----4byte---- ----4byte---- ----4byte----
2036 3196 1980 1000 1004 1008
starting address
(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
(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.
(vi) initialization and declaration, also elements are stored sequentially one after another.
10 20 30 40
(vii) the concept of garbage ends even if we initialise a single value in array, the rest of array
will be filled with 0.
10 20 0 0
(viii) the array "[]" is invalid because it cannot be empty, the group size is 0.
- int a[2+2];
(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.
1000 = 10;
must be some expression
variable variable
(xii) array_name is the constant address of first element of array we cannot use modify operators.
2++; invalid
(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
void main () {
int a[4]; {10, 20, 30,40};
10 20 30 40
10 20 30 40
10 20 30 40
concept : what is a?
numerical value of a is 100
a+1
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
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
(a) what is a?
- a is address because it is 0 dimensional and array is of 1 dimension.
a+1
&a[0]+1x4
= 100 + 4
= 104
10 20 30 40 10 20 30 40
(a) what is a?
- a is address because it is 0 dimensional and array is of 1 dimension.
a+2
&a[0]+2x4
= 100 + 8
= 108
10 20 30 40 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])
standing outside
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])
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};
10 20 30 40 50
concept : 2D array
int a[2][3]
a[0] a[1]
void main () {
int a[2][3]; {10, 20, 30,40,50,60};
10 20 30 40 50 60
a[0] a[1]
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
void main () {
int a[2][3]; {1, 2, 3,4,5,6};
1 2 3 4 5 6
a[0] a[1]
*(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};
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]
if only declaration is there without initialization, it is mandatory (compulsory) to provide the size
of each dimension.
in case, we are initializing an array, there is flexibility that you can omit the size of ist dimension.
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
int **q
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
pf ("%u",*p); 10
dereferencing
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
0100 0001
pf ("%d",*p); 10
1000
p = p+1; p
address + value = address
value + address = address
address value
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
pf ("%d", p[0]); 10
pf ("%d", p[1]); 20
pf ("%d", p[2]); 30
pf ("%d", p[3]); 40
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
--p;
(i) decrease the value
p=p-1
p = 112 - 1x4
p = 108
112 108
modifying pointers :
(actual difference)
p-q=
integer size
112 - 100 12
= = 3
4 4
(i) ++*p
++ (10)
11
(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
20
++(*(p++))
(ii)++(*(p))
(i) increase the value
++(value at (memory location 100))
++10
11
(iii) * : pointers
(ii) (right to left)
(iv) identifier
100 10 20 30
&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
a+1 a a+2 10 20 30 40
(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
swap function
parameters passed by address (call by reference)
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.
10 20 30 40
(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]
10 20 30 40
- *(&a[1])
*&a[1]
a[1]
20
10 20 30 40
chapters Page 134
a[0] a[1] a[2] a[3]
10 20 30 40
++ *p++; ++(*(p++))
(i) p++
(2nd time)
(a) use the value (used)
useless
(ii) increase the value.
(ii) ++(*p)
++(*&a[0])
++(a[0])
++(10)
11
jab bhi aap ek array pass kar rahe hai jisme no. of element of array par kaam ho raha hai toh
batana padega.
10 20 30 40 50 60
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
- *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] a[1]
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
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
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(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.
declaration of pointer :
#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;
}
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
(ii) p[0]-p[1]
&a[2] - &a[2]
(actual difference)
p-q=
integer size
108 - 108 0
= = 0
4 4
(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.
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)
2 1
a[i][j] = *(a[i]+j)
ptr = p+3
ptr = &p[0]+3
ptr = &p[3]
ptr ptr
++*ptr++;
(i) ptr++
useless
(ii) ++ (*ptr)
++(*&a[1])
++(a[1])
++(a[1][0])
++ 5
6
- *p[*p[1]-8]
*p[*&b-8]
*p[b-8]
*p[10-8]
*p[2]
* &c
15
ptr ptr
1 *a[3]
*&a[3]
a[3]
40
1 2 3 4 5 6
(b) *p = *p x *p ;
*p = 3 x 3;
*p = 9;
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
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
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);
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.
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.
points to remember -
(i) we can not dereference a void pointer directly.
(ii) first typecast then only dereference.
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
void main ()
{ garbage
int *p;
}
1012 1012
main main
p p
1012 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 *p = (int*)0;
here we are initialising pointer with null, esi location jo exist nahi karti.
ptr == NULL;
(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.
types :
(i) malloc ()
(ii) calloc ()
(iii) re-alloc ()
(iv) free ()
Syntax of malloc() in C :
(void*)malloc (unsigned int);
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)
the solution is; instead of allocating integer bytes we will write "[size of (int)]" so now it becomes
compiler or system dependent.
yahan hum for loop ka use kar sakte print karane ke lie
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]);
}
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
example : (grow)
10 20 30 40 50
p = realloc (p, 10 x 4) = 40 byte ka block chaiye toh agar memory available hai continiously toh vo hume
allocate ho jayegi
1000 1004 1008 1012 1016 1020 1024 1028 1032 1036
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 {
___
___
}
10 20 30 40 50
10 20
1000 1004
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))
p heap
(ii) fun () called for second time how much bytes operation system allocated : 1600 bytes
int *p = malloc (200 x size of (int))
p heap
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 ( )
{
strings
the difference between a character array and a string is that the string in C is terminated with a
unique character ‘\0’.
"aryan" a r y a n /0
null will be added by the compiler
name
name
a ur y a n /0
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
n[1]
(iii) behaviour is undefined if you provide same count in initialization as string characters.
(iv) if we are providing everything explicitly by own then we also have to provide null.
(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
#include <stdio.h>
void main () {
char *ptr = "aryan";
printf("%s", ptr);
}
a r y a n /0
100 101 102 103 104 105
ptr
a r y a n /0
100 101 102 103 104 105
ptr
(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
a r y a n /0
100 101 102 103 104 105
i r a m /0
address 200 201 202 203 204
address
(arr 2 have the starting address
arr2 200)
h e l l 0 /0
100 101 102 103 104 105
h e l l 0 /0
q 100 101 102 103 104 105
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]
char *p = "aryan"
char *q = "iram"
char *r = "pranjal"
3 pointer to character
52 100 a r y a n /0
56 200 i r a m /0
60 300 p r a n j a l /o
p[0] = "iram"
strings stored in read area only.
56 200 i r a m /0
60 300 p r a n j a l /o
(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*)
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
ptr
(i) counter : 0 a r y a n /0
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
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
counter stops.
ptr
(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.
it copies the string pointed by source pointer to the buffer/array pointer by destination pointer.
syntax:
(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
somewhere in memory
note : do not try to store string in a pointer because pointer ko address milta hai
char *ptr;
strcpy (ptr, "aryan");
i r a m /0
i m a r y a n /0
i r a m /0 a n /0
(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).
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 :
a r y a n /0
a r y a n i s h e r e /0
- we can also give it the set of characters that we want to allow in our string
scanf ("%[a-z, A-Z]s");
(i) gets () : reads the character and stores them as a string in str. (white space allowed)
syntax
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
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.
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.
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.
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.
10 20
aryan iram
s1 s2
• : it is a membership operator, to access a particular member of any structure we need the membership
operator.
aryan aryan
s1 s2
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.
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.
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.
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.
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)
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()
f() g()
PYQs__Part
_I_no_anno