Friday, April 4, 2014

couch mode print story

C Programming Interview Questions and Answers

C Programming Interview Questions answers

1. What is C language?

The C programming language is a standardized programming language developed in the early 1970s by Ken
Thompson and Dennis Ritchie for use on the UNIX operating system. It has since spread to many other
operating systems, and is one of the most widely used programming languages. C is prized for its efficiency,
and is the most popular programming language for writing system software, though it is also used for writing
applications.


2. What are the different storage classes in C?

C has three types of storage: automatic, static and allocated. Variable having block scope and
without static specifier have automatic storage duration.
Variables with block scope, and with static specifier have static scope. Global variables (i.e, file scope)
with or without the the static specifier also have static scope. Memory obtained from calls to
malloc(), alloc() or realloc() belongs to allocated storage class.


3. What is a pointer?

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 the purpose of main() function?

The function main() invokes other functions within it.It is the first function to be called when the
program starts execution.
 It is the starting function.
 It returns an int value to the environment that called the program.
 Recursive call is allowed for main( ) also.
 It is a user-defined function.


5. What are the uses of a pointer?

Pointer is used in the following cases
 It is used to access array elements.
 It is used for dynamic memory allocation.
 It is used in Call by reference.
 It is used in data structures like trees, graph, linked list etc.

<--break-->
6. What does static variable mean?

There are 3 main uses for the static.
1. If you declare within a function:
It retains the value between function calls
2.If it is declared for a function name:
By default function is extern..so it will be visible from other files if the function declaration is as
static..it is invisible for the outer files
3. Static for global variables:
By default we can use the global variables from outside files If it is static global..that variable is limited
to with in the file


7. Can static variables be declared in a header file?

You can’t declare a static variable without defining it as well (this is because the storage class modifiers static
and extern are mutually exclusive). A static variable can be defined in a header file, but this would cause each
source file that included the header file to have its own private copy of the variable, which is probably not
what was intended.


8. What is a structure?

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.


9. What is message passing?

An object oriented program consists of a set of objects that communicate with each other. Message passing
involves specifying the name of the object, the name of the function and the information to be sent.


10. What is a macro?

Macros are the identifiers that represent statements or expressions. To associate meaningful identifiers with
constants, keywords, and statements or expressions.


11. How can you determine the size of an allocated portion of memory?

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.


12. What is a null pointer?

There are times when it’s necessary to have a pointer that doesn’t point to anything. The macro
NULL, defined in , has a value that’s 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.


13. What is static memory allocation and dynamic memory allocation?

Static memory allocation: The compiler allocates the required memory space for a declared
variable.By using the address of operator,the reserved address is obtained and this address may be
assigned to a pointer variable.Since most of the declared variable have static memory,this way of
assigning pointer value to a pointer variable is known as static memory allocation. memory is
assigned during compilation time.
Dynamic memory allocation: It uses functions such as malloc( ) or calloc( ) to get memory
dynamically.If these functions are used to get memory dynamically and the values returned by these
functions are assingned to pointer variables, such assignments are known as dynamic memory
allocation.memory is assined during run time.


14. Difference between const char* p and char const* p

In const char* p, the character pointed by ‘p’ is constant, so u cant change the value of character
pointed by p but u can make ‘p’ refer to some other location.
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’.


15. What is the quickest sorting method to use?

The answer depends on what you mean by quickest. For most sorting problems, it just doesn’t
matter how quick the sort is because it is done infrequently or other operations take significantly
more time anyway. There are three sorting methods in this author’s toolbox that are all very fast and
that are useful in different situations. Those methods are quick sort, merge sort, and radix sort.


16. What are the differences between structures and union?

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.


17. What is the difference between arrays and pointers?

Array is collection of similar datatype. it is a static memory allocation means we can not increment
and decrement the arry size once we allocated. and we can not increment the base address, reassign
address.
Pointer is a dynamic memory allocation. we can allocate the size as we want, assigning into another
variable and base address incrementation is allowed.


18. What is the difference between class and structure?

 By default, the members ot structures are public while that tor class is private.
 structures doesn’t provide something like data hiding which is provided by the classes.
 structures contains only data while class bind both data and member functions.


19. What is the difference between ordinary variable and pointer in C?

An ordinary variable is like a container it can hold any value and we can change the value of ordinary variable
at a time throughout the program .A pointer is a variable that stores the address of another Variable.


20. What are the techniques you use for debugging?

(i)Using compiler’s features
(ii)Read The Fine Module
(iii)printf( ) debugging
(iv)Code grinding
(v)Assertion

Written