C Interview Questions and Answers for Freshers

1) What is the difference between #include <file> and #include “file” ?We use # include to include a file. The difference between two ways of file inclusion lies in the order in which preprocessor searches for the file specified. When the preprocessor encounters #include<file> statement, it looks for the file specified in the angled brackets in the default location (Path defined in INCLUDE environment variable of the system).When # include followed by file name in double quotation marks is encountered by the preprocessor, it looks for the file in the current directory. If the file is not found in the current directory, it will look for the file in the default location. 2) Can #include handle other file formats than .h? Yes. Irrespective of the file type, Preprocessor will do its job and will include any file like test.z. 3) What is a void pointer? A void pointer is a special pointer type which can point to any data type without letting the compiler know. It helps to pass a pointer to some function of any type which can be decided at run time. In the following example input parameter a and b can be both integer and string.Void PointerVoid(int type, void *a, void *b, void *c) { if(type == 1)/* int*/ *c = ((int) *a) + ((int)*b); else /*string*/ sprintf((char*)c,”%s%s”,(char*)a,(char*b)); } 4) What is the value of NULL? The value of NULL is 0 or (void*)0. Whenever NULL has to be compared with some variable or assigned to a variable, depending upon the type of that variable, the value of NULL will be decided. 5) Can the size of an array be declared at runtime? No. The size of an array must be stated at the time of compilation. Alternate way is to use dynamic allocation by calloc or malloc. 6) what is the difference between malloc() and calloc()?

Allocates a region of memory large enough to hold “n” elements of “size” bytes each.Allocates “size” bytes of memory.
Calloc initializes the whole memory with 0Malloc does not change the existing memory. So it returns a memory chunk with garbage value.

7) What is the heap in memory? The heap is where malloc(), calloc(), and realloc() get memory. The allocation of memory from the heap is much slower than the stack. But, the heap is much more flexible about memory allocation than the stack. Memory can be allocated and deallocated in any time and order. This heap memory isn’t deallocated by itself, method free() has to be called in order to do so. 8) What will be the output of the following code snippet? float num1 = 6 / 4; float num2 = 6 / 4.0; printf(“6/4 == %f or %f\n”, num1, num2); Output will be : 6/4 == 1.000000 or 1. 500000. This is a case of operator promotion. The variable num1 is set to “6/4”. Because, both 3 and 4 are integers. So integer division is performed on them and the result is the integer 0. The variable num2 is set to “6/4.0”. Because 4.0 is a float, the number 6 is converted to a float as well, and the result will be floating value 1.5. 9) What happens if you free a pointer twice? It is really dangerous to free the same memory twice. If the memory has not been reallocated in between, it will generate a “double free” error, since the memory location has already been freed. 10) How does free() method know about how much memory to release?There’s no concrete way. Most systems, keeps a track of each memory block as linked lists. When memory is allocated, all the blocks that are given to that particular call are put into a linked list and the size, block number and serial number are written in the head node. There is no assurance, though. But in some way or other, the system keeps track of each block to know the size of each allocated portion of the heap.

Leave a Reply