C Interview Questions - TechPreparation
C Interview Questions - TechPreparation
C Interview Questions - TechPreparation
com
C Interview Questions
And Answers
2008
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(...).
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");
}
}
"union" Data Type What is the output of the following program? Why?
#include
main() {
typedef union {
int a;
char b[10];
float c;
}
Union;
Macros are a necessary evils of life. The purists don’t 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.
* 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.
in char const* p, the ptr ‘p’ is constant not the character referenced by
it, so u cant make ‘p’ to reference to any other location but u can
change the value of the char pointed by ‘p’.
What is hashing?
To hash means to grind up, and that’s 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 can’t perform
comparison searches.
To search for an item, you simply hash it and look at all the data whose
hash values match that of the data you’re 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 can’t, really. free() can , but there’s no way for your program to
know the trick free() uses. Even if you disassemble the library and
discover the trick, there’s no guarantee the trick won’t change with the
next release of the compiler.
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 today’s
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?
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 don’t fit
in your small data and code segments. When that happens, you can
- 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?
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?
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.
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 don’t 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.
What is Polymorphism ?
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 program’s 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.