C Interview Questions Answers
C Interview Questions Answers
2. What is a pointer?
4. What is a structure?
5. What is a union?
10. What are macros? What are its advantages and disadvantages?
21. Out of fgets() and gets() which function is safe to use and why?
www.cpp-progams.blogspot.com
Page 2
24. Differentiate between a for loop and a while loop? What are it uses?
32. In C, why is the void pointer useful? When would you use it?
35. What does the error ‘Null Pointer Assignment’ means and what causes this
error?
37. Are the expressions arr and &arr same for an array of integers?
www.cpp-progams.blogspot.com
Page 3
54. How are Structure passing and returning implemented by the complier?
61. What do the ‘c’ and ‘v’ in argc and argv stand for?
66. Write a program to interchange 2 variables without using the third one.
www.cpp-progams.blogspot.com
Page 4
68. What are bit fields? What is the use of bit fields in a Structure declaration?
72. How would you use the functions randomize() and random()?
74. How would you use the functions fseek(), freed(), fwrite() and ftell()?
75. What is the difference between the functions memmove() and memcpy()?
90. Can we use any name in place of argv and argc as command line arguments
?
www.cpp-progams.blogspot.com
Page 5
96. What are the advantages of using array of pointers to string instead of an
array of strings?
98. What would be the equivalent pointer expression foe referring the same
element as
a[p][q][r][s] ?
99. Are the variables argc and argv are always local to main ?
www.cpp-progams.blogspot.com
Page 6
ANS:
1. What does static variable mean?
Ans: Static variables are the variables which retain their values between the function calls. They
are initialized only once their scope is within the function in which they are defined.
2. What is a pointer?
Ans: Pointers are variables which stores the address of another variable. That variable may be a
scalar (including another pointer), or an aggregate (array or structure). The pointed-to object may
be part of a larger object, such as a field of a structure or an element in an array.
3. What are the uses of a pointer?
Ans: Pointer is used in the following cases
i) It is used to access array elements
ii) It is used for dynamic memory allocation.
iii) It is used in Call by reference
iv) It is used in data structures like trees, graph, linked list etc.
4. What is a structure?
Ans: Structure constitutes a super data type which represents several different data types in a
single unit. A structure can be initialized if it is static or global.
5. What is a union?
Ans: Union is a collection of heterogeneous data type but it uses efficient memory utilization
technique by allocating enough memory to hold the largest member. Here a single area of
memory contains values of different types at different time. A union can never be initialized.
6. What are the differences between structures and union?
Ans: A structure variable contains each of the named members, and its size is large enough to
hold all the members. Structure elements are of same size.
A union contains one of the named members at a given time and is large enough to hold the
largest member. Union element can be of different sizes.
7. What are the differences between structures and arrays?
Ans: Structure is a collection of heterogeneous data type but array is a collection of
homogeneous data types.
Array
1-It is a collection of data items of same data type.
2-It has declaration only
3-.There is no keyword.
4- array name represent the address of the starting element.
Structure
1-It is a collection of data items of different data type.
2- It has declaration and definition
3- keyword struct is used
4-Structure name is known as tag it is the short hand notation of the declaration.
8. In header files whether functions are declared or defined?
Ans: Functions are declared within header file. That is function prototypes exist in a header
file,not function bodies. They are defined in library (lib).
9. What are the differences between malloc () and calloc ()?
Ans: Malloc Calloc 1-Malloc takes one argument Malloc(a);where a number of bytes 2-memory
allocated contains garbage values
1-Calloc takes two arguments Calloc(b,c) where b no of object and c size of object
www.cpp-progams.blogspot.com
Page 7
2-It initializes the contains of block of memory to zerosMalloc takes one argument, memory
allocated contains garbage values.
It allocates contiguous memory locations. Calloc takes two arguments, memory allocated
contains all zeros, and the memory allocated is not contiguous.
10. What are macros? What are its advantages and disadvantages?
Ans: Macros are abbreviations for lengthy and frequently used statements. When a macro is
called the entire code is substituted by a single line though the macro definition is of several
lines.
The advantage of macro is that it reduces the time taken for control transfer as in case of
function. The disadvantage of it is here the entire code is substituted so the program becomes
lengthy if a macro is called several times.
Read more: c interview questions for freshers | FreeJobAlert.com
http://www.freejobalert.com/clanguage-
interview-questions-part1/6125/#ixzz1O1xitnOp
11. Difference between pass by reference and pass by value?
Ans: Pass by reference passes a pointer to the value. This allows the callee to modify the
variable directly.Pass by value gives a copy of the value to the callee. This allows the callee to
modify the value without modifying the variable. (In other words, the callee simply cannot
modify the variable, since it lacks a reference to it.)
12. What is static identifier?
Ans: A file-scope variable that is declared static is visible only to functions within that file. A
function-scope or block-scope variable that is declared as static is visible only within that scope.
Furthermore, static variables only have a single instance. In the case of function- or block-scope
variables, this means that the variable is not ―automatic‖ and thus retains its value across
function invocations.
13. Where is the auto variables stored?
Ans: Auto variables can be stored anywhere, so long as recursion works. Practically, they‘re
stored on
the stack. It is not necessary that always a stack exist. You could theoretically allocate function
invocation records from the heap.
14. Where does global, static, and local, register variables, free memory and C Program
instructions get stored?
Ans: Global: Wherever the linker puts them. Typically the ―BSS segment‖ on many platforms.
Static: Again, wherever the linker puts them. Often, they‘re intermixed with the globals. The
only difference between globals and statics is whether the linker will resolve the symbols across
compilation units.Local: Typically on the stack, unless the variable gets register allocated and
never spills.Register: Nowadays, these are equivalent to ―Local‖ variables. They live on the stack
unless they get register-allocated.
15. Difference between arrays and linked list?
Ans: An array is a repeated pattern of variables in contiguous storage. A linked list is a set of
structures scattered through memory, held together by pointers in each element that point to the
next element. With an array, we can (on most architectures) move from one element to the next
by adding a fixed constant to the integer value of the pointer. With a linked list, there is a ―next‖
pointer in each structure which says what element comes next.
16. What are enumerations?
Ans: They are a list of named integer-valued constants. Example:enum color { black , orange=4,
www.cpp-progams.blogspot.com
Page 8
yellow, green, blue, violet };This declaration defines the symbols ―black‖, ―orange‖, ―yellow‖,
etc. to have the values ―1,‖ ―4,‖ ―5,‖ … etc. The difference between an enumeration and a macro
is that the enum actually declares a type, and therefore can be type checked.
17. Describe about storage allocation and scope of global, extern, static, local and register
variables?
Ans:
Globals have application-scope. They‘re available in any compilation unit that includes an
appropriate declaration (usually brought from a header file). They‘re stored wherever the linker
puts them, usually a place called the ―BSS segment.‖
Extern? This is essentially ―global.‖
Static: Stored the same place as globals, typically, but only available to the compilation unit that
contains them. If they are block-scope global, only available within that block and its subblocks.
Local: Stored on the stack, typically. Only available in that block and its subblocks.
(Although pointers to locals can be passed to functions invoked from within a scope where that
local is valid.)
Register: See tirade above on ―local‖ vs. ―register.‖ The only difference is that
the C compiler will not let you take the address of something you‘ve declared as ―register.‖
18. What are register variables? What are the advantages of using register variables?
Ans: If a variable is declared with a register storage class,it is known as register variable.The
register variable is stored in the cpu register instead of main memory.Frequently used variables
are declared as register variable as it‘s access time is faster.
19. What is the use of typedef?
Ans: The typedef help in easier modification when the programs are ported to another machine.
A descriptive new name given to the existing data type may be easier to understand the code.
20. Can we specify variable field width in a scanf() format string? If possible how?
Ans: All field widths are variable with scanf(). You can specify a maximum field width for a
given
field by placing an integer value between the ‗%‘ and the field type specifier. (e.g. %64s). Such a
specifier will still accept a narrower field width.
The one exception is %#c (where # is an integer). This reads EXACTLY # characters, and it is
the
only way to specify a fixed field width with scanf().
21. Out of fgets() and gets() which function is safe to use and why?
Ans: fgets() is safer than gets(), because we can specify a maximum input length. Neither one is
completely safe, because the compiler can‘t prove that programmer won‘t overflow the buffer he
pass to fgets ().
22. Difference between strdup and strcpy?
Ans: Both copy a string. strcpy wants a buffer to copy into. strdup allocates a buffer using
malloc().
Unlike strcpy(), strdup() is not specified by ANSI .
23. What is recursion?
Ans: A recursion function is one which calls itself either directly or indirectly it must halt at a
definite point to avoid infinite recursion.
24. Differentiate between for loop and a while loop? What are it uses?
Ans: For executing a set of statements fixed number of times we use for loop while when the
number of
www.cpp-progams.blogspot.com
Page 9
www.cpp-progams.blogspot.com
Page 10
www.cpp-progams.blogspot.com
Page 11
built in
functions. The form and place of declaration where the variable is declared in a program
determine the linkage of variable.
45. Is it possible to have negative index in an array?
Ans: Yes it is possible to index with negative value provided there are data stored in this
location. Even if it is illegal to refer to the elements that are out of array bounds, the compiler
will not produce error because C has no check on the bounds of an array.
46. Why is it necessary to give the size of an array in an array declaration?
Ans: When an array is declared, the compiler allocates a base address and reserves enough space
in
memory for all the elements of the array. The size is required to allocate the required space and
hence size must be mentioned.
47. What modular programming?
Ans: If a program is large, it is subdivided into a number of smaller programs that are called
modules or subprograms. If a complex problem is solved using more modules, this approach is
known as modular programming.
48. What is a function?
Ans: A large program is subdivided into a number of smaller programs or subprograms. Each
subprogram
specifies one or more actions to be performed for the larger program. Such sub programs are
called functions.
49. What is an argument?
Ans: An argument is an entity used to pass data from the calling to a called function.
50. What are built in functions?
Ans: The functions that are predefined and supplied along with the compiler are known as
builtin
functions. They are also known as library functions.
51. Difference between formal argument and actual argument?
Ans: Formal arguments are the arguments available in the function definition. They are preceded
by
their own data type. Actual arguments are available in the function call. These arguments are
given
as constants or variables or expressions to pass the values to the function.
52. Is it possible to have more than one main() function in a C program ?
Ans: The function main() can appear only once. The program execution starts from main.
53. What is the difference between an enumeration and a set of pre-processor # defines?
Ans: There is hardly any difference between the two, except that #defines has a global effect
(throughout the file) whereas an enumeration can have an effect local to the block if desired.
Some advantages of enumeration are that the numeric values are automatically assigned whereas
in #define we have to explicitly define them. A disadvantage is that we have no control over the
size of enumeration variables.
54. How are Structure passing and returning implemented by the complier?
Ans: When structures are passed as argument to functions, the entire structure is typically
pushed on
the stack. To avoid this overhead many programmer often prefer to pass pointers to structure
instead of actual structures. Structures are often returned from functions in a location pointed to
www.cpp-progams.blogspot.com
Page 12
www.cpp-progams.blogspot.com
Page 13
other separators.
63. What are C identifiers?
Ans: These are names given to various programming element such as variables, function,
arrays.It is a combination of letter, digit and underscore.It should begin with letter. Backspace is
not allowed.
64. Difference between syntax vs logical error?
Ans:
Syntax Error
1-These involves validation of syntax of language.
2-compiler prints diagnostic message.
Logical Error
1-logical error are caused by an incorrect algorithm or by a statement mistyped in such a way
that it doesn‘t violet syntax of language.
2-difficult to find.
65. What is preincrement and post increment?
Ans: ++n (pre increment) increments n before its value is used in an assignment operation or any
expression containing it. n++ (post increment) does increment after the value of n is used.
66. Write a program to interchange 2 variables without using the third one.
Ans:
a ^= b; ie a=a^b
b ^= a; ie b=b^a;
a ^= b ie a=a^b;
here the numbers are converted into binary and then xor operation is performed.
You know, you‘re just asking ―have you seen this overly clever trick that‘s not worth applying
on
modern architectures and only really applies to integer variables?‖
67. What is the maximum combined length of command line arguments including the space
between adjacent arguments?
Ans: It depends on the operating system.
68. What are bit fields? What is the use of bit fields in a Structure declaration?
Ans: A bit field is a set of adjacent bits within a single implementation based storage unit that we
will call a ―word‖.
The syntax of field definition and access is based on structure.
Struct {
unsigned int k :1;
unsigned int l :1;
unsigned int m :1;
}flags;
the number following the colon represents the field width in bits.Flag is a variable that contains
three bit fields.
69. What is a preprocessor, what are the advantages of preprocessor?
Ans: A preprocessor processes the source code program before it passes through the compiler.
1- a preprocessor involves the readability of program
2- It facilitates easier modification
3- It helps in writing portable programs
4- It enables easier debugging
www.cpp-progams.blogspot.com
Page 14
www.cpp-progams.blogspot.com
Page 15
Ans: The action of connecting a program to a file is called opening of a file. This requires
creating
an I/O stream before reading or writing the data.
80. What is FILE?
Ans: FILE is a predefined data type. It is defined in stdio.h file.
81. What is a file pointer?
Ans: The pointer to a FILE data type is called as a stream pointer or a file pointer. A file pointer
points to the block of information of the stream that had just been opened.
82. How is fopen()used ?
Ans: The function fopen() returns a file pointer. Hence a file pointer is declared and it is
assigned
as
FILE *fp;
fp= fopen(filename,mode);
filename is a string representing the name of the file and the mode represents:
―r‖ for read operation
―w‖ for write operation
―a‖ for append operation
―r+‖,‖w+‖,‖a+‖ for update operation
83How is a file closed ?
Ans: A file is closed using fclose() function
Eg. fclose(fp);
Where fp is a file pointer.
84. What is a random access file?
Ans:
A file can be accessed at random using fseek() function
fseek(fp,position,origin);
fp file pointer
position number of bytes offset from origin
origin 0,1 or 2 denote the beginning ,current position or end of file respectively.
85. What is the purpose of ftell ?
Ans: The function ftell() is used to get the current file represented by the file pointer.
ftell(fp);
returns a long integer value representing the current file position of the file pointed by the
file pointer fp.If an error occurs ,-1 is returned.
86. What is the purpose of rewind() ?
Ans: The function rewind is used to bring the file pointer to the beginning of the file.
Rewind(fp);
Where fp is a file pointer.Also we can get the same effect by
feek(fp,0,0);
87. Difference between a array name and a pointer variable?
Ans: A pointer variable is a variable where as an array name is a fixed address and is not a
variable. A
pointer variable must be initialized but an array name cannot be initialized. An array name being
a constant value , ++ and — operators cannot be applied to it.
88. Represent a two-dimensional array using pointer?
www.cpp-progams.blogspot.com
Page 16
Ans:
Address of a[I][j] Value of a[I][j]
&a[I][j]
or
a[I] + j
or
*(a+I) + j
*&a[I][j] or a[I][j]
or
*(a[I] + j )
or
*( * ( a+I) +j )
89. Difference between an array of pointers and a pointer to an array?
Ans:
Array of pointers
1- Declaration is: data_type *array_name[size];
2-Size represents the row size.
3- The space for columns may be dynamically
Pointers to an array
1-Declaration is data_type ( *array_name)[size];
2-Size represents the column size.
90. Can we use any name in place of argv and argc as command line arguments ?
Ans: yes we can use any user defined name in place of argc and argv;
91. What are the pointer declarations used in C?
Ans:
1- Array of pointers, e.g , int *a[10]; Array of pointers to integer
2-Pointers to an array,e.g , int (*a)[10]; Pointer to an array of into
3-Function returning a pointer,e.g, float *f( ) ; Function returning a pointer to float
4-Pointer to a pointer ,e.g, int **x; Pointer to apointer to int
5-pointer to a data type ,e.g, char *p; pointer to char
92. Differentiate between a constant pointer and pointer to a constant?
Ans:
const char *p; //pointer to a const character.
char const *p; //pointer to a const character.
char * const p; //const pointer to a char variable.
const char * const p; // const pointer to a const character.
93. Is the allocated space within a function automatically deallocated when the function
returns?
Ans: No pointer is different from what it points to .Local variables including local pointers
variables in a function are deallocated automatically when function returns.,But in case of a
local pointer variable ,deallocation means that the pointer is deallocated and not the block of
memory allocated to it. Memory dynamically allocated always persists until the allocation is
freed
or the program terminates.
94. Discuss on pointer arithmetic?
Ans:
www.cpp-progams.blogspot.com
Page 17
www.cpp-progams.blogspot.com
Ans:
C is not C++. Typedef names are not automatically generated for structure tags.
105. Why can’t we compare structures?
Ans:
There is no single, good way for a compiler to implement structure comparison which is
consistent with C‘s low-level flavor. A simple byte-by-byte comparison could founder on
random bits present in unused ―holes‖ in the structure (such padding is used to keep the
alignment of later fields correct). A field-by-field comparison might require unacceptable
amounts of repetitive code for large structures.
106. How are structure passing and returning implemented?
Ans: When structures are passed as arguments to functions, the entire structure is typically
pushed on
the stack, using as many words as are required. Some compilers merely pass a pointer to the
structure, though they may have to make a local copy to preserve pass-by-value semantics.
Structures are often returned from functions in a location pointed to by an extra,compilersupplied
―hidden‖ argument to the function. Some older compilers used a special,static location
for structure returns, although this made structure-valued functions non-reentrant, which ANSI C
disallows.
C Programs Online