C Interview Questions


C interview question: What is the difference between "calloc(...)" and "malloc(...)"?
Answer:
1. calloc(...) allocates a block of memory for an array of elements of a certain size. By default the block is initialized to 0. The total number of memory allocated will be (number_of_elements * size). malloc(...) takes in only a single argument which is the memory required in bytes. malloc(...) allocated bytes of memory and not blocks of memory like calloc(...).
2. malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insufficient memory available calloc(...) allocates an array in memory with elements initialized to and returns a pointer to the allocated space. calloc(...) calls malloc(...) in order to use the C++ _set_new_mode function to set the new handler mode.

C interview question:What is the difference between "printf(...)" and"sprintf(...)"?
Answer:sprintf(...) writes data to the character array whereas printf(...) writes data to the standard output device.

C interview question:What does static variable mean?
Answer: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

C interview question: What are the differences between malloc() and calloc()?
Answer: There are 2 differences. First, is in the number of arguments. malloc() takes a single argument(memory required in bytes), while calloc() needs 2 arguments(number of variables to allocate memory, size in bytes of a
single variable).Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

C interview question: Difference between const char* p and char const* p?
Answer: 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’.

C interview question:How can you determine the size of an allocated portion of memory?
Answer: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.





These are few basic Interview questions in C .....

1)How do we check whether a linked list is circular?

2)What is the output of this program?

#include
##include
main()
{
typedef union
{
int a;
char b[10];
float c;
}
Struct;

Struct x,y = {100};
x.a = 50;
strcpy(x.b,"hello");
x.c = 21.50;

printf("Union x : %d %s %f \n",x.a,x.b,x.c );
printf("Union y : %d %s %f \n",y.a,y.b,y.c);
}

3)What is a Null object?

4)Name some pure object oriented languages.

5)What is a container class? What are the different types of container classes?

6)What will be the output of the following program:
#include
main()
{
printf("%x",-1<<4);
}

7)What are the major differences between C and C++?

8)What is an incomplete type?

9)What are printf and scanf, call by reference or call by value?

10)What is a const pointer?

11)When should a type cast be used and when should it not be used?

12)Which is the easiest sorting method?

13)Which is the quickest sorting method?

14)Which are the best sorting algorithms to sort a linked list?

15)What is a preprocessor and what will it do for a program?

16)How can a string be converted into a number?

17)How can a number be converted into a string?

18)What is a heap?

19)Why does n++ execute much faster than n+1?

20)What is modular Programming?

21)Which expression always returns true and which expression always returns false?

22)What is the difference between "malloc" and "calloc"?

23)Is C
a)A low level language
b)A middle level language
c)A high level language ?

24)Why doesn't C support function overloading?

25)What is the difference between global int & static int declaration?



1)To check for it, create two pointers,and set each 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 linked list\n");
}
}

2)Union x : 1101791232 21.500000
Union y : 100 d 0.000000

3)It is an object of some class whose purpose is to indicate that a real object of that class does not exist. One common use for a null object is a return value from a member function that is supposed to return an object with some specified properties but cannot find such an object.

4) Java,Smalltalk,Eiffel,Sather.

5)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.

6)fffffff0

7)C was the C++ predecessor. As it's name implies, a lot of C remains in C++. Although not actually being more powerful than C, C++ allows the programmer to more easily manage and operate with Objects, using an OOP (Object Oriented Programming) concept.

C++ allows the programmer to create classes, which are somewhat similar to C structures. However, to a class can be assigned methods, functions associated to it, of various prototypes, which can access and operate within the class, somewhat like C functions often operate on a supplied handler pointer.

Although it is possible to implement anything which C++ could implement in C, C++ aids to standarize a way in which objects are created and managed, whereas the C programmer who implements the same system has a lot of liberty on how to actually implement the internals, and style among programmers will vary a lot on the design choices made.

In C, some will prefer the handler-type, where a main function initializes a handler, and that handler can be supplied to other functions of the library as an object to operate on/through. Others will even want to have that handler link all the related function pointers within it which then must be called using a convention closer to C++.

To finish this discussion, C++ applications are generally slower at runtime, and are much slower to compile than C programs. The low-level infrastructure for C++ binary execution is also larger. For these reasons C is always commonly used even if C++ has alot of popularity, and will probably continue to be used in projects where size and speed are primary concerns, and portable code still required (assembly would be unsuitable then).

8)Incomplete types
refers to pointers in which there is non availability of the
implementation of the referenced location or it points to some location
whose value is not available for modification.

int *i=0x400 // i points to address 400
*i=0; //set the value of memory location pointed by i.

9)Printf : Call by value
Scanf : Call by reference

10)a const pointer means the pointer which represents the address of one value. so if you declare a pointer inside the function, it doesn't have scope outside the function. if it is also available to the outside function whenever we declare a pointer as const.

11)Type casting must be done wheneever the data type of the variable to which u r gonna assign some values is diff from the data type of the variable on the right side.

for instance;

float f;
int i = 10 , j = 5 ;

f = (float) ( i / j ) ;

f -------> left side variable.
i -------> right side variable.

but always make sure that the size of the var on the left is greater than that of the right. else there will be data loss.

A type cast should not be used to override a const or volatile declaration. Overriding these type modifiers can cause the program to fail to run correctly.
A type cast should not be used to turn a pointer to one type of structure or data type into another. In the
rare events in which this action is beneficial, using a union to hold the values makes the programmer.s
intentions clearer.

12)he answer is the standard library function qsort(). It.s the easiest sort by far for several reasons:
It is already written.
It is already debugged.
It has been optimized as much as possible (usually).
Void qsort(void *buf, size_t num, size_t size, int (*comp)(const void *ele1, const void *ele2));

13)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. Even in cases in which sorting speed is of the essence, there is no one answer. It depends on not only the size and nature of the data, but also the likely order. No algorithm is best in all cases.
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.

The Quick Sort
The quick sort algorithm is of the .divide and conquer. type. That means it works by reducing a sorting
problem into several easier sorting problems and solving each of them. A .dividing. value is chosen from the input data, and the data is partitioned into three sets: elements that belong before the dividing value, the value itself, and elements that come after the dividing value. The partitioning is performed by exchanging elements that are in the first set but belong in the third with elements that are in the third set but belong in the first Elements that are equal to the dividing element can be put in any of the three sets.the algorithm will still work properly.

The Merge Sort
The merge sort is a .divide and conquer. sort as well. It works by considering the data to be sorted as a
sequence of already-sorted lists (in the worst case, each list is one element long). Adjacent sorted lists are merged into larger sorted lists until there is a single sorted list containing all the elements. The merge sort is good at sorting lists and other data structures that are not in arrays, and it can be used to sort things that don.t fit into memory. It also can be implemented as a stable sort.

The Radix Sort
The radix sort takes a list of integers and puts each element on a smaller list, depending on the value of its least significant byte. Then the small lists are concatenated, and the process is repeated for each more significant byte until the list is sorted. The radix sort is simpler to implement on fixed-length data such as ints.

14)Both the merge sort and the radix sort are good sorting algorithms to use for linked lists.

15)The preprocessor is used to modify your program according to the preprocessor directives in your source code. Preprocessor directives (such as #define) give the preprocessor specific instructions on how to modify your source code. The preprocessor reads in all of your include files and the source code you are compiling and creates a preprocessed version of your source code. This preprocessed version has all of its macros and constant symbols replaced by their corresponding code and value assignments. If your source code contains any conditional preprocessor directives (such as #if), the preprocessor evaluates the condition and modifies your source code accordingly.
The C preprocessor is used to modify your program according to the preprocessor directives in your source code. A preprocessor directive is a statement (such as #define) that gives the preprocessor specific instructions on how to modify your source code. The preprocessor is invoked as the first part of your compiler program.s compilation step. It is usually hidden from the programmer because it is run automatically by the compiler.

16)The standard C library provides several functions for converting strings to numbers of all formats (integers, longs, floats, and so on) and vice versa.

The following functions can be used to convert strings to numbers:
Function Name Purpose
atof() Converts a string to a double-precision floating-point value.
atoi() Converts a string to an integer.
atol() Converts a string to a long integer.
strtod() Converts a string to a double-precision floating-point value and reports any .leftover. numbers that could not be converted.
strtol() Converts a string to a long integer and reports any .leftover. numbers that could not be converted.
strtoul() Converts a string to an unsigned long integer and reports any .leftover. numbers that could not be converted.

17)
The standard C library provides several functions for converting numbers of all formats (integers, longs, floats, and so on) to strings and vice versa

The following functions can be used to convert integers to strings:
Function Name Purpose
itoa() Converts an integer value to a string.
ltoa() Converts a long integer value to a string.
ultoa() Converts an unsigned long integer value to a string.

The following functions can be used to convert floating-point values to strings:
Function Name Purpose
ecvt() Converts a double-precision floating-point value to a string without an embedded decimal point.
fcvt() Same as ecvt(), but forces the precision to a specified number of digits.
gcvt() Converts a double-precision floating-point value to a string with an embedded decimal point.

18)The heap is where malloc(), calloc(), and realloc() get memory.
Getting memory from the heap is much slower than getting it from the stack. On the other hand, the heap
is much more flexible than the stack. Memory can be allocated at any time and deallocated in any order. Such
memory isn't deallocated automatically; you have to call free().
Recursive data structures are almost always implemented with memory from the heap. Strings often come
from there too, especially strings that could be very long at runtime. If you can keep data in a local variable (and allocate it from the stack), your code will run faster than if you put the data on the heap. Sometimes you can use a better algorithm if you use the heap.faster, or more robust, or more flexible. It's a tradeoff.
If memory is allocated from the heap, it.s available until the program ends. That's great if you remember to deallocate it when you.re done.

19)n++ takes more than one instruction, ++n is faster. n++ has to store n, increment the variable and return n, while ++n increment n and return without storing the previous value of n.

20) 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.

21)expression if (a=0) always return false
expression if (a=1) always return true

22)Malloc is dynamic memory allocation,it allocates the memory and initialize garbage value.Calloc is similar to malloc but only difference is initialize zero

23)A middle level language

24)Overloading is polymorphism which is one of the characteristics of Object oriented programming. C is not and object oriented language like C++ or Java. Therefore, no overloading, inheritance, etc.

25)Static int variable are accessed only inside the file where it is defined. Thus we can have same variable name in 2 files if the variable is defined as static. The scope of the variable is limited to the file in which it is defined.

On the other hand if the variable is not defined as static and defined globally then it can be accessed across the files. To access the variable which is global variable and declared and defined in file A, keyword "extern" is used in front of the variable in file B. This indicated to compiler while compiling that the variable is defined in some other file other than B and continues compiling and while linking the variable it search for the actual definition and links.



·  What is C language?
·  What is a pragma?
·  What is the use of logical operators and the conditional operators with example?
·  Define static variable?
·  Is it possible to declare static variable in header file?
·  What is difference between nul and null?
·  What is a function and its use?
·  What is recursion?
·  What is hashing?
·  What is null pointer?
·  Define array and a simple program with array?
·  What is the difference between printf() and scanf()?
·  What is difference between charachter arrays and strings?
·  What is the output of printf("%f") and printf("%d")?
·  What is the difference between run time error and compile time error?
·  What is calloc() and malloc() and its differences?
·  How to reduce the final size of executable?
·  What are the diffrent class storages in c?
·  Is it possible to execute code even after the program exists the main() function?
·  How can you determine the size allocation of memory?
·  What is c-preprocessor and its features?
·  What is object file?
·  How can you access objectfile?
·  What are strings?
·  What are standard library string functions?
·  How to find a given number is armstrong or not in "c"?
·  What is structural language and procedural language with differences?
·  What is the limitation of array of pointers to strings?
·  What is the difference between structure and union?
·  What is null macro?
·  What is the difference between null macro and null pointer?
·  What is data structure in c?
·  How can we check whether the contents of two structure variables are same or not?
·  What is file pointer and its working method?
·  What is void pointer?
·  How are pointer variables intialized?
·  Will you add pointers together? Explain it?
·  What is static function?
·  What is the purpose of the main( ) function?
·  How to write a program in c to print its own code?
·  Difference between linker and linkage?
·  What is meant by malloc function?
·  How do you use a pointer to a function?
·  Difference between pointer to function and function to pointer?
·  How to convert decimal to octal and hexadecimal in c?
·  What is a method?
·  What is built-in function?
·  What are the advantages of macro over a function?
·  How to print a statement with out using printf( ) in c?
·  What is the code to draw a three dimensional graph using c graphics?
·  How to create a relation circular program using c?
·  What is #line used for?
·  What is an abstract class?
·  What are the important features in c?
·  What is a modular programming

Followers