SUMMARY
sizeof (short) <= sizeof (int) <= sizeof (long).
Writing signed int some_variable_name; is equivalent to writing
int some_variable_name;
Peete red ames art EK ea
‘%u isusedto print “unsigned integer”
PACT) Tam asta iene Ns ulmcmerourr Molle)
integer”
‘%lu isusedto print “unsigned long integer”
‘“Ulld isusedto print “long long integer”
“ally is usedto print “unsigned long long integer”
.
OK friends, this is it for now.SIGNED UNSIGNED RANGE REPRESENTATION
[oy
BASLE Os
Ara
Cenc eete]
BrenCS TO)
fe SIGNED
UNSIGNT t's try to verify it by executingASCII TABLE
Decimal Hex Char {Decimal Hex Char___|Decimal Hex Char Decimal Hex Char
° OMT 32 20 (SACET | 64 a 0
a 1 fsrarrornesona) | 33. ant 6 a 37 cary
2 2 (sranrorvexn of34 2S 66 2 Bb [se a2 8
3 3 tenorrexn) 35 236 67 ac |oo ee
4 4 Tenoorraansmssion: | 36 2s 68 4¢ Dp [100 oo
é 8 facxwont 3 3 & te tof fio ee oF
5 oto a 6 ios 9
a 3 taackseacer q aan | 404 ad
9 HORIZONTAL TAB) ) a 1 |ios of
10 ‘A (UNEFEED) * “a 106, eA
i 8 (venricat 40) + ao — |io7 sk
2B © (ronmreeD) ; act |it0e ee
G DB feanaiace neruRn) : som Nios $m
14 © (sueroun 2 "
15 Fo ISHETINI 1 eae Oneal ar
16 10 (DATA UK escare) ° So aie ae
y HL pewceconnat 2 2g |nG §
1a 32 fbewce conmot 2 52 iia mF
19 33. (oevce conmaot 31 3 53 5 [ais as
20 34 [DEVICE CONTROL 4 2 st Tite "ae
2 15 eGarve acknonteDoe} 5 eg |e re
22 16 Ismcnonous oe 5 eo | ne is oy
33 17 (encor rans. .0cx 57 119 ow
2a 18 (canceu! 8 58 x |.420 wm x
25 19° [END OF MEDIUM 2 soy fiat my
26 4A [sussriuTe? saz | a2 a
27 1B fescare) sr a3 Be
33 1b [enoup senna 55 135 =
i Pa 7 126 eS
ay Te urecoanc RUELM ES Some UOCSO Mceekelae settee | 129 7 peuwr
DEL
(vertical Tab)
(Gorm teed)
(Carnage return)
‘Sait Ou)
(Grit in)
(sta ink escape)
{Device contol 1)
{(Gevice conto! 2)
(Device contol 3)
(Dowie corivol 4)
(egative ackrow )
(Synchronous iio)
(Endorwans block)
(Concel)
(End of medium)
i
SSSssaess
BRLSSSLSRLSSLSSsIaaRIIIsIssIsaz
town Reena
Inceocurt 000020
SASGREE
toast
Lamas mmmoo niet ba emt ld +
i
o
1
2
a
4
5
6
7
a
°
AN
E
t}woan< xg ) oot double ‘C\Users\aspe\Downoads\C programs of dennis itchi\floatarithmatc axe
2 int m
int v
print ("sd
7 float
10 float
ia
13
Logs & others
Covessods >) Search resuts x) Acie x Ould og > # BSiathere > float doublese ~ foot xithmetice
‘C\Users\josp”\Downloads\C programs of dennis ritchi\float double.e
Logs 8 others
‘7 code:odks x) Search reuks =) Fcox x) OBuld op | ¢ Build messageQ1: what is the output of the following C program fragment:
#include
int main(
print » printf » "Hello World!"));
return 3
Prenat bred mune Kenko ce oi
STROMA aR MCU RULE seta ld
RRMA oc melon kel eRe Le Tes 100
Ra eet
12 and this printf function printsw
8
Siathere > questions anewers sete
‘CAUsers\aspADownlo,
answers-ete
return
Logs 8 thes
Coesiods >) Search esuts x] 2 Gne_» © Bul og
fe: coo saxo RCSA LCM EMS) Cre Rod LOQ4: Which of the following statement/statements is/are
correct corresponding to the definition of integer :
Se ny MMOH MW Aelk Kec
Sey MC antec tal
unsigned i; ROM elec
longi; POR MVAectecnc ag
long int i;
long long i;
Crete Trae
(s(t To UyQS: What does the following program fragment prints?
main() { garbage
i ss)
Integer value depends from
iiteCoaT i Rcomaale a9
NeTaT Kel mU-Ke olen}
-3 in 2s complement representation:
Se Rela Maid
3 = 00000000 00000000 00000000 00000011
a Keele) ea MUM °°)
Step 2: Add 1 to the result. It will give
1111111 11111111 integer value that depends)3 (on my computer)
Sree a omnes#include
int main(
printf ("%d
return @;
t
int fun()
{
}
printf (
Seto ol-Mel mia Zell) NLT
main0 function only. Therefore
fool to mainQ function.
Trying to access variable ‘var’
outside main( function
err R UR CUCL oa)
scope is within that functionet
aaee
ana)
Pete
Ver ane)
internal
ilead
eto
Teall
block are not
ears
outer block,
Basic PRINCIPLE OF SCOPING
paneer
UES er-lacelll elm ele eee nicole
eee
this block is
not visible to
Cased
CUE
So
net
cee
Pea
eon
outside to
SUD lrg
main() {
var = 3;
var = 4;
printf("%d\n", var);
printf("%d", var);
a;
main() {
var = 3;
{
var = 4;
print#("%d\n", var);
y
printf("%d", var);
e;
you are not redefining within same block#include
int fun();
This variable is outside of all functions.
Therefore calleda variable
int main() {
one
return @;
(orn ea)
fun()
printf ; It will access the GLOBAL variable.
of global variable.pean ane
Also called
ited #define (NAME )value
Bolo do) fll Kole -1o) a Urol meen) ole Lot o1.0 (MLR 1,
stdio.h>
3.14159
3.14159
int main() {
printf( )3 eR)
Press any key to continue.
as well as easy to write.TAKEAWAYS
We can use macros like functions.
#include
#define add(x, y) x+y
int main() {
printf("addition of two numbers: %d", add(4, 3));
return @;
7
addition of two numbers: 7TAKEAWAYS
@) WV eKeto MW ALM RAUL) cM Aa AY
greater(x, y) if(x > y) \
printf("%d is greater than %d", x, y)3 \
else \
printf("%d is lesser than %d", x, y)}
main() {
greater(5, 6);
as the values to x and yTAKEAWAYS
6) First expansion then evaluation.
#include
#define add(x, y) x+y
int main() {
printf("result of expre * ’ add(4, 3))
return @;
Cece)
r eeeTAKEAWAYS
Some predefined macros like __DATE__,
——TIME__ can print current date and time.
#include
int main() {
printf ("Dat
printf ("Tim
return 0;
OK friends,WW conste - Codelocks 1601
Fla Edt View Search Project Sul Debug Fortian Smith Took Tole+ Plugins Dowylocks Settings Hap
eagles xealaaiorsoa gerecccce us| em
lo slh mem
Bathee [conte =
T tinctude
Ant main()
a
prince ("td", var):
8
Ce ee
(ewe RVR rea -e-Q1: What is the output of the following C program?
ee
Ome
Ore
Cyc etsirg
rea a)
print an octal valueQ2: What is the output of the following C program?
#include Pn errs
#define STRING "%s\n" b) Me ome te Nese
#define NESO “Welcome to Neso Academy!" ce
¢) Garbage value
int main() {
a oleola Rode)
printf(STRING, NESO); Academy!
return 0;
+
rintf(STRING, NESO); printf("%s\n",
"Welcome to Neso Academy!");
They are one and the same
option d and not option b.Ivalue (left value) : simply means an object that has an identifiable location
Cuma -aoln Amare are Kola Kels (0 ft)
SNe ee aa lua am el MR
ened cher cltl
lvalue must be a variable because they have the
(etel fell NaC Celt-min-Xe (ohio
Ave Reel aha Toe ean Ao kee)
oo CR RLS 8
rvalve (right value) : simply means an object that has no identifiable
Tercera an
rhe oedema tren sia
Cer cera ole
~ Expressions lik-Eypregsions like (a¥'b) OStant valve.
TU RI oomShort circuit in case of &&: simply means if there is a condition
CT Ana oR Ra aod e oR UNO el Ral aR mo Lal}
[oolaTe (eo Um ULM one of Rel elo com
e
POEL w yl m elm UySame ce aoe komme au Mae ee Rolie (ea)
CIR etes aes URC Samana ces Reais
[ooTaTe (reo Um ULM ode ot Reet 08
ude
main() {
int a
int incr;
iner = (a > b) || (b++)5
» incr);DY iaiai ais tain [ol] AWA] tN A S] NN ec] (o7
(0) aia hela)
does
x = 1, y = 2; //x = 1(00@@ 0001), y = 2(0000 0010)
(x8y) //1&2 = @(@00@ 9000)
printf("Result of x&y is 1");
(x&ay)
printf("Result of x8&y is 1");
//18&2 = TRUE && TRUE = TRUE = 1
a
Result of x&&y is 1.IMPORTANT POINTS
2.) Right shifting is equivalent to division by
grightOperand
VCH]
Teese Output: 1 [3 / 2°]
Vel
Nea Crh ea a 2]
MMi cc-a(e)e RCV AClIUe 16ASSIGNMENT OPERATOR
Neo ete tesa nls ee nn ake eases
Cer CRC Uc mee ee eel Link of video
provided in the
, ; ~ description
This operator copies R-value to L-value
Here the variable is L-valueC7
i
3
3
=
5
3
FS)
ES
Cy
Bs
o
=
3
ro
cI
A
ce
=
INTRODUCTION TO CONDITIONAL OPERATOR
es
Look and feel:
char result; char result;
Pala er Pla er
if (marks > 33) result = (marks > 33) ? ‘p? :
ea
,
CS
i
ieee
} behavior of if-else construct.Quick FACTS CHECKLIST
——
Conditional operator is the only ternary operator available in
the list of operators in C language
Asin |Expression ? Expression2 : Expression 3], expression!
is the boolean expression. If we simply write @ instead of some
erect eee Rue auch eect
espe Cae Relea teice m
ocray aed Travan
result =@?2:1
rlComMaA (, ) OPERATOR
—
Comma operator can be used as an “operator”.
int a = (3, 4, 8);
6 2)
printf (“%d”, a);
Comma operator returns the |rightmost |operand in
the expression and it simply evaluates the rest of
Se Ieee me UCTen ais case
operands and finally reject them.oT Ta Mel eee UT
RRO eee CURE RRS SNS Ce eee sae ag
PO Sauna ee uch es ms tae
This value will be
returned to var
after evaluating
See aa a the first operand
pan LCL dG se Tae a)
A 2
printf (“%d”, var); SS
\ It will simply not
meats rejected. First
evaluated and then
rejected
And finally we will print this‘Ee
ComMMA (, ) OPERATOR
ne
Comma operator is having least precedence among
all the operators available in C language.
ca
int a; pa
rome) == | © Bo 4h Ge
Pua AMAA ODE
Othe)ComMMaA (, ) OPERATOR
i
Comma operator is having least precedence among
all the operators available in C language.
ee ee
int a = 3, 4, B (elie Teena
ee aE
int a = 3, 4, 83
Peco Mar dcx) Melle
eereltel ts int a = 3; int 4; int 8;
Ertsst Tele) (ss aNComMMaA (, ) OPERATOR
a
Comma operator is having least precedence among
all the operators available in C language.
eee
ae Frac a
Cn PDB
printf (“%d”, a);
Stade Cae
e Cieree each acd 5
baal Foresters Cuchi oie ieee)
other operatorASSOCIATIVITY OF OPERATORS
Ooty Sa ee
Deere Mak ken eric M al acect cae Riel eae eee
Someta ace keke ara ume ll ee tee ies
ere Need
cue 1) 7 AC 3 a
Se ele
fold
Left to right: (10 /2)*5 = ( CMA ld
Right to left: ot ee}
And this one is not correct. Okay.PRECEDENCE AND ASSOCIATIVITY TABLE
CATEGORY
OPERATORS
EN ra NVR
Prue yD Teed
(0 0 eran
ree Ca
Unary
er
rom tcal
Cras as
Multiplicative
ices
PU resnT
ceed
Chee Gad
ieee
POETS icyy
Tree Ca
enc a ena
ee a
CeCe ID)
cota
Coes
Tree Ta
Cae
iamet
ete van)
ste
renee Cia
pene Cie
elit hater Ie
Right to left
Assignment
Stila ae
ol
aac eetOR accrues C nee
[cl ee Prana =fun();
Ceca
= operator is having less precedence as compared to ()
therefore, () belongs to fun and fun will be treated as a function.
int var =(fun());
Ifsuppose = operator is having greater precedence then, fun will
POR CIeE erin R icc eae: el
variable,
int (var =fun)();Cl ola eet) 11
Eg They are used to access members of structures.
ATR el Xe LoL M Uo L CMR ACO 61 8
Postfix Increment/Decrement (++, --)
Be Aoi mecca era eee eimai
ecu ca
Es Associativity of Postfix is also different from Prefix. Associativity of postfix
operators is from left to right and that of prefix operators is from right to left.
’
is from left to right and that of
fey ae) eM MCN ETe MCN (1aee ae
ST Tan EFtTO) Ta c@}
F a tad to
rae ort ane
Eee O ee UelOr h
Pisa Cane
fata ae
int fun2()
fee et sdar CCL
Resale
}
Tonner ke) seh taed
fun1() or fun2()?
Itis not defined whether fun10 will be called first or whether fun20 will be called. Behaviour is undefined and.
esc ee ca
NOTE: Here associativity will not come into picture as we have just one operator and which
function will be called first is undefined. Associativity will only work when we have more than one
Cerrar gies ’ aNC standard is the language specification which is adopted by all C compilers across the globe.
Te a SE ge
ras
; Citis the latest revised version of C
According to C99 standard: rele eleeRet tse A OE
The sizeof operator yields the size (in bytes) of its
CI a RM CM eM EC u terete marl Ts
Coa ie at PEER (setae deel ey oy Tela
operand. If the type of the operand is a variable length
array type, then the operand is evaluated; Seo ae
operand is not evaluated and the result is an intege!
oleae aha
We will talk about
Therefore, i++ inside sizeof is not evaluated yoriable length
y arrays later in this,
course.Rigut SOLUTION
ree Rey
Cree ae
sane
d = b-- && --a;
i A,
Erma aT a
printf (“%d %d %d %d”, d, c, b, a);What is the output of the following C program fragment?
Saar] is
int b = 1;
eer res
int d = b¥- && --a;
printf(“%d %d %d %d”, d, c, b, a);ALE -l l(t Mehl
Eure e (=| @) er)
{ t
PIS eo StisdGe are
ils=8 ile=g
pa reeuee) |
Cie ome sa re ea)ease
Used to terminate from the loop
Example: WAP to allow user to enter integers until he/she
neler MM )-<- AM Lo a ee
.
negative number or a zero.CONTINUE
Similar to break but instead of terminating from the loop, it forces to
rv R a Mamie hela kon mala (olel om
Se CeO Stree Me ec
.
We need to write a programWhat is the output of the following C program fragment?
#include =) 5 10 15 20
int main()
af
int i
for(i=0; i<20; i++)
i
switch(i)
a
There is no break;
after cases
he
[Sz TV aad
yy
printf(“%d ”, i);
And hence the whole program
is successfully completedWhat is the output of the following C program fragment?
ESM CMe eas: FCS a) 5 10 15 20
Stam @) b) 71217 22
g Oe Sea als
int i; eer e
for(i=@; i<20; i++) ©
4
switch(i)
{
rae
ert
case 5: i += 5;
default: i += 4;
}
printf (“%d ”, i);ECCRLCeS UCR SOM Cee Lec Mle eta ed
PYRE eat Td
eet
c) Infinite times
het
Tach
while(i <= 5)
if(i >= @)
aera
tS
{
coo
tls
h
printf (“Neso”) ;
Therefore, the correct option is option d).What is the output of the following C program fragment?
Assuming size of unsigned int is 4 bytes.
#include
int main()
{
Pesta eae eee
Prete teen ea)
stad a eee
Pet
Range of unsigned int (4 bytes)
@ to 4294967295
When i reaches 4295967295, then
Pete Me gee ames aeee oe tas
Perea)
ee mat Ura Ce TES
therefore we come outside of the
while loop.
ae Mo TL aoa at ae hot)
will contain 1 because of post
dacs Cyr eos eee
he printed on the screen.
And due to rey an SURn-2 n-1 n n+1 n+2
if(j >= n-(4-1) && j <= n¥(i-1))
asd Gao
ET .
printe(* “)5
Se ed
As this is the correct code,Basics
What is a palindrome?
ANumber or a word or a phrase if read backwards gives the same number or a
word or a phrase as it is being read forward.
Example: 1221, racecar, 24542 etc.
felon
eae Ut ear MoMA lol mE ele Mallee e|
Tea
Da es Ue Ne aut ea aut iali
eta hin cmh etn ac mucht acme cle gens
.
EverStrong number: is a number in which the sum of factorial of individual
digits of a number is equal to the original number.
Se Se
Wes all ce al + 5! = 1+ 24 + 120 = 145BASICS OF FUNCTION
les) Or (eo
Pra e yao
aac e Cesc OR Res ee area ae) oT eon
perform some computation and produces output.
Mea
tae Mime Lc) Drs es elke
¥
See REC ee erstWHY FUNCTIONS
A
Dea eee een a ee SES ae Last eet
CIEE Bena)
ee 5 rl
Once the function is defined, it can be reused over and over
again.
2. Abstraction
If you are just using the function in your program then you
Colt a) oO LoL ae) Tl oe
Se eee aaa ast)WHAT IS FUNCTION DECLARATION
—
Similarly, function declaration (also called function prototype) means
Penh te eae kh aernkchuc tse
For example: int fun(int, char);
+
(Trey ees
eluate aU alot ah)
mY Rea Vite ae
Number of parameters: 2
ay Xod ete ucu as ia
Ui etKeui vel celia charIMPORTANT TAKEAWAY
—
Tame ACY Ray eet naraon)
Prey
, neue Y
For example: int fun(int(vard) char (var2
\
a
Not necessary to mention
TMUBasics
eal
Ne a Raoul ne culos
Function definition consists of block of code which is capable of performing
some specific task.
eee d
int add(int a, int b)
{
eT
rT ee
iano tLHow FUNCTION WORKS?
2p ASSN IR,
al euro ey
( ) aces ae Onn
int main() prototype of a function.
Recall: There is no need to
5 PL f
sum = add(m, n); Carollo helm
Pee ees rid %d”, sum) ; (rol k Es
}
Tarr Cua eal)
{
}
ectHow FUNCTION WORKS?
ee
PT aey GR eo
‘ y This is the way how you define a
int main() Sem
i ae Gl = oh Ga Eh GOR NOTE: it is important to mention
Gi sattiih ws Pes ey ecteut teaalte
printf(“sum is %d”, sum); eee
hy l
int add(int a, int b)
rf ’
}
return (a +b);WHAT IS THE DIFFERENCE BETWEEN AN ARGUMENT
AND A PARAMETER?
elon Ce Xoo AR Xe ale cole eRe Ueno Rao ol
ERC a he Smut enema eo arma eos
NOTE: Parameter is also called as Formal Parameter and Argument is also
called as Actual Parameter.
.WHAT IS THE DIFFERENCE BETWEEN ARGUMENTS AND
PARAMETERS?
Para e Leet
.
int main()
f -
se eee
EIT ee te(ol Ca
Prete d eT ee eT
Parr eae eet)
{
}
oC
eure ae vase
Cees
ela ee ee a oa
LelaRECALL
jn
Reem Meee seek h rae
ee eee Cem uc ck tas
Er Cae ae GUT eta)
rd Z a \
V,
N
Y, return (a+b)3
Actual a ~~
-\
Formal
aL a Ld
Le 1ar- LaLa}CALL BY VALUE
—
POC Setar Mere lie AOC ee Mcceten!
Raat R nek oka parameters store values in Celie ree eay
int x = 10, y = 20; int fun(int x, int y)
fun(x, y)3 * {
printf (“x = %d, y = %d”, x, y)5 eos
y = 10;
(ohare
*CALL BY REFERENCE
field
int x = 10, y = 20; int fun(int *ptri, int *ptr2)
Aire ce
printf(“x = %d, y = %d”, x, y)3 le ead
*ptr2 = 10;
Output: x = 20, y
bd td
ya) ay
rT)Consider the function func shown below:
int func(int num) apes 435 = 110110011
{ pEby Lt
int count = @; Cae
while(num) 001101100
if
orien
Cress
MTT TE)
return(count) ; og
Si
The value returned by func(435) is
CNPC]The output of the following C program is:
void f1(int a, int b)
a
Frac
a ae
}
void f2(int *a, int *b)
{
ar
c = *a; *a = *b; *b = c;
int main()
{ I
int a=4, b=5, c=6;
f1(a, b);
£2(&b, &c);
Piss Gore
facet a
B
Vee aConsider the following C program:
int fun()
{
static int num = 16;
isa S d
}
int main()
T
for(fun(); fun(); fun())
printf("%d ", fun());
Er
}
What is the output of the C
fla) ae NP we] a a
a) Infinite loop
b) 1310741
c) 1411852
d) 1512852Basics
In C, functions are global by default.
This means if we want to access the function outside from the
file where it is declared, we can access it easily.
NMA RN oR ma Role MUN UR Rae Kolt a
function static by simply putting a keyword static in front of the
a etolaySCORE Rca Omens cca snc cenek cen
Secu lal canta eae Obscene
Ww Two operations: push and pop.
main()
i | -—
Troe Cd
es
TOL —
fun2() { fun3(); }
fun3() {_ return; } mea
Primer crn eRe M tesKel Kel (tae
lott elyDEFINITION
ene
Recursion is a process in which a function calls itself directly or indirectly.
For example
int fun()
{ ’
fun();eM DET Sai eee a eit ara accu eects
1. Locals of the callee
PA caNe (ook Roh ee) (ig
lola Komal ee 19
cred
int main()
U
aed
}
int funi(int a)
af
cua) STACK (or
areey rene
uh Poa Position which means that
iusWy SCOPING?
dahl SIS e INISe
Scoping is necessary if you want to reuse
variable names
Crear led
PUL ana}
int a
a
int fun2()
raryWHAT IS STATIC SCOPING?
SUAS Se
In Static scoping (or lexical scoping), definition of a variable is resolved by
PCa ier UMA Cioni cutee haere
containing block and so on.
cae A Re
int fun()
{
Pa nea y Output: 4
if
That
eye
printf(“%d”, c);eet e
cord
Oa lt
cro
Reece
eee ee teste
EEridRae Reno
Saye to
aie
int main()
{
int a = 10;
a = funl(a);
printf (“%d”, a);
i
int funi(int b)
{
b = b+10;
eel
eet
STATIC SCOPING EXAMPLE
int fun2(int b)
{
int c;
reas
Praia
(rts Tad
5
B
ean)
CeresWHAT IS DYNAMIC SCOPING?
VAIS ANIS SE
Varela aM CUM eS ecceculare MLR liaT)
block and if not found, then searching its calling function and if still not found then
the function which called that calling function will be searched and so on.
.DYNAmic SCOPING EXAMPLE
Trae TEPRe TL OF
Trae TUPLE TL OF
int a = 5;
int main() int fun2(int b)
{ {
par ee cane
a = funl(a); reas
print#(“%d”, a); eat an
oi
int funi(int b)
it
Ey
Cee 5
eee TrlC
oat
reece
CeresAAAS rd
Lue URoRCO Mac T xcisuieal cd
is paneer g PTAs 0)
printf ( 7] a) 24
b) 31
int funa() { a)
ec) 3G
With dynamic scoping
a) 24
1)
Ca)
d) 3 4
fun2()3IMPORTANT TAKEAWAYS
In most of the programming languages, static scoping is
followed instead of dynamic scoping
ene Mee anny Conese ere eee iuatcle)
statically scoped
Some languages, including SNOBOL, APL, Lisp etc. are
evan cost)
eso Mee escl aun ules tess aro)
programmatically, how dynamic scoping works in C.
’What will be the output of the following pseudocode when parameters are passed
by reference and dynamic scoping is assumed?
Pra void main() { Eye
rst Cone en aa
x=x*a; } ome
eine aes
} .
void m(y) { [GATE 2016]
a=1;
a=y- a;
LICE
cD esConsider the program below in a hypothetical programming language which
allows global variables and a choice of static or dynamic scoping.
Tara are et CE Succ Rta Caer otae Sar
ay URLS ULO) EC SCPC es ec ECs
i Bees Ur eee ae)
racy
call F(); @) Ts 10
By (B) Oy 10
«c) rT Pry
procedure f() (D) x = 20, 20 [GATE IT 2007]
i
Tae earl
SeurOh
}
procedure g()
ce
}
SuaraPROGRAM TO DEMONSTRATE RECURSION
a
int fun( 1 )
my
cad rat)
feral Tae
Es
return 1 + fun( n-1 );
y
int main() {
AT ry
printf (“%d”, fun(n));
Eran
returning back to the collar and who is
the collar of 111
fun(1)
fun(2)
fun(3)
main()DEMONSTRATING RECURSION: METHOD 2
eS
int fun(int n) fun(3)
Fan) oF
aed return 1 + fun(2)
else |
Sete meee Tee)
M return 1 + fun(1)
int main() { |
ct ee
Ben Coe TONS rt ne
return 0;
this particular place you just simply
te ORCA UE-UCel lL 8DEMONSTRATING RECURSION: METHOD 2
eS
int fun(int n) 3
Fons) o
ec ae
else l
eel aie eT Gee)
ui ical ed
int main() { |
ct ea
Pree MTG dar ah
iia
5 ve Output: 3
need to follow this sequence and when
Rotate)Basic STRUCTURE
ee
Ceeetece @
F fo
Se ae)File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Settings _Hely
PESO e VHRR ARM soa) Or ac 7<1 oom
ie o/h BRR)
1 | finclude
|O Workspace 2
3 | int fact (int n)
4 jar
5 if(n == 1)
6 return 1;
7 else o
8 return n*fact (n-1);
9h)
10
a1 | int main()
12 far
13 int nj
14 print£("Enter the number: ");
15 scanf("d", &n);
16 print£("Factorial of a number td is $d", n, fact (n))7
a7 |)
18ney
@ Divide the problem into smaller sub-problems.
fe WRatehe r lad C)
Fact(1) = 1
Fact(2) =2*1 = 2 * Fact(1)
Fact(3) =3*2*41 = 3 * Fact(2)
Fact(4) =4*3%*2*1 = 4 * Fact(3)
Fact(n) = n * Fact(n-1)
+
i eeeRR MCs acs
>) Specify the base condition to stop the recursion.
fe WRatehe lad)
Fact(1) =
Fact(2) = 2*1 = 2 * Fact(1)
Fact(3) = 3*2*41 = 3 * Fact(2)
Fact(4) =4*3%*2*1 = 4 * Fact(3)
Base condition is the one which doesn't require to call the
same function again and it helps in stopping the recursion.
el ee edoS lo) ig-vei mx =tal]eci[e)a) Sec
PS Mee n exe eRe lic am elmo] RU oa aero Ne (Tea
Sete Molde eam cco elo)
fun()y{
TN all
fun();
alsIndirect recursion
A function (let say fun) is called indirect recursive if it calls another
function (let say fun2) and then fun2 calls fun directly or indirectly.
Structure of Indirect recursion:
fun() { S funy) {
Tol mal els
fun2()3 ALO Fs
.
Tol ale elConsider the following recursive C function:
void get(int n) {
Rezarse mac
aCe
Perey
Peseta
aa
If get(6) function is being called in main0 then how many times will the get0 function
rman lsicieaccutiaehrohiraarela Oks
(A) 15 (8) 25
(©) 35 (0) 45
[GATE 2015 - 2 Marks]
.
BNvoid get(int n) {
Fears sa
get(n-1);
get(n-3);
PO ae
get(®)
get (6)
get(5) get(3) +6 calls
get(4) get(2) +4 calls
ace) Merce) ene
get(2) get(@)
get(1) get(-1)
get (-2) All vere Be
’Determine, how many number of times the star will be printed on the screen:
void fun1(int n) aed
it
ee y fun1(3)
if (n > 1) for(i-03 i<3; i++)
funl(n-1); SOC she)
for (i = @; i 1) count(n-1);
sie, RD
main()
tT DFWhat will be the output of the following C program?
void count(int n) ED}
if LD)
static int d = 1; ct
printf("%d ", n); d)
printf("%d ", d);
ren
PCM O Cree
printf("%d ", d); Peta en
main() come)
count (3)
ote
TD FyWHAT is DATA STRUCTURE?
Deen Ah enc mene ae ht Eire atel- ick
Ie Merete erat Coeur kre ak Chat Celi
Vr tee
For Example: Array is a data structure which you can visualize as follows:
w
Imagine an array as a large chunk of memory divided into smaller block of
memory and each block is capable of storing a data value of some type.int arr[10] = {1, ®, @, @, ®, 2, 3, ®, @, @}5
We want:
- in position 0
- 2inpositionS
- 3inposition6
int arr[1e] = {[@] = eae
ay
\ Marre) coda oLloM Reel so Kelclle sleek
initialization.
And each number in the square brackets is said to be a
designator.FINALLY, NO ONE CAN STOP YOU FROM DOING THIS:
int a[] = {1, 7, 5, [5] = 90, 6, [8] = 4};
Ea PECRT erence Cue UN RaaneT Leak si
Cee enn
related
vee
oie eenWB untied. Codelocks 1601
Fle Edit View Search Project Build Debug Fortran wiSmith Tools Tools+ Plugins DowyBlocks Settings Help
[gGBM\e>/sealagiorson
1 finchude-
2
3 aint main’) {
oP ee seen tior = cons
s | iw
€ | prinee "enter the nunbe
a | Beang(eas, ans
3 ||
BE| tne remy
10 4 wnttettbo)
a fem = W410)
2 Eg(scentrem) == 1)
B break
rr confer
13 we wie
ze}
rf
ie | sea 0)
3 rine ("Yes")
20 | aise
2 ee)
22 | return oF
2 |)rao Melted size of whole array
Pee ee Soar
Brom y ae eas
wv sizeof (name_of_arr)/sizeof(name_of_arr[@])DEFINITION AND SYNTAX
Multidimensional arrays can be defined as an array of arrays
General form of declaring N-dimensional array is as follows:
data_type name_of_array[size1][size2]...[sizeN];
For example:
Th aes o A SCL Tae] a
them in details
eure aeclaa)
ray
race Aa es a Pe
BNBSUS Cea OM ES
1g Mo Rg (MAL aa Loe)
Puro (New Program)
pra
peste ee Cue Cae Te
scanf("%d", &n);
i
Tas
Pe rsa @ ese bana
for(i=®; icn; i++)
fi N VARIABLE LENGTH ARRAY
scanf("%d ", &a[i]); SI
y
printf("Elements in reverse order are: \n");
for(isn-1; i=; i--)
7
y
aT USS eor- Tne cello)
length at the time of execution.
printf("%d “, a[i]);POINTS TO BE NOTED
Variable length arrays cannot have static storage duration.
Variable Length Array does not have the initializer.
It is not the case that you are specifying
the length at the time of executionIMPORTANT POINTS
Sg Pointer is a special variable that is capable of storing some address.
p
ad ECA eae huCKiGa aCioeaace|
a
Pp aI P
In the next lecture we'll start with how to declare pointers.int x = 5;
ST ra a
ptr = &;
address of
operator
Address of x is 1000. With the help of this statement
we can assign this address to this pointer variable.aera
BU al ard is equivalent to
ptr = &;
Therefore we can write these
two lines in one single line.Neltetake crear are cae een eg nk Cuca ue
ever bree Ae erage ema teeta sey Ce
3
int j 5
Pla
ptr
printé(“%d”, *ptr);
VALUE OF
rere
MECC RU MeN Ril VoePerce ie SRea CRN tat ead eich any ac
Peecntacs
ap are
int *ptr = ax;
that is 1000 and with the help of this dereference
operator or indirection operator or value of operator,ON] =a Ye) sia
Assigning valve to an uninitialized pointer is
dangerous.
pa .
Sees
ras
rent}
Usually,
Se ea)
ores Lg
Perna
eee
illegal memory
ee
2)
We the users have no rights to access the location.aC
int *p, *q3
oo)
q = Ps
printf (“%d %d”, *p, *q);
| Ourpur: 10 10
Therefore output of this program
is 10 10. But please note thatNote: q = p is not same as *q = *p
i = 10;
il
a
Ps* minMax.c ><
i
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
finclude
void minMax(int arr[], int len, int *min, int *max)
{
‘min
‘max = arr(0]; ty
int
for(i=lr iclen; i++)
f
af(arr[i] > *max)
‘max = arr[i]?
ig(arr(i] < *min)
‘min = arr([i]?
main() (
int a[] = (23, 4, 21, 98, 97, 45, 32, 10, 123, 986, 50, 3, 4, Sz
int min, max;
int len = sizeof (a) /sizeof(a[0));
minMax(a, len, émin, émax);
printf ("Minimum value in the array is: $d and Maximum value is: $d", min, max);
return 0;
9int main()
{
int a[] = {1, 2, 3, 4, 5}3
an ees ead Este CCID
int *mid = findMid(a, n);
printf("%d", *mid);
Re eH
}
int *findMid(int a[], int n)
{
frie ane CTP caddadl 3)
} a
Returning the address is
equivalent to returning the pointer.WorD OF CAUTION
ee
LNTeNlm CUO COM CALC a Ral -Xo(0(0 lft) Ke i Mele Ne Rela Le) cd
For example:
int *fun() Ter eee as teed
R Coan recs MN LSet
Tra es Se
ae
}
int main() {
int *p = fun();
printfg'%d", *p);
Therefore it is important for us to understand that we should
Ware Cele Ceol ee eel CL eR ZL)DEFINITION:
Se ere OM Lenk ue Koakeuteleraet re
within double quotes.
For EXAMPLE:
Se ea ice Dre eRe eo
meloeTo Rai
.
Italian it is char with toothy these are
all string literals because they
“Hallo zusammen”%s is a placeholder Double quotes are important.
int main() {
esta Gee CUBR 1D
iv
int main() {
printf("%s", ‘Hello everyone!?);
return 0; ‘
string literal so this one is correct
butCONTINUING STRING LITERALS
printf("%s", “You have to dream before your dreams can come true.
--A.P.J. Abdul Kalam");
Error
Ue aR Ue SU eon
SO PRCT COED
No Error
Leo an re
Ree ettecl cesetal
“Earth” is a string literal
Pieri On SU lame nC eae ase Rea
pe aaa Bie
But, what we are actually passing to the printf/scanf?
Let’s understand how string literals are stored?
+Set ae reser ont eee eee fata
Ueber h tt)
LC OL
Sat eae 4
Total 6 bytes of read-only
memory is allocated to the
PUN estas eRe De trate
‘\@’ character must not be confused with ‘@’ character
Both have different ASCII codes. ‘\@’ has the code @ while
*@’ has the code 48.
.In C, Compiler treats a string literal as a pointer to the first character.
= u N
+.
BCC)
So to the printf or scanf, we are passing a pointer to the first character of a
STi M tke
Both printf and scanf functions expects a character pointer (char *) as an
Preteata
printf (“Earth”) ;
Passing “Earth” is equivalent to passing the pointer to letter ‘E’