Sunteți pe pagina 1din 21

C Questions

1. 2. 3. 4. 5. 6. . ". $. 1'. 11. 12. 13. 14. 15. 16. 1 . 1". 1$. 2'. 21. 22. 23. 24. 25. 26. 2 . 2". 2$. 3'. 31. 32. 33. 34. 35. What does static variable mean? What is a pointer? What are the uses of a pointer? What is a structure? What is a union? What are the differences between structures and union? What are the differences between structures and arra!s? #n header files whether functions are declared or defined? What are the differences between malloc %& and calloc %&? What are macros? What are its advanta(es and disadvanta(es? )ifference between pass b! reference and pass b! What is static identifier? Where is the auto variables stored? Where does (lobal* static* and local* re(ister )ifference between arra!s and lin+ed list? What are enumerations? )escribe about stora(e allocation and scope of What are re(ister variables? What are the advanta(es What is the use of t!pedef? Can we specif! variable field width in a scanf%& ,ut of f(ets%& and (ets%& which function is safe to use and wh!? )ifference between strdup and strcp!? What is recursion? )ifferentiate between a for loop and a while loop? What are it uses? What is stora(e class. What are the different stora(e classes in C? What the advanta(es of usin( -nions? What is the difference between .trin(s and /rra!s? What is a far pointer? where we use it? What is a hu(e pointer? What is a normali0ed pointer *how do we normali0e a pointer? What is near pointer. #n C* wh! is the void pointer useful? When would !ou use it? What is a 1-22 3ointer? Whether it is same as an uninitiali0ed /re pointers inte(er ? What does the error 41ull 3ointer /ssi(nment4 means and what

pointer?

causes this error?

36. 3 . 3". 3$. 4'. 41. 42. 43. 44. 45. 46. 4 . 4". 4$. 5'. 51. 52. 53. 54. 55. 56. 5 . 5". 5$. 6'. 61. 62. 63. 64. 65. 66. one. 6 . 6".

What is (eneric pointer in C? /re the e5pressions arr and 6arr same for an arra! of inte(ers? 7ow pointer variables are initiali0ed ? What is static memor! allocation ? What is d!namic memor! allocation? What is the purpose of realloc ? What is pointer to a pointer. What is an arra! of pointers ? )ifference between lin+er and lin+a(e ? #s it possible to have ne(ative inde5 in an arra!? Wh! is it necessar! to (ive the si0e of an arra! in an arra! What modular pro(rammin( ? What is a function ? What is an ar(ument ? What are built in functions ? )ifference between formal ar(ument and actual ar(ument ? #s it possible to have more than one main%& function in a C What is the difference between an enumeration and a set of pre8 7ow are .tructure passin( and returnin( implemented b! the What is the similarit! between a .tructure* -nion and enumeration? Can a .tructure contain a 3ointer to itself? 7ow can we read:write .tructures from:to data files? Write a pro(ram which emplo!s ;ecursion ? Write a pro(ram which uses Command 2ine /r(uments? )ifference between arra! and pointer ? What do the 4c4 and 4v4 in ar(c and ar(v stand for? What are C to+ens ? What are C identifiers? )ifference between s!nta5 <s. lo(ical error? What is preincrement and post increment ? Write a pro(ram to interchan(e 2 variables without usin( the third What is the ma5imum combined len(th of command line ar(uments What are bit fields? What is the use of bit fields in a .tructure

declaration ?

pro(ram ? processor 9 defines? complier?

includin( the space between ad=acent ar(uments?

declaration? 6$. '. 1. 2. 3. 4. 5. 6. . ". $. "1. "2. "3. "4. "5. "6. " . "". "$. $'. $1. $2. $3. $4. $5. $6. $ . $". $$. What is a preprocessor* What are the advanta(es of preprocessor ? What are the facilities provided b! preprocessor ? What are the two forms of 9include directive ? 7ow would !ou use the functions randomi0e%& and random%&? What do the functions atoi%&* itoa%& and (cvt%& do? 7ow would !ou use the functions fsee+%&* freed%&* fwrite%& and What is the difference between the functions memmove%& and What is a file ? What are the t!pes of file? What is a stream ? What is meant b! file openin( ? What is a file pointer ? 7ow is fopen%&used ? 7ow is a file closed ? What is a random access file ? What is the purpose of ftell ? What is the purpose of rewind%& ? )ifference between a arra! name and a pointer variable ? ;epresent a two8dimensional arra! usin( pointer ? )ifference between an arra! of pointers and a pointer to an arra! ? Can we use an! name in place of ar(v and ar(c as command line What are the pointer declarations used in C? )ifferentiate between a constant pointer and pointer to a #s the allocated space within a function automaticall! deallocated )iscuss on pointer arithmetic? What are the invalid pointer arithmetic ? What are the advanta(es of usin( arra! of pointers to strin( /re the e5pressions >ptr ?? and ?? >ptr same ? What would be the e@uivalent pointer e5pression foe referrin( the /re the variables ar(c and ar(v are alwa!s local to main ?

ftell%&? memcp!%&?

ar(uments ?

constant ? when the function returns?

instead of an arra! of strin(s?

same element as aApBA@BArBAsB ? 1''. Can main %& be called recursivel!? 1'1. Can we initiali0e unions?

1'2. WhatCs the difference between these two declarations? 1'3. Wh! doesn4t this codeD aAiB E i??F wor+? 1'4. Wh! doesn4t struct 5 G ... HF5 thestructF wor+? 1'5. Wh! can4t we compare structures? 1'6. 7ow are structure passin( and returnin( implemented?

/nswers 1.What does static variable mean? /nsD .tatic variables are the variables which retain their values between the function calls. Ihe! are initiali0ed onl! once Iheir scope is within the function in which the! are defined. 2.What is a pointer? /nsD 3ointers are variables which stores the address of another variable. Ihat variable ma! be a scalar %includin( another pointer&* or an a((re(ate %arra! or structure&. Ihe pointed8to ob=ect ma! be part of a lar(er ob=ect* such as a field of a structure or an element in an arra!. 3.What are the uses of a pointer? /nsD 3ointer is used in the followin( cases i& #t is used to access arra! elements ii& #t is used for d!namic memor! allocation. iii& #t is used in Call b! reference iv& #t is used in data structures li+e trees* (raph* lin+ed list etc. 4.What is a structure? /nsD .tructure constitutes a super data t!pe which represents several different data t!pes in a sin(le unit. / structure can be initiali0ed if it is static or (lobal. 5.What is a union? /nsD -nion is a collection of hetero(eneous data t!pe but it uses efficient memor! utili0ation techni@ue b! allocatin( enou(h memor! to hold the lar(est member. 7ere a sin(le area of memor! contains values of different t!pes at different time. / union can never be initiali0ed. 6.What are the differences between structures and union? /nsD / structure variable contains each of the named members* and its

si0e is lar(e enou(h to hold all the members. .tructure elements are of same si0e. / union contains one of the named members at a (iven time and is lar(e enou(h to hold the lar(est member. -nion element can be of different si0es. .What are the differences between structures and arra!s? /nsD .tructure is a collection of hetero(eneous data t!pe but arra! is a collection of homo(eneous data t!pes. /rra! <s .tructure 18#t is a collection of data items of same data t!pe. 28#t has declaration onl! 38.Ihere is no +e!word. 48 arra! name represent the address of the startin( element. 18#t is a collection of data items of different data t!pe. 28 #t has declaration and definition 38 +e!word struct is used 48.tructure name is +nown as ta( it is the short hand notation of the declaration. ".#n header files whether functions are declared or defined? /nsD Junctions are declared within header file. Ihat is function protot!pes e5ist in a header file* not function bodies. Ihe! are defined in librar! %lib&. $.What are the differences between malloc %& and calloc %&? /nsD Kalloc Calloc 18Kalloc ta+es one ar(ument Kalloc%a&Fwhere a number of b!tes 28memor! allocated contains (arba(e values 18Calloc ta+es two ar(uments Calloc%b*c& where b no of ob=ect and c si0e of ob=ect28#t initiali0es the contains of bloc+ of memor! to 0eros Kalloc ta+es one ar(ument* memor! allocated contains (arba(e values. #t allocates conti(uous memor! locations. Calloc ta+es two ar(uments* memor! allocated contains all 0eros* and the memor! allocated is not conti(uous. 1'.What are macros? What are its advanta(es and disadvanta(es? /nsD Kacros are abbreviations for len(th! and fre@uentl! used statements. When a macro is called the entire code is substituted b! a sin(le line thou(h the macro definition is of several lines. Ihe advanta(es of macro is that it reduces the time ta+en for control transfer as in case of function. Ihe disadvanta(e of it is here the entire code is substituted so the pro(ram becomes len(th! if a macro is called

several times. 11.)ifference between pass b! reference and pass b! value? /nsD 3ass b! reference passes a pointer to the value. Ihis allows the callee to modif! the variable directl!. 3ass b! value (ives a cop! of the value to the callee. Ihis allows the callee to modif! the value without modif!in( the variable. %#n other words* the callee simpl! cannot modif! the variable* since it lac+s a reference to it.& 12.What is static identifier? /nsD / file8scope variable that is declared static is visible onl! to functions within that file. / function8scope or bloc+8scope variable that is declared as static is visible onl! within that scope. Jurthermore* static variables onl! have a sin(le instance. #n the case of function8 or bloc+8scope variables* this means that the variable is not LautomaticM and thus retains its value across function invocations. 13.Where is the auto variables stored? /nsD /uto variables can be stored an!where* so lon( as recursion wor+s. 3racticall!* the!Cre stored on the stac+. #t is not necessar! that alwa!s a stac+ e5ist. Nou could theoreticall! allocate function invocation records from the heap. 14.Where does (lobal* static* and local* re(ister variables* free memor! and C 3ro(ram instructions (et stored? /nsD OlobalD Wherever the lin+er puts them. I!picall! the LP.. se(mentM on man! platforms. .taticD /(ain* wherever the lin+er puts them. ,ften* the!Cre intermi5ed with the (lobals. Ihe onl! difference between (lobals and statics is whether the lin+er will resolve the s!mbols across compilation units. 2ocalD I!picall! on the stac+* unless the variable (ets re(ister allocated and never spills. ;e(isterD 1owada!s* these are e@uivalent to L2ocalM variables. Ihe! live on the stac+ unless the! (et re(ister8allocated. 15.)ifference between arra!s and lin+ed list? /nsD /n arra! is a repeated pattern of variables in conti(uous stora(e. / lin+ed list is a set of structures scattered throu(h memor!* held to(ether b! pointers in each element that point to the ne5t element. With an arra!* we can %on most architectures& move from

one element to the ne5t b! addin( a fi5ed constant to the inte(er value of the pointer. With a lin+ed list* there is a Lne5tM pointer in each structure which sa!s what element comes ne5t. 16.What are enumerations? /nsD Ihe! are a list of named inte(er8valued constants. Q5ampleD enum color G blac+ * oran(eE4* !ellow* (reen* blue* violet HFIhis declaration defines the s!mbols Lblac+R* Loran(eR* L!ellowR* etc. Io have the values L1*M L4*M L5*M S etc. Ihe difference between an enumeration and a macro is that the enum actuall! declares a t!pe* and therefore can be t!pe chec+ed. 1 .)escribe about stora(e allocation and scope of (lobal* e5tern* static* local and re(ister variables? /nsD Olobals have application8scope. Ihe!Cre available in an! compilation unit that includes an appropriate declaration %usuall! brou(ht from a header file&. Ihe!Cre stored wherever the lin+er puts them* usuall! a place called the LP.. se(ment.M Q5tern? Ihis is essentiall! L(lobal.M .taticD .tored the same place as (lobals* t!picall!* but onl! available to the compilation unit that contains them. #f the! are bloc+8scope (lobal* onl! available within that bloc+ and its sub bloc+s. 2ocalD .tored on the stac+* t!picall!. ,nl! available in that bloc+ and its sub bloc+s. %/lthou(h pointers to locals can be passed to functions invo+ed from within a scope where that local is valid.& ;e(isterD .ee tirade above on LlocalM vs. Lre(ister.M Ihe onl! difference is that the C compiler will not let !ou ta+e the address of somethin( !ouCve declared as Lre(ister.M 1".What are re(ister variables? What are the advanta(es of usin( re(ister variables? /nsD #f a variable is declared with a re(ister stora(e class* it is +nown as re(ister variable. Ihe re(ister variable is stored in the cpu re(ister instead of main memor!. Jre@uentl! used variables are declared as re(ister variable as itCs access time is faster. 1$.What is the use of t!pedef? /nsD Ihe t!pedef help in easier modification when the pro(rams are ported to another machine.

/ descriptive new name (iven to the e5istin( data t!pe ma! be easier to understand the code. 2'.Can we specif! variable field width in a scanf%& format strin(? #f possible how? /nsD /ll field widths are variable with scanf%&. Nou can specif! a ma5imum field width for a (iven field b! placin( an inte(er value between the TUC and the field t!pe specifier. %e.(. U64s&. .uch a specifier will still accept a narrower field width. Ihe one e5ception is U9c %where 9 is an inte(er&. Ihis reads QV/CI2N 9 characters* and it is the onl! wa! to specif! a fi5ed field width with scanf%&. 21.,ut of f(ets%& and (ets%& which function is safe to use and wh!? /nsD f(ets%& is safer than (ets%&* because we can specif! a ma5imum input len(th. 1either one is completel! safe* because the compiler canCt prove that pro(rammer wonCt overflow the buffer he pass to f(ets %&. 22.)ifference between strdup and strcp!? /nsD Poth cop! a strin(. strcp! wants a buffer to cop! into. strdup allocates a buffer usin( malloc%&. -nli+e strcp!%&* strdup%& is not specified b! /1.# . 23.What is recursion? /nsD / recursion function is one which call itself either directl! or indirectl! #t must halt at a definite point to avoid infinite recursion. 24.)ifferentiate between a for loop and a while loop? What are it uses? /nsD Jor e5ecutin( a set of statements fi5ed number of times we use for loop while when the number of iterations to be performed is not +nown in advance we use while loop. 25.What is stora(e class. What are the different stora(e classes in C? /nsD .tora(e class is an attribute that chan(es the behavior of a variable. #t controls the lifetime* scope and lin+a(e. Ihe stora(e classes in c are auto* re(ister* and e5tern* static* t!pedef. 26.What the advanta(es of usin( -nions? /nsD When the C compiler is allocatin( memor! for unions it will alwa!s reserve enou(h room for the lar(est member. 2 .What is the difference between .trin(s and /rra!s? /nsD .trin( is a se@uence of characters endin( with 1-22 .it can be

treated as a one dimensional arra! of characters terminated b! a 1-22 character. 2".What is a far pointer? where we use it? /nsD #n lar(e data model %compact* lar(e* hu(e& the address P'''"''' is acceptable because in these model all pointers to data are 32bits lon(. #f we use small data model %tin!* small* medium& the above address wonCt wor+ since in these model each pointer is 16bits lon(. #f we are wor+in( in a small data model and want to access the address P'''"''' then we use far pointer. Jar pointer is alwa!s treated as a 32bit pointer and contains a se(ment address and offset address both of 16bits each. Ihus the address is represented usin( se(ment D offset format P'''hD"'''h. Jor an! (iven memor! address there are man! possible far address se(ment D offset pair. Ihe se(ment re(ister contains the address where the se(ment be(ins and offset re(ister contains the offset of data:code from where se(ment be(ins. 2$.What is a hu(e pointer? /nsD 7u(e pointer is 32bit lon( containin( se(ment address and offset address. 7u(e pointers are normali0ed pointers so for an! (iven memor! address there is onl! one possible hu(e address se(ment D offset pair. 7u(e pointer arithmetic is doe with calls to special subroutines so its arithmetic slower than an! other pointers. 3'.What is a normali0ed pointer* how do we normali0e a pointer? /nsD #t is a 32bit pointer* which has as much of its value in the se(ment re(ister as possible. .ince a se(ment can start ever! 16b!tes so the offset will have a value from ' to J. for normali0ation convert the address into 2'bit address then use the 16bit for se(ment address and 4bit for the offset address. Oiven a pointer 5'')D $4' *we convert it to a 2'bitabsolute address 54$) *Which then normali0ed to 54$)D''' . 31.What is near pointer. /nsD / near pointer is 16 bits lon(. #t uses the current content of the C. %code se(ment& re(ister %if the pointer is pointin( to code& or current contents of ). %data se(ment& re(ister %if the pointer is pointin( to data& for the se(ment part* the offset part is stored in a 16 bit near pointer. -sin( near pointer limits the data:code to 64+b se(ment. 32.#n C* wh! is the void pointer useful? When would !ou use it?

/nsD Ihe void pointer is useful because it is a (eneric pointer that an! pointer can be cast into and bac+ a(ain without loss of information. 33.What is a 1-22 3ointer? Whether it is same as an uninitiali0ed pointer? /nsD 1ull pointer is a pointer which points to nothin( but uninitiali0ed pointer ma! point to an!where. 34./re pointers inte(er ? /nsD 1o* pointers are not inte(ers. / pointer is an address. #t is a positive number. 35.What does the error 41ull 3ointer /ssi(nment4 means and what causes this error? /nsD /s null pointer points to nothin( so accessin( a uninitiali0ed pointer or invalid location ma! cause an error. 36.What is (eneric pointer in C? /nsD #n C* void> acts as a (eneric pointer. When other pointer t!pes are assi(ned to (eneric pointer* conversions are applied automaticall! %implicit conversion&. 3 ./re the e5pressions arr and 6arr same for an arra! of inte(ers? /nsD Nes for arra! of inte(ers the! are same. 3".7ow pointer variables are initiali0ed ? /nsD 3ointer variables are initiali0ed b! one of the followin( wa!s. #. .tatic memor! allocation ##. )!namic memor! allocation 3$.What is static memor! allocation ? /nsD Compiler allocates memor! space for a declared variable. P! usin( the address of operator* the reserved address is obtained and this address is assi(ned to a pointer variable. Ihis wa! of assi(nin( pointer value to a pointer variable at compilation time is +nown as static memor! allocation. 4'.What is d!namic memor! allocation? /nsD / d!namic memor! allocation uses functions such as malloc%& or calloc%& to (et memor! d!namicall!. #f these functions are used to (et memor! d!namicall! and the values returned b! these function are assi(ned to pointer variables* such an wa! of allocatin( memor! at run time is +nown as d!namic memor! allocation. 41.What is the purpose of realloc ? /nsD #t increases or decreases the si0e of d!namicall! allocated arra!.

Ihe function realloc%ptr*n& uses two ar(uments. Ihe first ar(ument ptr is a pointer to a bloc+ of memor! for which the si0e is to be altered. the second ar(ument specifies the new si0e. Ihe si0e ma! be increased or decreased. #f sufficient space is not available to the old re(ion the function ma! create a new re(ion. 42.What is pointer to a pointer. /nsD #f a pointer variable points another pointer value. such a situation is +nown as a pointer to a pointer. Q5. int >p1*>>p2*vE1'F 31E6vF p2E6p1F 7ere p2 is a pointer to a pointer. 43.What is an arra! of pointers ? /nsD if the elements of an arra! are addresses* such an arra! is called an arra! of pointers. 44.)ifference between lin+er and lin+a(e ? /nsD 2in+er converts an ob=ect code into an e5ecutable code b! lin+in( to(ether the necessar! built in functions. Ihe form and place of declaration where the variable is declared in a pro(ram determine the lin+a(e of variable. 45.#s it possible to have ne(ative inde5 in an arra!? /nsD Nes it is possible to inde5 with ne(ative value provided there are data stored in these location. Qven if it is ille(al to refer to the elements that are out of arra! bounds* the compiler will not produce error because C has no chec+ on the bounds of an arra!. 46.Wh! is it necessar! to (ive the si0e of an arra! in an arra! declaration ? /nsD When an arra! is declared *the compiler allocates a base address and reserves enou(h space in memor! for all the elements of the arra!. Ihe si0e is re@uired to allocate the re@uired space and hence si0e must be mentioned. 4 .What modular pro(rammin( ? /nsD #f a pro(ram is lar(e* it is subdivided into a number of smaller pro(rams that are called modules or subpro(rams. #f a comple5 problem is solved usin( more modules* this approach is +nown as modular pro(rammin(. 4".What is a function ?

/nsD / lar(e pro(ram is subdivided into a number of smaller pro(rams or subpro(rams. Qach subpro(ram specifies one or more actions to be performed for the lar(er pro(ram. such sub pro(rams are called functions. 4$.What is an ar(ument ? /nsD /n ar(ument is an entit! used to pass data from the callin( to a called function. 5'.What are built in functions ? /nsD Ihe functions that are predefined and supplied alon( with the compiler are +nown as built8in functions. Ihe! are also +nown as librar! functions. 51.)ifference between formal ar(ument and actual ar(ument ? /nsD Jormal ar(uments are the ar(uments available in the function definition. Ihe! are preceded b! their own data t!pe. /ctual ar(uments are available in the function call. Ihese ar(uments are (iven as constants or variables or e5pressions to pass the values to the function. 52.#s it possible to have more than one main%& function in a C pro(ram ? /nsD Ihe function main%& can appear onl! once. Ihe pro(ram e5ecution starts from main. 53.What is the difference between an enumeration and a set of pre8 processor 9 defines? /nsD Ihere is hardl! an! difference between the two* e5cept that a 9define has a (lobal effect %throu(hout the file& whereas an enumeration can have an effect local to the bloc+ if desired. .ome advanta(es of enumeration are that the numeric values are automaticall! assi(ned whereas in 9define we have to e5plicitl! define them. / disadvanta(es is that we have no control over the si0e of enumeration variables. 54.7ow are .tructure passin( and returnin( implemented b! the complier? /nsD When structure are passed as ar(ument to functions* the entire structure is t!picall! pushed on the stac+. Io avoid this overhead man! pro(rammer often prefer to pass pointers to structure instead of actual structures. .tructures are often returned from functions in a location pointed to b! an e5tra* compiler8supported ThiddenC ar(ument to the function. 55.What is the similarit! between a .tructure* -nion and enumeration? /nsD /ll of them let the pro(rammer to define new data t!pe.

56.Can a .tructure contain a 3ointer to itself? /nsD Nes such structures are called self8referential structures. 5 .7ow can we read:write .tructures from:to data files? /nsD Io write out a structure we can use fwrite%& as Jwrite% 6e*si0eof%e&*1*fp&FWhere e is a structure variable. / correspondin( fread%& invocation can read the structure bac+ from file. ,n callin( fwrite%& it writes out si0eof%e& b!tes from the address 6e. )ata files written as memor! ima(es with fwrite%&* however *will not be portable* particularl! if the! contain floatin( point fields or 3ointers. Ihis is because memor! la!out of structures is machine and compiler dependent. Iherefore* structures written as memor! ima(es cannot necessaril! be read bac+ b! pro(rams runnin( on other machine* and this is the important concern if the data files !ouCre writin( will ever be interchan(ed between machines. 5".Write a pro(ram which emplo!s ;ecursion ? /nsD int fact%int n& G return n W 1 ? n > fact%n 8 1& D 1F H 5$.Write a pro(ram which uses Command 2ine /r(uments? /nsD 9includeXstdio.hW 9includeXconio.hW void main%int ar(c*char >ar(vAB& G int iF clrscr%&F for%iE'FiXar(cFi??& printf%RYnUdR*ar(vAiB&F H 6'.)ifference between arra! and pointer ? /nsD /rra! <s 3ointer 18 /rra! allocates space automaticall! 28 #t cannot be resi0ed 38 #t cannot be reassi(ned 48 si0eof %arra!name& (ives the number of b!tes occupied b! the arra!. 18Q5plicitl! assi(ned to point to an allocated space. 28#t can be si0ed usin( realloc%& 38pointer can be reassi(ned. 48si0eof%p& returns the number of b!tes used to store the pointer variable p. 61.what do the 4c4 and 4v4 in ar(c and ar(v stand for? /nsD Ihe c in ar(c %ar(ument count& stands for the number of command

line ar(ument the pro(ram is invo+ed with and v in ar(v %ar(ument vector& is a pointer to an arra! of character strin( that contain the ar(uments. 62.What are C to+ens ? /nsD Ihere are si5 classes of to+ensD identifier* +e!words* constants* strin( literals* operators and other separators. 63.What are C identifiers? /nsD Ihese are names (iven to various pro(rammin( element such as variables* function* arra!s. #t is a combination of letter* di(it and underscore. #t should be(in with letter. bac+space is not allowed. 64.)ifference between s!nta5 <s lo(ical error? /nsD .!nta5 Qrror <s 2o(ical Qrror 18Ihese involves validation of s!nta5 of lan(ua(e. 28compiler prints dia(nostic messa(e. 18lo(ical error are caused b! an incorrect al(orithm or b! a statement mist!ped in such a wa! that it doesnCt violet s!nta5 of lan(ua(e. 28difficult to find. 65.What is preincrement and postincrement ? /nsD ??n %pre increment& increments n before itCs value is used in an assi(nment operation or an! e5pression containin( it. n?? %post increment& does increment after the value of n is used. 66.Write a pro(ram to interchan(e 2 variables without usin( the third one. /nsD a ZE bF ie aEaZb b ZE aF ie bEbZaF a ZE b ie aEaZbF here the numbers are converted into binar! and then 5or operation is performed. Nou +now* !ouCre =ust as+in( Lhave !ou seen this overl! clever tric+ thatCs not worth appl!in( on modern architectures and onl! reall! applies to inte(er variables?M 6 .What is the ma5imum combined len(th of command line ar(uments includin( the space between ad=acent ar(uments? /nsD #t depends on the operatin( s!stem. 6".What are bit fields? What is the use of bit fields in a .tructure

declaration? /nsD / bit field is a set of ad=acent bits within a sin(le implementation based stora(e unit that we will call a RwordR. Ihe s!nta5 of field definition and access is based on structure. struct G unsi(ned int + D1F unsi(ned int l D1F unsi(ned int m D1F Hfla(sF the number followin( the colon represents the field width in bits. Jla( is a variable that contains three bit fields. 6$.What is a preprocessor* What are the advanta(es of preprocessor ? /nsD / preprocessor processes the source code pro(ram before it passes throu(h the compiler. 18 a preprocessor involves the readabilit! of pro(ram 28 #t facilitates easier modification 38 #t helps in writin( portable pro(rams 48 #t enables easier debu((in( 58 #t enables testin( a part of pro(ram 68 #t helps in developin( (enerali0ed pro(ram '.what are the facilities provided b! preprocessor ? /nsD 18file inclusion 28substitution facilit! 38conditional compilation 1.What are the two forms of 9include directive ? /nsD 1.9include RfilenameR 2.9includeXfilenameW the first form is used to search the director! that contains the source file. #f the search fails in the home director! it searches the implementation defined locations. #n the second form *the preprocessor searches the file onl! in the implementation defined locations. 2.7ow would !ou use the functions randomi0e%& and random%&? /nsD ;andomi0e%& initiates random number (eneration with a random value. ;andom%& (enerates random number between ' and n81F 3.What do the functions atoi%&* itoa%& and (cvt%& do? /nsD

atoi%& is a macro that converts inte(er to character. itoa%& #t converts an inte(er to strin( (cvt%& #t converts a floatin( point number to strin( 4.7ow would !ou use the functions fsee+%&* freed%&* fwrite%& and ftell%&? 5.What is the difference between the functions memmove%& and memcp!%&? /nsD Ihe ar(uments of memmove%& can overlap in memor!. Ihe ar(uments of memcp!%& cannot. 6.What is a file ? /nsD / file is a re(ion of stora(e in hard dis+s or in au5iliar! stora(e devices. #t contains b!tes of information. #t is not a data t!pe. . What are the t!pes of file? /nsD Jiles are of two t!pes 18hi(h level files%stream oriented files& DIhese files are accessed usin( librar! functions 28low level files%s!stem oriented files& DIhese files are accessed usin( s!stem calls ".What is a stream ? /nsD / stream is a source of data or destination of data that ma! be associated with a dis+ or other #:, device. Ihe source stream provides data to a pro(ram and it is +nown as input stream. Ihe destination stream receives the output from the pro(ram and is +nown as output stream. $.What is meant b! file openin( ? /nsD Ihe action of connectin( a pro(ram to a file is called openin( of a file. Ihis re@uires creatin( an #:, stream before readin( or writin( the data. "'.What is J#2Q ? /nsD J#2Q is a predefined data t!pe. #t is defined in stdio.h file. "1.What is a file pointer ? /nsD Ihe pointer to a J#2Q data t!pe is called as a stream pointer or a file pointer. / file pointer points to the bloc+ of information of the stream that had =ust been opened. "2.7ow is fopen%& used ? /nsD Ihe function fopen%& returns a file pointer. 7ence a file pointer is declared and it is assi(ned as J#2Q >fpF fpE fopen%filename*mode&F

filename is a strin( representin( the name of the file and the mode representsD LrM for read operation LwM for write operation LaM for append operation Lr?M*Mw?M*Ma?M for update operation "37ow is a file closed ? /nsD / file is closed usin( fclose%& function Q(. fclose%fp&F Where fp is a file pointer. "4.What is a random access file ? /nsD / file can be accessed at random usin( fsee+%& function fsee+%fp*position*ori(in&F fp 88 file pointer position 88 number of b!tes offset from ori(in ori(in 88 '*1 or 2 denote the be(innin( *current position or end of file respectivel!. "5.What is the purpose of ftell ? /nsD Ihe function ftell%& is used to (et the current file represented b! the file pointer. ftell%fp&F returns a lon( inte(er value representin( the current file position of the file pointed b! the file pointer fp. #f an error occurs *81 is returned. "6.What is the purpose of rewind%& ? /nsD Ihe function rewind is used to brin( the file pointer to the be(innin( of the file. ;ewind%fp&F Where fp is a file pointer. /lso we can (et the same effect b! fsee+%fp*'*'&F " .)ifference between a arra! name and a pointer variable ? /nsD / pointer variable is a variable where as an arra! name is a fi5ed address and is not a variable. / pointer variable must be initiali0ed but an arra! name cannot be initiali0ed. /n arra! name bein( a constant value *?? and 88 operators cannot be applied to it. "".;epresent a two8dimensional arra! usin( pointer ? /nsD /ddress of aA#BA=B <alue of aA#BA=B 6aA#BA=B

or aA#B ? = or >%a?#& ? = >6aA#BA=B or aA#BA=B or >%aA#B ? = & or >% > % a?#& ?= & "$.)ifference between an arra! of pointers and a pointer to an arra! ? /nsD /rra! of pointers 6 3ointers to an arra! 18 )eclaration isD data[t!pe >arra![nameAsi0eBF 28.i0e represents the row si0e. 28 Ihe space for columns ma! be d!namicall! 18)eclaration is data[t!pe % >arra![name&Asi0eBF 28.i0e represents the column si0e. $'.Can we use an! name in place of ar(v and ar(c as command line ar(uments ? /nsD !es we can use an! user defined name in place of ar(c and ar(vF $1.What are the pointer declarations used in C? /nsD 18 /rra! of pointers*e.( * int >aA1'BF /rra! of pointers to inte(er 283ointers to an arra!*e.( * int %>a&A1'BF 3ointer to an arra! of into 38Junction returnin( a pointer*e.(* float >f% & F Junction returnin( a pointer to float 483ointer to a pointer *e.(* int >>5F 3ointer to apointer to int 58pointer to a data t!pe *e.(* char >pF pointer to char $2.)ifferentiate between a constant pointer and pointer to a constant ? /nsD const char >pF ::pointer to a const character. char const >pF ::pointer to a const character. char > const pF ::const pointer to a char variable. const char > const pF :: const pointer to a const character. $3.#s the allocated space within a function automaticall! deallocated when the function returns? /nsD 1o pointer is different from what it points to .2ocal variables includin( local pointers variables in a function are deallocated automaticall! when function

returns.*Put in case of a local pointer variable *deallocation means that the pointer is deallocated and not the bloc+ of memor! allocated to it.Kemor! d!namicall! allocated alwa!s persists until the allocation is freed or the pro(ram terminates. $4.)iscuss on pointer arithmetic? /nsD 18 /ssi(nment of pointers to the same t!pe of pointers. 28 /ddin( or subtractin( a pointer and an inte(er. 38subtractin( or comparin( two pointer. 48incrementin( or decrementin( the pointers pointin( to the elements of an arra!.When a pointer to an inte(er is incremented b! one *the address is incremented b! two.#t is done automaticall! b! the compiler. 58/ssi(nin( the value ' to the pointer variable and comparin( ' with the pointer.Ihe pointer havin( address ' points to nowhere at all. $5.What are the invalid pointer arithmetic ? /nsD i& addin( *multipl!in( and dividin( two pointers. ii& shiftin( or mas+in( pointer. iii& /ddition of float or double to pointer. iv& /ssi(nment of a pointer of one t!pe to a pointer of another t!pe ? $6.What are the advanta(es of usin( arra! of pointers to strin( instead of an arra! of strin(s? /nsD i& Qfficient use of memor!. ii& Qasier to e5chan(e the strin(s b! movin( their pointers while sortin(. $ ./re the e5pressions >ptr ?? and ?? >ptr same ? /nsD 1o *>ptr ?? increments pointer and not the value pointed b! it.Whereas ?? >ptr increments the value bein( pointed to b! ptr. $".What would be the e@uivalent pointer e5pression foe referrin( the same element as aApBA@BArBAsB ? /ns D >% > % > % > %a?p& ? @ & ? r & ? s& $$./re the variables ar(c and ar(v are alwa!s local to main ? /nsD Nes the! are local to main.

1''.Can main %& be called recursivel!? /nsD Nes an! function includin( main %& can be called recursivel!. 1'1.Can we initiali0e unions? /nsD /1.# .tandard C allows an initiali0er for the first member of a union. Ihere is no standard wa! of initiali0in( an! other member %nor* under a pre8/1.# compiler* is there (enerall! an! wa! of initiali0in( a union at all&. 1'2.WhatCs the difference between these two declarations? /nsD struct 51 G ... HF t!pedef struct G ... H 52F Ihe first form declares a structure ta(F the second declares a t!pedef. Ihe main difference is that the second declaration is of a sli(htl! more abstract t!pe.its users don4t necessaril! +now that it is a structure* and the +e!word struct is not used when declarin( instances of it. 1'3.Wh! doesn4t this codeD aAiB E i??F wor+? /nsD Ihe sube5pression i?? causes a side effect.it modifies i4s value.which leads to undefined behavior since i is also referenced elsewhere in the same e5pression. 1'4.W7! doesn4t struct 5 G ... HF 5 thestructF wor+? /nsD C is not C??. I!pedef names are not automaticall! (enerated for structure ta(s. 1'5.Wh! can4t we compare structures? /nsD Ihere is no sin(le* (ood wa! for a compiler to implement structure comparison which is consistent with C4s low8level flavor. / simple b!te8b!8b!te comparison could founder on random bits present in unused \\holes44 in the structure %such paddin( is used to +eep the ali(nment of later fields correct&. / field8b!8field comparison mi(ht re@uire unacceptable amounts of repetitive code for lar(e structures. 1'6.7ow are structure passin( and returnin( implemented? /nsD When structures are passed as ar(uments to functions* the entire

structure is t!picall! pushed on the stac+* usin( as man! words as are re@uired. .ome compilers merel! pass a pointer to the structure* thou(h the! ma! have to ma+e a local cop! to preserve pass8 b!8value semantics. .tructures are often returned from functions in a location pointed to b! an e5tra* compiler8supplied \\hidden44 ar(ument to the function. .ome older compilers used a special* static location for structure returns* althou(h this made structure8 valued functions non8reentrant* which /1.# C disallows.

S-ar putea să vă placă și