most important faq in c
TRANSCRIPT
![Page 1: Most Important Faq in c](https://reader038.vdocuments.us/reader038/viewer/2022100505/5695cf5f1a28ab9b028dcc58/html5/thumbnails/1.jpg)
7/23/2019 Most Important Faq in c
http://slidepdf.com/reader/full/most-important-faq-in-c 1/5
MOST IMPORTANT FAQ in C
1. Why would you use a structure
structure is to declare di!!erent data ty"es at a ti#e$ where as a arra y is a %rou" o! relateddata ty"es. the #ain di!!erence &'w structure ( union is in #e#ory re)uire#ent. In
structure s #e#ory is su# o! all the dataty"es declared in it$ where as in union #e#ory is
the one which holds the hi%hest &ytes. struct* int i+!loat %+,+here #e#ory re) is -/0
union*int i+!loat %+,+ here #e#ory re) is only / &ytes
-. 2i!!erence &etween class and structure
There is no #uch di!!erence &etween a class and a structure and the sli%ht
di!!erence is the de!ault sco"e o! class is "ri3ate where as the de!ault sco"e o! structure is%lo&al.
Class is a user de!ined data ty"e in o&4ect oriented "aradi%#. It consists o! &oth
data and code 5#ethods6.where as structure is also a user de!ined data ty"e &ut consists o!
only data #e#&ers. Not #ethods.Structures allow or%ani7ation o! data whereas class allows &oth or%ani7ation o!
data and data security.
8. whats is structure "addin% Say a %i3en structure
Struct*
int a+
char c+
!loat d+,
the si7e o! structure is 9 here. you can control "addin% usin% "ra%#a "ac: why you need "addin% . Ans is you need to ali%n your ;AR accordin% to word &oundary$ "addin% will#a:e your "ro%ra#ne wor: !ast $ I! you don<t ha3e "addin% $ use o! a&o3e struct will %i3e
&us error on so#e #achine. To understand that we need %o throu%h #ore docu#ent on
this
/. what is the di!!erence &etween #ethods and !unction
The #a4or di!!erence &etween #ethods and !unctions is that #ethods called &y there!erence 3aria&les called o&4ects where as the !unctions do not ha3in% any re!erence
3aria&les.
=. What is static #e#ory allocation and dyna#ic #e#ory allocation
Static #e#ory allocation> The co#"iler allocates the re)uired #e#ory s"ace !or adeclared 3aria&le. ?y usin% the address o! o"erator$ the reser3ed address is o&tained and
this address #ay &e assi%ned to a "ointer 3aria&le. Since #ost o! the declared 3aria&le
ha3e static #e#ory$ this way o! assi%nin% "ointer 3alue to a "ointer 3aria&le is :nown as
static #e#ory allocation. Me#ory is assi%ned durin% co#"ilation ti#e.
MAI@TO >
ra4esh:u#ar.#annuByahoo.co#
![Page 2: Most Important Faq in c](https://reader038.vdocuments.us/reader038/viewer/2022100505/5695cf5f1a28ab9b028dcc58/html5/thumbnails/2.jpg)
7/23/2019 Most Important Faq in c
http://slidepdf.com/reader/full/most-important-faq-in-c 2/5
MOST IMPORTANT FAQ in C
in static #e#ory allocation #e#ory is assi%ned at co#"ile ti#e!or
e int arrDE 0 *1$-$8,+ co#"iler will allocate #e#ory !or 8 inte%er at co#"ile.
2yna#ic #e#ory allocation> It uses !unctions such as #alloc5 6 or calloc5 6 to %et#e#ory dyna#ically.I! these !unctions are used to %et #e#ory dyna#ically and the
3alues returned &y these !unctions are assin%ned to "ointer 3aria&les$ such assi%n#ents
are :nown as dyna#ic #e#ory allocation.#e#ory is assined durin% run ti#e.whil in dyna#ic #e#ory allocation #e#ory is assi%ned at run ti#e.
#alloc5-G6 co#"iler will si#"ly see its a !unction and is ar%u#ent. while the
#e#ory will &e allocated at run ti#e.
. What is indirection
I! you declare a 3aria&le$ its na#e is a direct re!erence to its 3alue. I! you ha3e a "ointer to a 3aria&le$ or any other o&4ect in #e#ory$ you ha3e an indirect re!erence to its
3alue.
9. ?etween a lon% "ointer and a char "ointer which one consu#es #ore #e#ory
16 As We All Hnow That Pointer56 Jolds AddressK-6 Address is Always INTLR 5-?ytes6
86 Jence$ -?ytes Are Allocated For Any 2ata Ty"esK
?oth will consu#e sa#e a#ount o! #e#ory. why &ecause they #eans lon% or char "ointer always stores the address o! the character or lon% inte%er.
. Is it &etter to use #alloc56 or calloc56
?oth the #alloc56 and the calloc56 !unctions are used to allocate dyna#ic #e#ory. ach
o"erates sli%htly di!!erent !ro# the other. #alloc56 ta:es a si7e and returns a "ointer to a
chun: o! #e#ory at least that &i%>3oid #alloc5 si7et si7e 6+
calloc56 ta:es a nu#&er o! ele#ents$ and the si7e o! each$ and returns a "ointer to a chun:
o! #e#ory at least &i% enou%h to hold the# all>3oid calloc5 si7et nu#le#ents$ si7et si7eO!le#ent 6+
There<s one #a4or di!!erence and one #inor di!!erence &etween the two !unctions. The
#a4or di!!erence is that #alloc56 doesn<t initiali7e the allocated #e#ory. The !irst ti#e
#alloc56 %i3es you a "articular chun: o! #e#ory$ the #e#ory #i%ht &e !ull o! 7eros. I! #e#ory has &een allocated$ !reed$ and reallocated$ it "ro&a&ly has whate3er 4un: was le!t
in it. That #eans$ un!ortunately$ that a "ro%ra# #i%ht run in si#"le cases 5when #e#ory
is ne3er reallocated6 &ut &rea: when used harder 5and when #e#ory is reused6.calloc56 !ills the allocated #e#ory with all 7ero &its. That #eans that anythin%
there you<re %oin% to use as a char or an int o! any len%th$ si%ned or unsi%ned$ is
%uaranteed to &e 7ero. Anythin% you<re %oin% to use as a "ointer is set to all 7ero &its.That<s usually a null "ointer$ &ut it<s not %uaranteed.Anythin% you<re %oin% to use as a
!loat or dou&le is set to all 7ero &its+ that<s a !loatin%"oint 7ero on so#e ty"es o!
#achines$ &ut not on all.
MAI@TO >
ra4esh:u#ar.#annuByahoo.co#
![Page 3: Most Important Faq in c](https://reader038.vdocuments.us/reader038/viewer/2022100505/5695cf5f1a28ab9b028dcc58/html5/thumbnails/3.jpg)
7/23/2019 Most Important Faq in c
http://slidepdf.com/reader/full/most-important-faq-in-c 3/5
MOST IMPORTANT FAQ in C
The #inor di!!erence &etween the two is that calloc56 returns an array o! o&4ects+
#alloc56 returns one o&4ect. So#e "eo"le use calloc56 to #a:e clear that they want an
array.
. What is a const "ointer
There are cases when you need to de!ine a constant "ointer to a 3aria&le'o&4ect+
!or instance$ when ta:in% a !unction address$ or when you want to "rotect a "ointer !ro#unintended #odi!ications such as assi%n#ent o! new address$ "ointer arith#etic$ etc. In
!act$ an o&4ect<s this is a const"ointer. A constant "ointer is declared>
1G. What is the di!!erence &etween o3erloadin% and o3erridin%
O3erloadin%> &y o3erloadin% a !unction we can use the sa#e !unction !or "er!or#in%
si#ilar o"erations on di!!erent data ty"es.
O3erridin%> &y o3erridin% a !unction we can use the sa#e !unction na#e and "ara#eters
&ut "er!or# di!!erent o"erations on data ty"es.
1. O3erloadin%> It occurs when two #ethods &elon% to sa#e class.
-. O3erridin% occurs when there is inheritance.8. In o3erridin% the si%nature #ust &e sa#e &ut here 4ust the code chan%es.
/. O3erloaded #ethods should di!!er in any one o! the !ollowin% as"ects
a. Nu#&er o! ar%u#ents$
&. Ty"e o! ar%u#ents$c. Order o! ar%u#ents.
11. What is a null "ointer
When re!errin% to co#"uter #e#ory$ a null "ointer is a co##and used to direct a
so!tware "ro%ra# or o"eratin% syste# to an e#"ty location in the co#"uter #e#ory.Co##only$ the null "ointer is used to denote the end o! a #e#ory search or "rocessin%
e3ent. In co#"uter "ro%ra##in%$ a null "ointer is a "ointer that does not "oint to any
o&4ects or !unction. In the "ro%ra##in% lan%ua%e c$ N@@ is an a3aila&le co##and that
can &e used.
1-. "lain the need !or ;irtual 2estructor.
I! &oth the &ase class and deri3ed class can ha3e the constructors$ i! the deri3ed
class is to call the constructor o! its own usin% its o&4ect$ it can &ut when it calls its
destructor$ the co#"iler calls the destructor o! &ase class. That<s why the 3irtualdestructor conce"t is coined. ?ut there are no 3irtual constructors.
18. Jow #any le3els o! "ointers can you ha3e
MAI@TO >
ra4esh:u#ar.#annuByahoo.co#
![Page 4: Most Important Faq in c](https://reader038.vdocuments.us/reader038/viewer/2022100505/5695cf5f1a28ab9b028dcc58/html5/thumbnails/4.jpg)
7/23/2019 Most Important Faq in c
http://slidepdf.com/reader/full/most-important-faq-in-c 4/5
MOST IMPORTANT FAQ in C
The answer de"ends on what you #ean &y le3els o! "ointers. I! you #ean Jow #any
le3els o! indirection can you ha3e in a sin%le declaration the answer is At least 1-.
int i 0 G+int i"G1 0 ( i+
int i"G- 0 ( i"G1+
int i"G8 0 ( i"G-+int i"G/ 0 ( i"G8+
int i"G= 0 ( i"G/+
int i"G 0 ( i"G=+int i"G9 0 ( i"G+
int i"G 0 ( i"G9+
int i"G 0 ( i"G+
int i"1G 0 ( i"G+int i"11 0 ( i"1G+
int i"1- 0 ( i"11+
i"1- 0 1+ ' i 0 1 '
The ANSI C standard says all co#"ilers #ust handle at least 1- le3els. our co#"iler #i%ht su""ort #ore.
1/. what is the di!!erence &etween #alloc and calloc
MALLOC ( )
1. #alloc ta:es only the si7e o! the #e#ory &loc: to &e allocated as in"ut "ara#eter.
-. #alloc allocates #e#ory as a sin%le conti%uous &loc:.
8. i! a sin%le conti%uous &loc: cannot &e allocated then #alloc would !ail.CALLOC ( )
1. calloc ta:es two "ara#eters> the nu#&er o! #e#ory &loc:s and the si7e o! each &loc:
o! #e#ory
-. calloc allocates #e#ory which #ay'#ay not &e conti%uous.8. all the #e#ory &loc:s are initiali7ed to G.
/. it !ollows !ro# "oint - that$ calloc will not !ail i! #e#ory can &eallocated in non
conti%uous &loc:s when a sin%le conti%uous &loc:cannot &e allocated.
1=. Jow do you use a "ointer to a !unction
The hardest "art a&out usin% a "ointerto!unction is declarin% it. Consider an ea#"le.ou want to create a "ointer$ "!$ that "oints to the strc#"56 !unction.
The strc#"56 !unction is declared in this way>
int strc#"5const char $ const char 6To set u" "! to "oint to the strc#"56 !unction$ you want a declaration that loo:s 4ust li:e
the strc#"56 !unction<s declaration$ &ut that has "! rather than strc#">
int 5"!65 const char $ const char 6+A!ter you<3e %otten the declaration o! "!$ you can Uinclude Vstrin%.h and assi%n the
address o! strc#"56 to "!> "! 0 strc#"+
MAI@TO >
ra4esh:u#ar.#annuByahoo.co#
![Page 5: Most Important Faq in c](https://reader038.vdocuments.us/reader038/viewer/2022100505/5695cf5f1a28ab9b028dcc58/html5/thumbnails/5.jpg)
7/23/2019 Most Important Faq in c
http://slidepdf.com/reader/full/most-important-faq-in-c 5/5
MOST IMPORTANT FAQ in C
1 what is the di!!erence &etween !unction "ointer an "ointer to !unction
we can create a "ointer to !unction. so we can call the !unction with that "ointer.e%
let us ta:e two !unctions
3oid su#5int a$int &6+
3oid su&5int a$int &6+
we can create "ointer to these !unctions
3oid 5"65int$int6+
"0su# "58$-6%i3es the su# o! two nu#&ers
"0su& "58$-6 %i3es the di!!erence o! two nu#&ers
MAI@TO >
ra4esh:u#ar.#annuByahoo.co#