question on c
TRANSCRIPT
-
8/18/2019 Question on C
1/22
Question
1. What does static variable mean?2. What is a pointer?
3. What are the uses of a pointer?
4. What is a structure?
5. What is a union?
6. What are the differences between structures and union?
7. What are the differences between structures and arrays?
. !n header files whether functions are declared or defined?
". What are the differences between malloc #$ and calloc #$?
1%. What are macros? What are its advanta&es and disadvanta&es?
11. 'ifference between pass by reference and pass by12. What is static identifier?
13. Where is the auto variables stored?
14. Where does &lobal( static( and local( re&ister
15. 'ifference between arrays and lin)ed list?
16. What are enumerations?
17. 'escribe about stora&e allocation and scope of
1. What are re&ister variables? What are the advanta&es
1". What is the use of typedef?
2%. *an we specify variable field width in a scanf#$
21. +ut of f&ets#$ and &ets#$ which function is safe to use and why?
22. 'ifference between strdup and strcpy?
23. What is recursion?
24. 'ifferentiate between a for loop and a while loop? What are it uses?
25. What is stora&e class.What are the different stora&e classes in *?
26. What the advanta&es of usin& ,nions?
27. What is the difference between -trin&s and rrays?
2. What is a far pointer? where we use it?
2". What is a hu&e pointer?
3%. What is a normali/ed pointer (how do we normali/e a pointer?31. What is near pointer.
32. !n *( why is the void pointer useful? When would you use it?
33. What is a 0, ointer? Whether it is same as an uninitiali/ed pointer?
34. re pointers inte&er ?
35. What does the error 0ull ointer ssi&nment means and what causes this error?
36. What is &eneric pointer in *?
-
8/18/2019 Question on C
2/22
37. re the epressions arr and arr same for an array of inte&ers?
3. ow pointer variables are initiali/ed ?
3". What is static memory allocation ?
4%. What is dynamic memory allocation?
41. What is the purpose of realloc ?
42. What is pointer to a pointer.
43. What is an array of pointers ?
44. 'ifference between lin)er and lin)a&e ?
45. !s it possible to have ne&ative inde in an array?
46. Why is it necessary to &ive the si/e of an array in an array declaration ?
47. What modular pro&rammin& ?
4. What is a function ?
4". What is an ar&ument ?
5%. What are built in functions ?
51. 'ifference between formal ar&ument and actual ar&ument ?52. !s it possible to have more than one main#$ function in a * pro&ram ?
53. What is the difference between an enumeration and a set of pre8processor 9 defines?
54. ow are -tructure passin& and returnin& implemented by the complier?
55. What is the similarity between a -tructure( ,nion and enumeration?
56. *an a -tructure contain a ointer to itself?
57. ow can we read:write -tructures from:to data files?
5. Write a pro&ram which employs ;ecursion ?
5". Write a pro&ram which uses *ommand ine r&uments?
6%. 'ifference between array and pointer ?
61. What do the c and v in ar&c and ar&v stand for?62. What are * to)ens ?
63. What are * identifiers?
64. 'ifference between synta vs lo&ical error?
65. What is preincrement and post increment ?
66. Write a pro&ram to interchan&e 2 variables without usin& the third one.
67. What is the maimum combined len&th of command line ar&uments includin& the space
between ad
-
8/18/2019 Question on C
3/22
77. What are the types of file?
7. What is a stream ?
7". What is meant by file openin& ?
1. What is a file pointer ?
2. ow is fopen#$used ?
3. ow is a file closed ?
4. What is a random access file ?
5. What is the purpose of ftell ?
6. What is the purpose of rewind#$ ?
7. 'ifference between a array name and a pointer variable ?
. ;epresent a two8dimensional array usin& pointer ?
". 'ifference between an array of pointers and a pointer to an array ?
"%. *an we use any name in place of ar&v and ar&c as command line ar&uments ?
"1. What are the pointer declarations used in *?
"2. 'ifferentiate between a constant pointer and pointer to a constant ?"3. !s the allocated space within a function automatically deallocated when the function returns?
"4. 'iscuss on pointer arithmetic?
"5. What are the invalid pointer arithmetic ?
"6. What are the advanta&es of usin& array of pointers to strin& instead of an array of strin&s?
"7. re the epressions =ptr >> and >> =ptr same ?
". What would be the euivalent pointer epression foe referrin& the same element as a@pA@A@rA
@sA ?
"". re the variables ar&c and ar&v are always local to main ?
1%%. *an main #$ be called recursively?
1%1. *an we initiali/e unions?1%2. Whats the difference between these two declarations?
1%3. Why doesnt this codeB a@iA C i>>D wor)?
1%4. Why doesnt struct E F GD thestructD wor)?
1%5. Why cant we compare structures?
1%6. ow are structure passin& and returnin& implemented?
C language interview questions | Part1
1. What does static variable mean?nsB -tatic variables are the variables which retain their values between the function calls. Hheyare initiali/ed only once their scope is within the function in which they are defined.
2. What is a pointer?
nsB ointers are variables which stores the address of another variable. Hhat variable may be a
scalar #includin& another pointer$( or an a&&re&ate #array or structure$. Hhe pointed8to ob
-
8/18/2019 Question on C
4/22
3. What are the uses o a pointer?
nsB ointer is used in the followin& cases
i$ !t is used to access array elementsii$ !t is used for dynamic memory allocation.
iii$ !t is used in *all by reference
iv$ !t is used in data structures li)e trees( &raph( lin)ed list etc.
!. What is a structure?
nsB -tructure constitutes a super data type which represents several different data types in a
sin&le unit. structure can be initiali/ed if it is static or &lobal.
". What is a union?
nsB ,nion is a collection of hetero&eneous data type but it uses efficient memory utili/ation
techniue by allocatin& enou&h memory to hold the lar&est member. ere a sin&le area of
memory contains values of different types at different time. union can never be initiali/ed.
#. What are the dierences between structures and union?nsB structure variable contains each of the named members( and its si/e is lar&e enou&h to
hold all the members. -tructure elements are of same si/e.
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 si/es.
$. What are the dierences between structures and arra%s?
nsB -tructure is a collection of hetero&eneous data type but array is a collection of
homo&eneous data types.
&rra%
18!t is a collection of data items of same data type.
28!t has declaration only38.Hhere is no )eyword.48 array name represent the address of the startin& element.
'tructure
18!t is a collection of data items of different data type.28 !t has declaration and definition
38 )eyword struct is used
48-tructure name is )nown as ta& it is the short hand notation of the declaration.
(. )n header iles whether unctions are declared or deined?
nsB Iunctions are declared within header file. Hhat is function prototypes eist in a header
file(not function bodies. Hhey are defined in library #lib$.
*. What are the dierences between malloc +, and calloc +,?
nsB Jalloc *alloc 18Jalloc ta)es one ar&ument Jalloc#a$Dwhere a number of bytes 28memoryallocated contains &arba&e values
18*alloc ta)es two ar&uments *alloc#b(c$ where b no of ob
-
8/18/2019 Question on C
5/22
!t allocates conti&uous memory locations. *alloc ta)es two ar&uments( memory allocated
contains all /eros( and the memory allocated is not conti&uous.
1-. What are macros? What are its advantages and disadvantages?
nsB Jacros are abbreviations for len&thy and freuently used statements. When a macro is
called the entire code is substituted by a sin&le line thou&h the macro definition is of severallines.
Hhe advanta&e of macro is that it reduces the time ta)en for control transfer as in case of function.
Hhe disadvanta&e of it is here the entire code is substituted so the pro&ram becomes
len&thy if a macro is called several times.
c language interview questions | Part2
11. ierence between pass b% reerence and pass b% value?
&ns/ ass by reference passes a pointer to the value. Hhis allows the callee to modify the
variable directly.ass by value &ives a copy of the value to the callee. Hhis allows the callee to
modify the value without modifyin& the variable. #!n other words( the callee simply cannot
modify the variable( since it lac)s a reference to it.$
12. What is static identiier?
&ns/ file8scope variable that is declared static is visible only to functions within that file.
function8scope or bloc)8scope variable that is declared as static is visible only within that scope.
Iurthermore( static variables only have a sin&le instance. !n the case of function8 or bloc)8scope
variables( this means that the variable is not KautomaticL and thus retains its value across
function invocations.
http://www.freejobalert.com/c-language-interview-questions-part2/6132/http://www.freejobalert.com/c-language-interview-questions-part2/6132/
-
8/18/2019 Question on C
6/22
13. Where is the auto variables stored?
&ns/ uto variables can be stored anywhere( so lon& as recursion wor)s. ractically( theyre
stored on
the stac). !t is not necessary that always a stac) eist. Mou could theoretically allocate function
invocation records from the heap.
1!. Where does global0 static0 and local0 register variables0 ree memor% and C Program
instructions get stored?
&ns/ NlobalB Wherever the lin)er puts them. Hypically the KO-- se&mentL on many platforms.
-taticB &ain( wherever the lin)er puts them. +ften( theyre intermied with the &lobals. Hhe
only difference between &lobals and statics is whether the lin)er will resolve the symbols across
compilation units.ocalB Hypically on the stac)( unless the variable &ets re&ister allocated and
never spills.;e&isterB 0owadays( these are euivalent to KocalL variables. Hhey live on the stac)
unless they &et re&ister8allocated.
1". ierence between arra%s and lined list?
&ns/ n array is a repeated pattern of variables in conti&uous stora&e. lin)ed list is a set of
structures scattered throu&h memory( held toðer by pointers in each element that point to the
net element. With an array( we can #on most architectures$ move from one element to the net
by addin& a fied constant to the inte&er value of the pointer. With a lin)ed list( there is a KnetL
pointer in each structure which says what element comes net.
1#. What are enumerations?
&ns/ Hhey are a list of named inte&er8valued constants. PampleBenum color E blac) ( oran&eC4(
yellow( &reen( blue( violet GDHhis declaration defines the symbols Kblac)L( Koran&eL( KyellowL(etc. to have the values K1(L K4(L K5(L F etc. Hhe difference between an enumeration and a macro
is that the enum actually declares a type( and therefore can be type chec)ed.
1$. escribe about storage allocation and scope o global0 etern0 static0 local and register
variables?
&ns/
Nlobals have application8scope. Hheyre available in any compilation unit that includes an
appropriate declaration #usually brou&ht from a header file$. Hheyre stored wherever the lin)er
puts them( usually a place called the KO-- se&ment.L
Ptern? Hhis is essentially K&lobal.L'tatic/ -tored the same place as &lobals( typically( but only available to the compilation unit that
contains them. !f they are bloc)8scope &lobal( only available within that bloc) and its subbloc)s.
ocal/ -tored on the stac)( typically. +nly available in that bloc) and its subbloc)s.
#lthou&h pointers to locals can be passed to functions invo)ed from within a scope where that
local is valid.$
-
8/18/2019 Question on C
7/22
4egister/ -ee tirade above on KlocalL vs. Kre&ister.L Hhe only difference is that
the * compiler will not let you ta)e the address of somethin& youve declared as Kre&ister.L
1(. What are register variables? What are the advantages o using register variables?
&ns/ !f a variable is declared with a re&ister stora&e class(it is )nown as re&ister variable.Hhe
re&ister variable is stored in the cpu re&ister instead of main memory.Ireuently used variables
are declared as re&ister variable as its access time is faster.
1*. What is the use o t%pede?
&ns/ Hhe typedef help in easier modification when the pro&rams are ported to another machine.
descriptive new name &iven to the eistin& data type may be easier to understand the code.
2-. Can we speci% variable ield width in a scan+, ormat string? ) possible how?
&nsB ll field widths are variable with scanf#$. Mou can specify a maimum field width for a
&iven
field by placin& an inte&er value between the Q and the field type specifier. #e.&. Q64s$. -uch a
specifier will still accept a narrower field width.
Hhe one eception is Q9c #where 9 is an inte&er$. Hhis reads PR*HM 9 characters( and it is
the
only way to specify a fied field width with scanf#$.
c language interview questions | Part3
July 18th, 2010
!f you would li)e to view ll * lan&ua&e interview uestions only at one place visit below lin)
ll * an&ua&e !nterview Suestions
21. 5ut o gets+, and gets+, which unction is sae to use and wh%?
&ns/ f&ets#$ is safer than &ets#$( because we can specify a maimum input len&th. 0either one is
completely safe( because the compiler cant prove that pro&rammer wont overflow the buffer he
pass to f&ets #$.
22. ierence between strdup and strcp%?&ns/ Ooth copy a strin&. strcpy wants a buffer to copy into. strdup allocates a buffer usin&
malloc#$.
,nli)e strcpy#$( strdup#$ is not specified by 0-! .
http://www.freejobalert.com/c-language-interview-questions-part3/6136/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/http://www.freejobalert.com/c-language-interview-questions-part3/6136/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/
-
8/18/2019 Question on C
8/22
23. What is recursion?
&ns/ recursion function is one which calls itself either directly or indirectly it must halt at a
definite point to avoid infinite recursion.
2!. ierentiate between or loop and a while loop? What are it uses?
&ns/ Ior eecutin& a set of statements fied 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.
2". What is storage class? What are the dierent storage classes in C?
&ns/ -tora&e class is an attribute that chan&es the behavior of a variable. !t controls the lifetime(
scope and lin)a&e. Hhe stora&e classes in c are auto( re&ister( and etern( static( typedef.
2#. What the advantages o using 6nions?
&ns/ When the * compiler is allocatin& memory for unions it will always reserve enou&h room
for the
lar&est member.
2$. What is the dierence between 'trings and &rra%s?
&ns/ -trin& is a seuence of characters endin& with 0, .it can be treated as a one dimensional
array
of characters terminated by a 0, character.
2(. What is a ar pointer? Where we use it?
&ns/ !n lar&e data model #compact( lar&e( hu&e$ the address O%%%%%% is acceptable because in
these
model all pointers to data are 32bits lon&. !f we use small data model#tiny( small( medium$ the
above address wont wor) since in these model each pointer is 16bits lon&. ) we are woring in
a small data model and want to access the address 7---(--- then we use ar pointer. 8ar
pointer is alwa%s treated as a 32bit pointer and contains a segment address and oset
address both o 1#bits each. Hhus the address is represented usin& se&ment B offset format
O%%%hB%%%h. Ior any
&iven memory address there are many possible far address se&ment B offset pair. Hhe 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 huge pointer?
&ns/ 9uge pointer is 32bit long containing segment address and oset address. u&e
pointers are
normali/ed pointers so for any &iven memory address there is only one possible hu&e address
-
8/18/2019 Question on C
9/22
se&mentB offset pair. u&e pointer arithmetic is doe with calls to special subroutines so its
arithmetic slower than any other pointers.
3-. What is a normali:ed pointer0 how do we normali:e a pointer?
&ns/ !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 every 16bytes so the offset will have a value from % to I. for normali/ation
convert the address into 2%bit address then use the 16bit for se&ment address and 4bit for the
offset address. Niven a pointer 5%%'B "4%7(we convert it to a 2%bitabsolute address
54"'7(Which then normali/ed to 54"'B%%%7.
c language interview questions | Part4
July 18th, 2010
!f you would li)e to view ll * lan&ua&e interview uestions only at one place visit below lin)
ll * an&ua&e !nterview Suestions
31. What is near pointer?
&ns/ near pointer is 16 bits lon&. !t uses the current content of the *- #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 C0 wh% is the void pointer useul? When would %ou use it?
&ns/ Hhe void pointer is useful because it is a &eneric pointer that any pointer can be cast into
and
bac) a&ain without loss of information.
33. What is a ;6 Pointer? Whether it is same as an uninitiali:ed pointer?
&ns/ 0ull pointer is a pointer which points to nothin& but uninitiali/ed pointer may point to
anywhere.
3!. &re pointers integer?
&ns/ 0o( pointers are not inte&ers. pointer is an address. !t is a positive number.
http://www.freejobalert.com/c-language-interview-questions-part4/6141/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/http://www.freejobalert.com/c-language-interview-questions-part4/6141/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/
-
8/18/2019 Question on C
10/22
3". What does the error arr same or an arra% o integers?
&ns/ Mes for array of inte&ers they are same.
3(. )P@9ow pointer variables are initiali:ed?
&ns/ ointer variables are initiali/ed by one of the followin& ways.
!. -tatic memory allocation
!!. 'ynamic memory allocation
3*. What is static memor% allocation?
&ns/ *ompiler allocates memory space for a declared variable. Oy usin& the address of operator(
the
reserved address is obtained and this address is assi&ned to a pointer variable. Hhis way of
assi&nin& pointer value to a pointer variable at compilation time is )nown as static memory
allocation.
!-. What is d%namic memor% allocation?
&ns/ dynamic memory allocation uses functions such as malloc#$ or calloc#$ to &et memory
dynamically. !f these functions are used to &et memory dynamically and the values returned by
these function are assi&ned to pointer variables( such a way of allocatin& memory at run time is
)nown as dynamic memory allocation.
c language interview questions | Part5
July 18th, 2010
!f you would li)e to view ll * lan&ua&e interview uestions only at one place visit below lin)
ll * an&ua&e !nterview Suestions
http://www.freejobalert.com/c-language-interview-questions-part5/6146/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/http://www.freejobalert.com/c-language-interview-questions-part5/6146/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/
-
8/18/2019 Question on C
11/22
!1. What is the purpose o realloc?
&ns/ !t increases or decreases the si/e of dynamically allocated array. Hhe function realloc #ptr(n$
uses two ar&uments. Hhe first ar&ument ptr is a pointer to a bloc) of memory for which the si/e is
to be altered. Hhe second ar&ument specifies the new si/e. Hhe si/e may be increased or
decreased. !f sufficient space is not available to the old re&ion the function may create a new
re&ion.
!2. What is pointer to a pointer?
&ns/ !f a pointer variable points another pointer value. -uch a situation is )nown as a pointer to a
pointer.
PampleB
int =p1(==p2(vC1%D
1CvD p2Cp1D
ere p2 is a pointer to a pointer.
!3. What is an arra% o pointers?
&ns/ if the elements of an array are addresses( such an array is called an array of pointers.
!!. ierence between liner and linage?
&ns/ in)er converts an ob
-
8/18/2019 Question on C
12/22
!(. What is a unction?
&ns/ lar&e pro&ram is subdivided into a number of smaller pro&rams or subpro&rams. Pach
subpro&ram
specifies one or more actions to be performed for the lar&er pro&ram. -uch sub pro&rams are
called functions.
!*. What is an argument?
&ns/ n ar&ument is an entity used to pass data from the callin& to a called function.
"-. What are built in unctions?
&ns/ Hhe functions that are predefined and supplied alon& with the compiler are )nown as built8
in functions. Hhey are also )nown as library functions.
c language interview questions | Part 6
July 18th, 2010
!f you would li)e to view ll * lan&ua&e interview uestions only at one place visit below lin)
ll * an&ua&e !nterview Suestions
"1. ierence between ormal argument and actual argument?
&ns/ Iormal ar&uments are the ar&uments available in the function definition. Hhey are preceded
by
their own data type. ctual ar&uments are available in the function call. Hhese ar&uments are
&iven
as constants or variables or epressions to pass the values to the function.
"2. )s it possible to have more than one main+, unction in a C program ?
&ns/ Hhe function main#$ can appear only once. Hhe pro&ram eecution starts from main.
"3. What is the dierence between an enumeration and a set o preAprocessor B deines?
&ns/ Hhere is hardly any difference between the two( ecept that Bdeines has a global eect+throughout the ile, whereas an enumeration can have an eect local to the bloc i
desired. 'ome advantages o enumeration are that the numeric values are automaticall%
assigned whereas in Bdeine we have to eplicitl% deine them. & disadvantage is that we
have no control over the si:e o enumeration variables.
http://www.freejobalert.com/c-language-interview-questions-part-6/6150/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/http://www.freejobalert.com/c-language-interview-questions-part-6/6150/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/
-
8/18/2019 Question on C
13/22
"!. 9ow are 'tructure passing and returning implemented b% the complier?
&ns/ When structures are passed as ar&ument to functions( the entire structure is typically pushed
on
the stac). Ho avoid this overhead many pro&rammer often prefer to pass pointers to structure
instead of actual structures. -tructures are often returned from functions in a location pointed to
by an etra( compiler8supported hidden ar&ument to the function.
"". )P@what is the similarit% between a 'tructure0 6nion and enumeration?
&ns/ ll of them let the pro&rammer to define new data type.
"#. Can a 'tructure contain a Pointer to itsel?
&ns/ Mes such structures are called self8referential structures.
"$. 9ow can we readwrite 'tructures romto data iles?
&ns/ Ho write out a structure we can use fwrite#$ as Iwrite# e( si/eof#e$(1(fp$DWhere e is a
structure
variable. correspondin& fread#$ invocation can read the structure bac) from file. callin&
fwrite#$ it writes out si/eof#e$ bytes from the address e. 'ata files written as memory ima&es
with fwrite#$(however (will not be portable( particularly if they contain floatin& point fields or
ointers. Hhis is because memory layout of structures is machine and compiler
dependent. Hherefore( structures written as memory ima&es cannot necessarily be read bac) by
pro&rams runnin& on other machine( and this is the important concern if the data files youre
writin& will ever be interchan&ed between machines.
"(. Write a program which emplo%s 4ecursion?&ns/ int fact#int n$ E return n T 1 ? n = fact#n U 1$ B 1D G
"*.Write a program which uses Command ine &rguments?
&ns/
9include
void main#int ar&c(char =ar&v@A$
E
int iD
clrscr#$Dfor#iC%Di
printf#KVnQdL(ar&v@iA$D
G
#-. ierence between arra% and pointer?
&ns/
-
8/18/2019 Question on C
14/22
&rra%
18 rray allocates space automatically
28 !t cannot be resi/ed
38 !t cannot be reassi&ned
48 si/eof #arrayname$ &ives the number of bytes occupied by the array.
Pointer
18Pplicitly assi&ned to point to an allocated space.
28!t can be si/ed usin& realloc#$
38pointer can be reassi&ned.
48si/eof #p$ returns the number of bytes used to store the pointer variable p.
c language interview questions | Part7
July 18th, 2010
!f you would li)e to view ll * lan&ua&e interview uestions only at one place visit below lin)
ll * an&ua&e !nterview Suestions
#1. What do the
-
8/18/2019 Question on C
15/22
18Hhese involves validation of synta of lan&ua&e.
28compiler prints dia&nostic messa&e.
ogical Drror
18lo&ical error are caused by an incorrect al&orithm or by a statement mistyped in such a way
that it doesnt violet synta of lan&ua&e.
28difficult to find.
#". What is preincrement and post increment?
&ns/ >>n #pre increment$ increments n before its value is used in an assi&nment operation or any
epression containin& it. n>> #post increment$ does increment after the value of n is used.
##. Write a program to interchange 2 variables without using the third one.
&ns/
a C bD ie aCab
b C aD ie bCbaD
a C b ie aCabD
here the numbers are converted into binary and then or operation is performed.
Mou )now( youre
-
8/18/2019 Question on C
16/22
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& &enerali/ed pro&ram
$-. What are the acilities provided b% preprocessor?
&ns/
18file inclusion
28substitution facility
38conditional compilation
c language interview questions | Part8
July 18th, 2010
!f you would li)e to view ll * lan&ua&e interview uestions only at one place visit below lin)
ll * an&ua&e !nterview Suestions
$1. What are the two orms o Binclude directive?
&ns/
1.9includeLfilenameL
2.9include
the first form is used to search the directory that contains the source file.!f the search fails in the
home directory it searches the implementation defined locations.!n the second form (the
preprocessor searches the file only in the implementation defined locations.
$2. 9ow would %ou use the unctions randomi:e+, and random+,?
&ns/
;andomi/e#$ initiates random number &eneration with a random value.
;andom#$ &enerates random number between % and n81D
$3. What do the unctions atoi+,0 itoa+, and gcvt+, do?
&ns/
atoi#$ is a macro that converts strin& to inte&er
itoa#$ !t converts an inte&er to strin&
&cvt#$ !t converts a floatin& point number to strin&
http://www.freejobalert.com/c-language-interview-questions-part8/6162/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/http://www.freejobalert.com/c-language-interview-questions-part8/6162/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/
-
8/18/2019 Question on C
17/22
$!. 9ow would %ou use the unctions see+,0 reed+,0 write+, and tell+,?
&ns/
fsee)#f(1(i$ Jove the pointer for file f a distance 1 byte from location i.
fread#s(i1(i2(f$ Pnter i2 dataitems(each of si/e i1 bytes(from file f to strin& s.
fwrite#s(i1(i2(f$ send i2 data items(each of si/e i1 bytes from strin& s to file f.
ftell#f$ ;eturn the current pointer position within file f.
Hhe data type returned for functions fread(fsee) and fwrite is int and ftell is lon& int.
$". What is the dierence between the unctions memmove+, and memcp%+,?
&ns/ Hhe ar&uments of memmove#$ can overlap in memory. Hhe ar&uments of memcpy#$ cannot.
$#. What is a ile?
&ns/ file is a re&ion of stora&e in hard dis)s or in auiliary stora&e devices.!t contains bytes of
information .!t is not a data type.
$$. )P@what are the t%pes o ile?
&ns/ Iiles are of two types
18hi&h level files #stream oriented files$ BHhese files are accessed usin& library functions
28low level files#system oriented files$ BHhese files are accessed usin& system calls
$(. )P@what is a stream?
&ns/ stream is a source of data or destination of data that may be associated with a dis) or
other
!:+ device. Hhe source stream provides data to a pro&ram and it is )nown as input stream. Hhe
destination stream eceives the output from the pro&ram and is )nown as output stream.
$*. What is meant b% ile opening?
&ns/ Hhe action of connectin& a pro&ram to a file is called openin& of a file. Hhis reuires
creatin&
an !:+ stream before readin& or writin& the data.
(-. What is 8)D?
&ns/ I!P is a predefined data type. !t is defined in stdio.h file.
c language interview questions | Part
http://www.freejobalert.com/c-language-interview-questions-part9/6165/http://www.freejobalert.com/c-language-interview-questions-part9/6165/
-
8/18/2019 Question on C
18/22
July 18th, 2010
!f you would li)e to view ll * lan&ua&e interview uestions only at one place visit below lin)
ll * an&ua&e !nterview Suestions
(1. What is a ile pointer?&ns/ Hhe pointer to a I!P data type is called as a stream pointer or a file pointer. file pointer
points to the bloc) of information of the stream that had L(Lw>L(La>L for update operation
(39ow is a ile closed ?
&ns/ file is closed usin& fclose#$ function
P&. fclose#fp$D
Where fp is a file pointer.
(!. What is a random access ile?
&ns/
file can be accessed at random usin& fsee)#$ function
fsee)#fp(position(ori&in$D
fp file pointer
position number of bytes offset from ori&in
ori&in %(1 or 2 denote the be&innin& (current position or end of file respectively.
(". What is the purpose o tell ?
&ns/ Hhe function ftell#$ is used to &et the current file represented by the file pointer.
ftell#fp$D
returns a lon& inte&er value representin& the current file position of the file pointed by the
file pointer fp.!f an error occurs (81 is returned.
(#. What is the purpose o rewind+, ?
&ns/ Hhe function rewind is used to brin& the file pointer to the be&innin& of the file.
;ewind#fp$D
http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/
-
8/18/2019 Question on C
19/22
Where fp is a file pointer.lso we can &et the same effect by
fee)#fp(%(%$D
($. ierence between a arra% name and a pointer variable?
&ns/ pointer variable is a variable where as an array name is a fied address and is not a
variable.
pointer variable must be initiali/ed but an array name cannot be initiali/ed. n array name bein&
a constant value ( >> and X operators cannot be applied to it.
((. 4epresent a twoAdimensional arra% using pointer?
&ns/
ddress of a@!A@ <
=a@!A@!$ >< $
(*. ierence between an arra% o pointers and a pointer to an arra%?
&ns/&rra% o pointers
18 'eclaration isB dataZtype =arrayZname@si/eAD
28-i/e represents the row si/e.
38 Hhe space for columns may be dynamically
Pointers to an arra%
18'eclaration is dataZtype # =arrayZname$@si/eAD
28-i/e represents the column si/e.
*-. Can we use an% name in place o argv and argc as command line arguments ?&ns/ yes we can use any user defined name in place of ar&c and ar&vD
-
8/18/2019 Question on C
20/22
c language interview questions | Part10
July 18th, 2010
!f you would li)e to view ll * lan&ua&e interview uestions only at one place visit below lin)
ll * an&ua&e !nterview Suestions
*1. What are the pointer declarations used in C?
&ns/
18 rray of pointers( e.& ( int =a@1%AD rray of pointers to inte&er
28ointers to an array(e.& ( int #=a$@1%AD ointer to an array of into
38Iunction returnin& a pointer(e.&( float =f# $ D Iunction returnin& a pointer to float
48ointer to a pointer (e.&( int ==D ointer to apointer to int
58pointer to a data type (e.&( char =pD pointer to char
*2. ierentiate between a constant pointer and pointer to a constant?
&ns/
const char =pD ::pointer to a const character.
char const =pD ::pointer to a const character.
char = const pD ::const pointer to a char variable.
const char = const pD :: const pointer to a const character.
*3. )s the allocated space within a unction automaticall% deallocated when the unction
returns?&ns/ 0o pointer is different from what it points to .ocal variables includin& local pointers
variables in a function are deallocated automatically when function returns.(Out in case of a
local pointer variable (deallocation means that the pointer is deallocated and not the bloc) of
memory allocated to it. Jemory dynamically allocated always persists until the allocation is
freed
or the pro&ram terminates.
*!. iscuss on pointer arithmetic?
&ns/
18 ssi&nment of pointers to the same type 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 array. When a pointer
to an inte&er is incremented by one ( the address is incremented by two. !t is done automatically
by the compiler.
http://www.freejobalert.com/c-language-interview-questions-part10/6168/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/http://www.freejobalert.com/c-language-interview-questions-part10/6168/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/
-
8/18/2019 Question on C
21/22
58ssi&nin& the value % to the pointer variable and comparin& % with the pointer. Hhe pointer
havin& address % points to nowhere at all.
*". What is the invalid pointer arithmetic?
&ns/
i$ addin& (multiplyin& and dividin& two pointers.
ii$ -hiftin& or mas)in& pointer.
iii$ ddition of float or double to pointer.
iv$ ssi&nment of a pointer of one type to a pointer of another type ?
*#. What are the advantages o using arra% o pointers to string instead o an arra% o
strings?
&ns/
i$ Pfficient use of memory.
ii$ Pasier to echan&e the strin&s by movin& their pointers while sortin&.
*$. &re the epressions Fptr GG and GG Fptr same?
&ns/ 0o(=ptr >> increments pointer and not the value pointed by it. Whereas >> =ptr
increments the value bein& pointed to by ptr.
*(. What would be the equivalent pointer epression oe reerring the same element as
aHpIHqIHrIHsI ?
&ns / =# = # = # = #a>p$ > $ > r $ > s$
**. &re the variables argc and argv are alwa%s local to main?
&ns/ Mes they are local to main.
1--. Can main +, be called recursivel%?
&ns/ Mes any function includin& main #$ can be called recursively.
c language interview questions | Part 11
July 18th, 2010
!f you would li)e to view ll * lan&ua&e interview uestions only at one place visit below lin)
ll * an&ua&e !nterview Suestions
1-1. )P@Can we initiali:e unions?
&ns/ 0-! -tandard * allows an initiali/er for the first member of a union. Hhere is no standard
http://www.freejobalert.com/c-language-interview-questions-part-11/6171/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/http://www.freejobalert.com/c-language-interview-questions-part-11/6171/http://www.freejobalert.com/it-interviewtechnical-questions/c-interview-questions/
-
8/18/2019 Question on C
22/22
way
of initiali/in& any other member #nor( under a pre80-! compiler( is there &enerally any way of
initiali/in& a union at all$.
1-2. What=s the dierence between these two declarations?
&ns/ struct 1 E F GD
typedef struct E F G 2D
Hhe first form declares a structure ta&D the second declares a typedef. Hhe main difference is that
the second declaration is of a sli&htly more abstract type.its users dont necessarily )now that it is
a structure( and the )eyword struct is not used when declarin& instances of it.
1-3. Wh% doesn=t this code/ aHiI J iGGK wor?
&ns/ Hhe subepression i>> causes a side effect.it modifies is value.which leads to undefined
behavior since i is also referenced elsewhere in the same epression.
1-!.W9% doesn=t struct L M NK
thestructK
wor?
&ns/
* is not *>>. Hypedef names are not automatically &enerated for structure ta&s.
1-". Wh% can=t we compare structures?
&ns/
Hhere is no sin&le( &ood way for a compiler to implement structure comparison which is
consistent with *s low8level flavor. simple byte8by8byte comparison could founder on random bits present in unused KholesL in the structure #such paddin& is used to )eep the ali&nment of
later fields correct$. field8by8field comparison mi&ht reuire unacceptable amounts of
repetitive code for lar&e structures.
1-#. 9ow are structure passing and returning implemented?
&ns/ When structures are passed as ar&uments to functions( the entire structure is typically
pushed on
the stac)( usin& as many words as are reuired. -ome compilers merely pass a pointer to the
structure( thou&h they may have to ma)e a local copy to preserve pass8by8value semantics.
-tructures are often returned from functions in a location pointed to by an etra(compiler8supplied KhiddenL ar&ument to the function. -ome older compilers used a special(static location
for structure returns( althou&h this made structure8valued functions non8reentrant( which 0-! *
disallows.