C-Language Interview Questions
1 .
Distinguish between malloc() & calloc() memory allocation.
Both allocates memory from heap area/dynamic memory. By default calloc fills the allocated memory with 0’s.
2 .
What are the valid places for the keyword break to appear.
Break can appear only with in the looping control and switch statement. The purpose of the break is to bring the control out from the said blocks.
3 .
What is the purpose of extern storage specifier?
Used to resolve the scope of global symbol.
 
Example :  
main() {
   extern int i;
   Printf(“%d”,i);
}

int i = 20;
4 .
What is a dangling pointer?
A pointer initially holding valid address, but later the held address is released or freed. Then such a pointer is called as dangling pointer.
5 .
What is a self-referential structure?
A structure containing the same structure pointer variable as its element is called as self-referential structure.
6 .
What is recursion in C?
When a function calls itself, and this process is known as recursion. The function that calls itself is known as a recursive function.
 
Recursive function comes in two phases :
 
* Winding phase
* Unwinding phase

Winding phase : When the recursive function calls itself, and this phase ends when the condition is reached.
 
Unwinding phase : Unwinding phase starts when the condition is reached, and the control returns to the original call.
 
Example of recursion : 
 
#include <stdio.h>  
int calculate_fact(int);  
int main()  
{  
 int n=5,f;  
 f=calculate_fact(n); // calling a function  
 printf("factorial of a number is %d",f);  
  return 0;  
}  
int calculate_fact(int a)  
{  
  if(a==1)  
  {  
      return 1;  
  }  
  else  
  return a*calculate_fact(a-1); //calling a function recursively.  
   }  
 
Output :
 
factorial of a number is 120
7 .
What is the usage of the pointer in C?
Accessing array elements : Pointers are used in traversing through an array of integers and strings. The string is an array of characters which is terminated by a null character '\0'.

Dynamic memory allocation : Pointers are used in allocation and deallocation of memory during the execution of a program.

Call by Reference : The pointers are used to pass a reference of a variable to other function.

Data Structures like a tree, graph, linked list, etc,. : The pointers are used to construct different data structures like tree, graph, linked list, etc.
8 .
What is static memory allocation?
* In case of static memory allocation, memory is allocated at compile time, and memory can't be increased while executing the program. It is used in the array.

* The lifetime of a variable in static memory is the lifetime of a program.

* The static memory is allocated using static keyword.

* The static memory is implemented using stacks or heap.

* The pointer is required to access the variable present in the static memory.

* The static memory is faster than dynamic memory.

* In static memory, more memory space is required to store the variable.

For example :  
int a[10];  ‚Äč
The above example creates an array of integer type, and the size of an array is fixed, i.e., 10.
9 .
What is an auto keyword in C?
In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value.
10 .
Can we compile a program without main() function?
Yes, we can compile, but it can't be executed.
 
But, if we use #define, we can compile and run a C program without using the main() function. For example:
 
#include<stdio.h>    
#define start main    
void start() {    
   printf("Hello");    
}