50 Frequently Asked C Interview Questions For C Programming in 2021

50 Frequently Asked C Interview Questions For C Programming in 2021

Without a doubt, C Programming Interview Questions have become an essential part of the interview process in most MNCs. Throughout this article, I will focus mainly on the most frequently asked and most current questions asked during interviews. Additionally, you will find a mix of Basic and Advanced C Programming Interview Questions in this article.

C Programming Interview Questions & Answers

1. What are different storage class specifiers in C?

Answer: The different storage class specifiers in C - auto, register, static, extern.

2. What's the value of the expression 5["abxdef"]?

Answer: The value is f.

Reason: "abxdef" is an array mentioned in the string, whereas the expression is equal to "abxdef"[5]. What makes an inside-out expression equivalent? Since a[b] is equivalent to *(a + b) that is equivalent to *(b + a) which is equivalent to b[a].

3. What is C language?

Answer: C is a mid-level and procedural programming language. It is a procedural programming language, also called structured programming, that involves breaking large programs into smaller modules and using structured code for each one. This method minimizes error and misinterpretation.

Dennis Ritchie founded it in 1972 at the Bell Laboratories of AT&T (American Telephone & Telegraph) in the U.S.A.

4. Why is C called a mid-level programming language?

Answer: C is called a middle-level language because it blends the features of both machine-level language and high-level language. Apart from System Programming (for writing operating systems), you can also use it for application programming (creating customer billing systems).

5. What is a memory leak? Why should you avoid it?

Answer: A memory leak happens when we get some memory on the heap and forget to delete it. We should avoid memory leaks because these are especially problematic issues for programs like daemons and servers, which by definition never terminate.

/* Function with memory leak */
#include <stdlib.h>

void f()
{
    int* ptr = (int*)malloc(sizeof(int));

    /* Do some work */

    return; /* Return without freeing ptr*/
}

6. What are the key features of the C language?

Answer: The key features of the C language are:

  • Extensible: C is an extensible language, allowing it to accept newly added features in the future.
  • Fast Speed: As it uses a powerful set of data types and operators, it is extremely fast.
  • Mid Level: It combines the low-level language with the features of the high-level language.
  • Memory Management: The C language has an inbuilt memory management function for saving memory and making our programs more efficient.
  • Structured: C is a structured language since it is broken up into segments.
  • Portable: C is highly portable, meaning that once the program is written, it can be run on any system with little or no modification.
  • Simple: C is a simple language because it follows the structured approach, i.e., a program is broken into parts.

7. Explain the difference between const char* p and char const* p?

Answer: Const char and char const indicate that the pointer can point to a constant char and that the value of the char pointed by this pointer cannot be altered. We can, however, change the value of the pointer since it is not constant and can point to another constant char.

char* const indicates that the pointer can point to a char and that the value of the char pointed by this pointer can be changed. However, we cannot change the value of the pointer because it is now constant and cannot point to another character.

const char* const indicates that the pointer can point to a constant char and the value of the int pointed to by this pointer cannot be changed. Furthermore, the value of the pointer is now constant and it cannot point to another constant character.

8. What are the advantages of Macro over function?

Answer: The main advantage macro offers over an actual function is speed. It saves a lot of time since no time is spent transferring control from one function to another, and control is always with the callee function. Nevertheless, one downside is the size of the compiled binary is large, but once compiled the program comparatively runs faster.

9. What are the basic Datatypes supported in C Programming Language?

Answer: C Language datatypes can be broadly classified into four categories:

  1. Basic Datatypes
  2. Derived Datatypes
  3. Enumerated Datatypes
  4. Void Datatypes

The Basic Datatypes supported in C Language are as follows:

Datatype NameDatatype SizeDatatype Range
short1 byte-128 to 127
unsigned short1 byte0 to 255
char1 byte-128 to 127
unsigned char1 byte0 to 255
int2 bytes-32,768 to 32,767
unsigned int2 bytes0 to 65,535
long4 bytes-2,147,483,648 to 2,147,483,647
unsigned long4 bytes0 to 4,294,967,295
float4 bytes3.4E-38 to 3.4E+38
double8 bytes1.7E-308 to 1.7E+308
long double10 bytes3.4E-4932 to 1.1E+4932

10. Specify different types of decision control statements?

Answer: In a program, all statements are executed one by one from top to bottom. Depending on the condition, control statements are used to execute/transfer control from one part of the program to another.

If-else statement

  • normal if-else statement.
  • Else-if statement
  • nested if-else statement.

switch statement

11. Why n++ execute faster than n+1 ?

Answer: Since n++ is a unary operation, it requires just one variable. Meanwhile, n = n + 1 is a binary operation that adds overhead to take more time (also binary operation: n += 1). However, on modern platforms, it depends on few things such as processor architecture, C compiler, usage in your code, and other factors such as hardware problems.

12. What is the use of the function in C?

Answer: The use of function in C is as follows:

  • C functions can be called any number of times from anywhere in our program.
  • C functions are used to prevent rewriting the same code repeatedly in our program.
  • C functions provide the reusability concept, i.e., it breaks the big task into smaller tasks to make the C program easier to understand.
  • When a program is divided into functions, any part of the program can easily be tracked.

13. What is the difference between struct and union in C?

Answer: A struct is a collection of complex data structures stored in a block of memory, where each member gets their own memory location for ease of access

Whereas in the union, all the member variables are stored at a similar location on the memory due to which assigning a value to a member variable will change the value of all other members.

/* struct & union definations*/
struct bar {
    int a;    // we can use a & b both simultaneously
    char b;
}    bar;

union foo {
    int a;    // we can't use both a and b simultaneously
    char b;
}    foo;

/* using struc and union variables*/

struct bar y;
y.a = 3;    // OK to use
y.b = 'c'; // OK to use

union foo x;
x.a = 3; // OK
x.b = 'c'; // NOl this affects the value of x.a!

14. What is recursion in C?

Answer: Recursion occurs when a function calls itself. Similarly, Recursive functions are those functions that call themselves. Recursive function comes in two phases:

  1. Winding phase
  2. Unwinding phase

15. What is the scope of a variable? How are variables scoped in C?

Answer: The scope of a variable is the part of the program where the variable may directly be accessible. All identifiers are lexically (or statically) scoped in the C language.

16. What is an array in C?

Answer: An array is a collection of similar types of elements. It has a contiguous memory location. It makes the code optimized, easy to traverse, and easy to sort. After an array is declared, its size and type cannot be changed.

There are two types of arrays:

  • One-dimensional array: One-dimensional array is an array that stores the elements one after the other.

Syntax:

data_type array_name[size];
  • Multidimensional array: Multidimensional array is an array that contains more than one array.

Syntax:

data_type array_name[size];

17. How will you print “Hello World” without a semicolon?

Answer:

#include <stdio.h>
int main(void)
{
    if (printf("Hello World")) {
    }
}

18. When should we use pointers in a C program?

Answer: Use pointers in a C program in the following cases:

  1. To get the address of a variable
  2. For achieving pass by reference in C: Pointers allow different functions to share and modify their local variables.
  3. To pass large structures so that a complete copy of the structure can be avoided.
  4. To implement “linked” data structures like linked lists and binary trees.

19. What are local static variables? What is their use?

Answer: In programming, a local static variable is a variable whose lifetime doesn't end with the function call where it is declared. It continues for the lifetime of the complete program. All calls to the function share the same copy of local static variables. It is possible to count the number of times a function is invoked using static variables. Additionally, static variables have a default value of 0.

20. What is the difference between declaration and definition of a variable/function?

Answer: Variable/function declarations simply declare that the variables/functions exist somewhere in the program, but no memory is allocated for them. However, the declaration of a variable/function is crucial. This is the variable/function type. Thus, when a variable is declared, the program knows the data type of that variable. With function declaration, the program knows what are the arguments to that function, their data types, the order of arguments, and the return type of the function. So that’s all about the declaration.

Coming to the definition, when we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function. Therefore, we can think of the definition as a superset of declaration. (or declaration as a subset of definition).

21. What is a Dangling pointer?

Answer: Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers occur when an object is deleted or deallocated without changing the value of the pointer, so the pointer points to the memory location of the deallocated memory. An example is given below.

// EXAMPLE 
int* ptr = (int*)malloc(sizeof(int));
..........................free(ptr);

// ptr is a dangling pointer now and operations like following are invalid
*ptr = 10; // or printf("%d", *ptr);

22. What is a NULL pointer?

Answer: NULL is used to denote that the pointer doesn’t point to a valid location. If we do not know their value at the time of declaration, we should initialize pointers as NULL. As well, we should make a pointer NULL when the memory pointed by it is deallocated in the middle of a program.

23. What is pass by reference in functions?

Answer: In Pass by reference, the callee receives the address and copies the address of an argument into the formal parameter. Callee function uses the address to access the actual argument (to do some manipulation). Likewise, the callee function's changes in the value addressed at the passed address will be visible to the caller function.

24. What is call by reference in functions?

Answer: Call by reference in functions occurs when a caller function makes a function call ignoring the addresses of actual parameters passed in. In incall by reference, the operation performed on formal parameters affects the value of actual parameters because all the operations performed on the value stored in the address of actual parameters.

25. What is a far pointer in C?

Answer: Far pointer is a pointer that can access all 16 segments of RAM (the whole residence memory). It is a 32-bit pointer that accesses information outside the memory in a given section.

26. Differentiate between calloc() and malloc()

Answer: Both calloc() and malloc() are memory dynamic memory allocating functions. There is only one difference between them: calloc() loads all assigned memory locations with 0 values but malloc() does not.

27. What is the purpose of printf() and scanf() in C Program?

Answer: printf() is used to print the values on the screen. On the other hand, scanf() is used for scanning values. Both printing and scanning require an appropriate datatype format specifier. As an example,

  • %d: It is a datatype format specifier used to print and scan an integer value.
  • %s: It is a datatype format specifier used to print and scan a string.
  • %c: It is a datatype format specifier used to display and scan a character value.
  • %f: It is a datatype format specifier used to display and scan a float value.

28. Can I use int datatype to store 32768 value?

Answer: No, the Integer datatype only supports the range between -32768 and 32767. If you exceed the range, it will not be stored.

However, you can use float or long int.

29. How is a Function declared in C Language?

Answer: Here:

1. return_type function_name(formal parameter list)
2. {
3.       Function_Body;
4. }

30. What is Dynamic memory allocation in C? Name the dynamic allocation functions.

Answer: In Dynamic Memory Allocation, memory is allocated to the program and its variables in run-time. Dynamic Memory Allocation uses three functions to allocate memory and one function to free used memory.

To allow dynamic memory allocation in C programming, C provides four library functions defined under the header file stdlib.h>. They are:

  • malloc()

Syntax:

ptr = (cast-type*) malloc(byte-size) // allocating the memory using malloc() function.
  • calloc()

Syntax:

ptr = (cast-type*)calloc(n, element-size);// allocating the memory using calloc() function.
  • free()

Syntax:

free(ptr); // memory is released using free() function.
  • realloc()

Syntax:

ptr = realloc(ptr, newsize); // updating the memory size using realloc() function.

31. Why is it usually a bad idea to use gets()? Suggest a workaround.

Answer: The standard input library gets() reads user input until it encounters a new line character. As a result, it does not check to see if the variable size provided by the user is within the maximum size of the data type, which makes the system vulnerable to buffer overflow and the input being written into memory where it shouldn't have been.

Therefore, we use fgets() to achieve the same with a restricted range of input.

32. What is the use of a semicolon (;) at the end of every program statement?

Answer: The compiler reads (or parses) the entire code and breaks it down into a set of instructions(or statements), in which the semicolon in C acts as a boundary between the two sets of instructions.

33. What is the difference between malloc() and calloc()?

Answer:

calloc()malloc()
It consists of two arguments.It consists of only one argument.
It returns a pointer pointing to the allocated memory.It returns a pointer pointing to the allocated memory.
It initializes the content of the memory to zero.It does not initialize the content of memory, so it carries the garbage value.
The malloc() function allocates a single block of requested memory.The calloc() function allocates multiple blocks of requested memory.

34. Write a simple example of a structure in C Language.

Answer: A structure is defined as a user-defined data type that is intended to store multiple members of different data types into a single unit. A structure will consume memory equal to the summation of all the data members.

1. struct employee
2. {
3.    char name[10];
4.    int age;
5. }e1;
6. int main()
7. {
8.    printf("Enter the name");
9.    scanf("%s",e1.name);
10.    printf("n");
11.    printf("Enter the age");
12.    scanf("%d",&amp;e1.age);
13.    printf("n");
14.    printf("Name and age of the employee: %s,%d",e1.name,e1.age);
15.    return 0;
16. }

35. Differentiate between getch() and getche().

Answer: In both cases, the function reads characters from the keyboard. The only difference between them is that:

  • getch(): reads characters from the keyboard but it does not use any buffers. Hence, data is not displayed on the screen.

  • getche(): reads characters from the keyboard and uses a buffer. Hence, data is displayed on the screen.

36. Explain toupper() with an example.

Answer: toupper() is a function designed to convert lowercase characters into upper case. For Example:

1. #include&lt;stdio.h&gt;
2. #include&lt;ctype.h&gt;
3. int main()
4. {
5.    char c;
6.    c=a;
7.    printf("%c after conversions  %c", c, toupper(c));
8.    c=B;
9.    printf("%c after conversions  %c", c, toupper(c));

Output:

a after conversions A

B after conversions B

37. Can I create a customized Head File in C language?

Answer: Yes, you can create a customized head file in the C language. It is possible to create a new header file. Create a file with function prototypes that you want to use in the program. Put the file in the ‘#include’ section in its name.

38. Can we compile a program without main() function?

Answer: 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");    
}

39. What is the difference between declaring a header file with < > and ” “?

Answer: If the Header File is declared using < > then the compiler searches for the header file in the Built-in Path. If the Header File is declared using ” ” then the compiler will search for the Header File in the current working directory and if not found then it searches for the file in other locations.

40. Which variable can be used to access Union data members if the Union variable is declared as a pointer variable?

Answer: Arrow Operator( -> ) can be used to access the data members of a Union if the Union Variable is declared as a pointer variable.

41. When should we use the register storage specifier?

Answer: We use Register Storage Specifier if a certain variable is used very often. It helps the compiler to locate the variable as the variable will be declared in one of the CPU registers.

42. Which statement is efficient and why? x=x+1; or x++; ?

Answer: x++; is the more efficient statement as it is just a single instruction to the compiler while the other is not.

43. What are the different storage class specifiers in C?

Answer: The following are the different storage specifiers available in C:

  • auto
  • register
  • static
  • extern

44. What is typecasting?

Answer: The process of converting one data type into another is known as typecasting. If we want to store the floating type value to an int type, then we will convert the data type into another data type explicitly.

Syntax:

1. (type_name) expression;

45. Write a program to swap two numbers without using the third variable.

Answer:

1.#include&lt;stdio.h&gt;      
2.#include&lt;conio.h&gt;      
3. main()      
4. {      
5.    int a=10, b=20;    
6.     clrscr();       
7.     printf("Before swapping a=%d b=%d",a,b);        
8.    a=a+b;       
9.   b=a-b;    
10.     a=a-b;      
11.    printf("nAfter swapping a=%d b=%d",a,b);      
12.     getch();      
13. }

Output: Before swapping a=10 b=20

After swapping a=20 b=10

46. Check whether the number is EVEN or ODD, without using any arithmetic or relational operators

#include<stdio.h>
int main()
{
   int x;
   printf("Enter a number: ");
   scanf("%d", &x);
   (x&1)?printf("Odd"):printf("Even");
   return 0;
}

Answer: We can use the bitwise and(&) operator to quickly check the number is odd or even.

47. When is the "void" keyword used in a function?

Answer: "Void" is a data type that represents no data at all. A function that returns nothing is the most obvious use for this:

void PrintHello() 
{ 
    printf("Hello\n"); 
    return;        // the function does "return", but no value is returned 
}

In the above example, we’ve declared a function, and all functions have a return type. In this case, we’ve said the return type is “void”, and that means, “no data at all” is returned.

Another use for the void keyword is a void pointer. At the time of variable definition, a void pointer points to the memory location where the data type is undefined. In addition, you can define a function of return type void* or void pointer, meaning that at compile time we don't know what it will return. For example:

void MyMemCopy(void* dst, const void* src, int numBytes) 
{ 
    char* dst_c = reinterpret_cast<char*>(dst); 
    const char* src_c = reinterpret_cast<const char*>(src); 
    for (int i = 0; i < numBytes; ++i) 
        dst_c[i] = src_c[i]; 
}

48. Write a program to print Fibonacci series using recursion?

Answer:

#include<stdio.h>      
#include<conio.h>      
void printFibonacci(int n) // function to calculate the fibonacci series of a given number.  
{      
static int n1=0,n2=1,n3;    // declaration of static variables.  
    if(n>0){      
         n3 = n1 + n2;      
         n1 = n2;      
        n2 = n3;      
         printf("%d ",n3);      
         printFibonacci(n-1);    //calling the function recursively.  
    }      
}      
void main(){      
    int n;      
    clrscr();      
    printf("Enter the number of elements: ");      
    scanf("%d",&n);      
    printf("Fibonacci Series: ");      
    printf("%d %d ",0,1);      
    printFibonacci(n-2);//n-2 because 2 numbers are already printed      
    getch();      
}

49. Write a program to print Fibonacci series without using recursion?

Answer:

#include<stdio.h>    
#include<conio.h>    
void main()    
{    
 int n1=0,n2=1,n3,i,number;    
 clrscr();    
 printf("Enter the number of elements:");    
 scanf("%d",&number);    
 printf("\n%d %d",n1,n2);//printing 0 and 1    

 for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed    
 {    
  n3=n1+n2;    
  printf(" %d",n3);    
  n1=n2;    
  n2=n3;    
 }    
getch();    
}

50. What are header files and what are their uses in C programming?

Answer: In C header files must have the extension as .h, which contains function definitions, data type definitions, macro, etc. The header is useful to import the above definitions to the source code using the #include directive.

For example, if your source code needs to take input from the user do some manipulation and print the output on the terminal, it should have stdio.h file included as #include , with which we can take input using scanf() do some manipulation and print using print().


If you have made it this far, then certainly you are willing to learn more about C. Here are some more resources related to C programming that we think will be useful to you.

Did you find this article valuable?

Support Yash Tiwari by becoming a sponsor. Any amount is appreciated!