C Interview Questions and Answers
C Interview Questions and Answers
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.
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.
1|Page
PRAKASH ANIL BUCHADCE
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.
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.
2|Page
PRAKASH ANIL BUCHADCE
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.
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 ().
3|Page
PRAKASH ANIL BUCHADCE
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 iterations to be performed is not known in advance we use while loop.
25. What is storage class? What are the different storage classes in C?
Ans: Storage class is an attribute that changes the behavior of a variable. It controls the lifetime,
scope and linkage. The storage classes in c are auto, register, and extern, static, typedef.
4|Page
PRAKASH ANIL BUCHADCE
32. In C, why is the void pointer useful? When would you use it?
Ans: The void pointer is useful because it is a generic pointer that any pointer can be cast into and
back again without loss of information.
35. What does the error ‘Null Pointer Assignment’ means and what causes this error?
Ans: As null pointer points to nothing so accessing a uninitialized pointer or invalid location may
cause an error.
37. Are the expressions arr and &arr same for an array of integers?
Ans: Yes for array of integers they are same.
5|Page
PRAKASH ANIL BUCHADCE
61. What do the ‘c’ and ‘v’ in argc and argv stand for?
Ans: The c in argc(argument count) stands for the number of command line argument the program is
invoked with and v in argv(argument vector) is a pointer to an array of character string that contain
the arguments.
6|Page
PRAKASH ANIL BUCHADCE
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.
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.
7|Page
PRAKASH ANIL BUCHADCE
72. How would you use the functions randomize() and random()?
Ans:
Randomize() initiates random number generation with a random value.
Random() generates random number between 0 and n-1;
74. How would you use the functions fseek(), freed(), fwrite() and ftell()?
Ans:
fseek(f,1,i) Move the pointer for file f a distance 1 byte from location i.
fread(s,i1,i2,f) Enter i2 dataitems,each of size i1 bytes,from file f to string s.
fwrite(s,i1,i2,f) send i2 data items,each of size i1 bytes from string s to file f.
ftell(f) Return the current pointer position within file f.
The data type returned for functions fread,fseek and fwrite is int and ftell is long int.
75. What is the difference between the functions memmove() and memcpy()?
Ans: The arguments of memmove() can overlap in memory. The arguments of memcpy() cannot.
8|Page
PRAKASH ANIL BUCHADCE
9|Page
PRAKASH ANIL BUCHADCE
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;
10 | P a g e
PRAKASH ANIL BUCHADCE
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.
96. What are the advantages of using array of pointers to string instead of an array of strings?
Ans:
i) Efficient use of memory.
ii) Easier to exchange the strings by moving their pointers while sorting.
98. What would be the equivalent pointer expression foe referring the same element as
a[p][q][r][s] ?
Ans : *( * ( * ( * (a+p) + q ) + r ) + s)
11 | P a g e
PRAKASH ANIL BUCHADCE
99. Are the variables argc and argv are always local to main?
Ans: Yes they are local to main.
2. What is an object?
Ans: It is an entity which may correspond to real-world entities such as students, employees, bank
account. It may be concrete such as file system or conceptual such as scheduling policies in
multiprocessor operating system. Every object will have data structures called attributes and
behavior called operations.
Class person
{
private:
char name[20];
int age;
char sex;
public: speak();
walk();
};
12 | P a g e
PRAKASH ANIL BUCHADCE
7. Define OOPs?
Ans: OOP is a method of implementation in which programs are organized as co-operative
collection of objects, each of which represents an instance of some class and whose classes are all
member of a hierarchy of classes united through the property of inheritance.
13 | P a g e
PRAKASH ANIL BUCHADCE
17. What is the difference between method overloading and method overriding?
Ans: Overloading a method (or function) in C++ is the ability for functions of the same name to be
defined as long as these methods have different signatures (different set of parameters). Method
overriding is the ability of the inherited class rewriting the virtual method of the base class.
14 | P a g e
PRAKASH ANIL BUCHADCE
26. How many ways are there to initialize an int with a constant?
Ans: Two.
There are two formats for initializers in C++ as shown in the example that follows. The first format
uses the traditional C notation. The second format uses constructor notation.
int foo = 123;
int bar (123);
15 | P a g e
PRAKASH ANIL BUCHADCE
35. What is the difference between a copy constructor and an overloaded assignment operator?
Ans: A copy constructor constructs a new object by using the content of the argument object. An
overloaded assignment operator assigns the contents of an existing object to another existing object
of the same class.
39. What is a container class? What are the types of container classes?
Ans: A container class is a class that is used to hold objects in memory or external storage. A
container class acts as a generic holder. A container class has a predefined behavior and a well-
known interface. A container class is a supporting class whose purpose is to hide the topology used
for maintaining the list of objects in memory. When a container class contains a group of mixed
objects, the container is called a heterogeneous container; when the container is holding a group of
objects that are all the same, the container is called a homogeneous container
16 | P a g e
PRAKASH ANIL BUCHADCE
17 | P a g e
PRAKASH ANIL BUCHADCE
Best Luck..!!
18 | P a g e