C Questions
C Questions
C Interview Questions
And Answers
2017
answers
C Interview Questions and Answers
What is C language?
printf() Function
What is the output of printf("%d")?
3. Some compilers check the format string and will generate an error
without the proper number and type of arguments for things like
printf(...) and scanf(...).
Page 2
malloc() Function- What is the difference between "calloc(...)" and
"malloc(...)"?
sprintf(...) writes data to the character array whereas printf(...) writes data to the
standard output device.
Size of the final executable can be reduced using dynamic linking for
libraries.
Linked Lists -- Can you tell me how to check whether a linked list is circular?
Create two pointers, and set both to the start of the list. Update each
as follows:
while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next;
if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2) {
print ("circular");
}
}
Page 3
If a list is circular, at some point pointer2 will wrap around and be
either at the item just before pointer1, or the item before that. Either
way, its either 1 or 2 jumps until they meet.
"union" Data Type What is the output of the following program? Why?
#include
main() {
typedef union {
int a;
char b[10];
float c;
}
Union;
Page 4
For example, you cant do this without macros
#define PRINT(EXPR) printf( #EXPR =%d\n, EXPR)
Macros are a necessary evils of life. The purists dont like them, but
without it no real work gets done.
Variables with block scope, and with static specifier have static scope.
Global variables (i.e, file scope) with or without the static specifier also
have static scope.
A major difference is: string will have static storage duration, whereas
as a character array will not, unless it is explicity specified by using the
static keyword.
Page 5
* the multibyte character sequence, to which we generally call string,
is used to initialize an array of static storage duration. The size of this
array is just sufficient to contain these characters plus the terminating
NUL character.
* Two strings of same value[1] may share same memory area. For
example, in the following declarations:
[1] The value of a string is the sequence of the values of the contained
characters, in order.
What is hashing?
To hash means to grind up, and thats essentially what hashing is all
about. The heart of a hashing algorithm is a hash function that takes
your nice, neat data and grinds it into some random-looking integer.
The idea behind hashing is that some data either has no inherent
ordering (such as images) or is expensive to compare (such as
images). If the data has no inherent ordering, you cant perform
comparison searches.
Page 6
even by a binary search might be too many. So instead of looking at
the data themselves, youll condense (hash) the data to an integer (its
hash value) and keep all the data with the same hash value in the
same place. This task is carried out by using the hash value as an
index into an array.
To search for an item, you simply hash it and look at all the data whose
hash values match that of the data youre looking for. This technique
greatly lessens the number of items you have to look at. If the
parameters are set up with care and enough storage is available for
the hash table, the number of comparisons needed to find an item can
be made arbitrarily close to one.
There are two ways to resolve this problem. In open addressing, the
collision is resolved by the choosing of another position in the hash
table for the element inserted later. When the hash table is searched, if
the entry is not found at its hashed position in the table, the search
continues checking until either the element is found or an empty
position in the table is found.
You cant, really. free() can , but theres no way for your program to
know the trick free() uses. Even if you disassemble the library and
discover the trick, theres no guarantee the trick wont change with the
next release of the compiler.
Page 7
own private copy of the variable, which is probably not what was
intended.
Yes. The const modifier means that this code cannot change the value
of the variable, but that does not mean that the value cannot be
changed by means outside this code. For instance, in the example in
FAQ 8, the timer structure was accessed through a volatile const
pointer. The function itself did not change the value of the timer, so it
was declared const. However, the value was changed by hardware on
the computer, so it was declared volatile. If a variable is both const and
volatile, the two modifiers can appear in either order.
Yes. Include files can be nested any number of times. As long as you
use precautionary measures , you can avoid including the same file
twice. In the past, nesting header files was seen as bad programming
practice, because it complicates the dependency tracking function of
the MAKE program and thus slows down compilation. Many of todays
popular compilers make up for this difficulty by implementing a
concept called precompiled headers, in which all headers and
associated dependencies are stored in a precompiled state.
When does the compiler not implicitly generate the address of the first
element of an array?
Page 8
There are times when its necessary to have a pointer that doesnt
point to anything. The macro NULL, defined in , has a value thats
guaranteed to be different from any valid pointer. NULL is a literal zero,
possibly cast to void* or char*. Some people, notably C++
programmers, prefer to use 0 rather than NULL.
The null pointer is used in three ways:
1) To stop indirection in a recursive data structure
2) As an error value
3) As a sentinel value
Streams can be classified into two types: text streams and binary
streams. Text streams are interpreted, with a maximum length of 255
characters. With text streams, carriage return/line feed combinations
are translated to the newline n character and vice versa. Binary
streams are uninterrupted and are treated one byte at a time with no
translation of characters. Typically, a text stream would be used for
reading and writing standard text files, printing output to the screen or
printer, or receiving input from the keyboard.
A binary text stream would typically be used for reading and writing
binary files such as graphics or word processing documents, reading
mouse input, or reading and writing to the modem.
Sometimes you can get away with using a small memory model in
most of a given program. There might be just a few things that dont fit
in your small data and code segments. When that happens, you can
Page 9
use explicit far pointers and function declarations to get at the rest of
memory. A far function can be outside the 64KB segment most
functions are shoehorned into for a small-code model. (Often, libraries
are declared explicitly far, so theyll work no matter what code model
the program uses.) A far pointer can refer to information outside the
64KB data segment. Typically, such pointers are used with farmalloc()
and such, to manage a heap separate from where all the rest of the
data lives. If you use a small-data, large-code model, you should
explicitly make your function pointers far.
- Pointers are used to manipulate data using the address. Pointers use
* operator to access the data pointed to by them
- Arrays use subscripted variables to access and manipulate data.
Array variables can be equivalently written using pointer expression.
No. The exit() function is used to exit your program and return control
to the operating system. The return statement is used to return from a
function and return control to the calling function. If you issue a return
from the main() function, you are essentially returning control to the
calling function, which is the operating system. In this case, the return
statement and exit() function are similar.
What is a method?
What is indirection?
Page 10
What is modular programming?
Declaring a variable means describing its type to the compiler but not
allocating any space for it. Defining a variable means declaring it and
also allocating space to hold the variable. You can also initialize a
variable at the time it is defined.
What is an lvalue?
Page 11
What is the difference between a string and an array?
The value of an array is the same as the address of (or a pointer to)
the first element; so, frequently, a C string and a pointer to char are
used to mean the same thing.
Page 12
idea what type of object a void Pointer really points to. If you write
int *ip;
void *p;
In C and C++, any time you need a void pointer, you can use another
pointer type. For example, if you have a char*, you can pass it to a
function that expects a void*. You dont even need to cast it. In C (but
not in C++), you can use a void* any time you need any kind of
pointer, without casting. (In C++, you need to cast it).
A void pointer is used for working with raw memory or for passing a
pointer to an unspecified type.
A switch statement is generally best to use when you have more than
two conditional expressions based on a single variable of numeric type.
Page 13
source file. Scope refers to the visibility of a function or variable. If the
function or variable is visible outside of the current source file, it is said
to have global, or external, scope. If the function or variable is not
visible outside of the current source file, it is said to have local, or
static, scope.
Page 14
parameters. Built-in functions that predefined and supplied along with
the compiler are known as built-in functions. They are also known as
library functions.
What is Polymorphism ?
Page 15
jump of program execution.
Generally, a jump in execution of any kind should be avoided because
it is not considered good programming practice to use such statements
as goto and longjmp in your program.
When your program calls setjmp(), the current state of your program is
saved in a structure of type jmp_buf. Later, your program can call the
longjmp() function to restore the programs state as it was when you
called setjmp().Unlike the goto statement, the longjmp() and setjmp()
functions do not need to be implemented in the same function.
Page 16