C and Interview Questions

40+ Best C Interview Questions

Posted in C, Interview Questions
40+ Best C Interview Questions

Table of Contents

Despite being almost 50 years old, C is still leading the programming scene from the forefront. The programming language not only has a wide variety of applications but is also the inspiration for a variety of modern, powerful programming languages, such as Java, C#, and Python.

Although there are very few job profiles these days that only require a high-level proficiency in C, most programming jobs necessitate for a good understanding of the fundamental concepts of the C programming language. So, many programming job interviews involve C-based questions.

Best C Interview Questions

If you’re preparing for a C-based interview, then you’ve landed at the right spot. Here are 40+ best C interview questions to help you get going for that job interview or to simply self-assess how good you’ve gotten with the general-purpose, procedural programming language:

Question: List some of the most important features of the C programming language.

Answer: C supports a plethora of great features. Most important among them are:

  • Extensibility - Capable of adopting new features.
  • Fast - Support for system programming allows faster compilation and execution compared to popular high-level languages like Java and Python.
  • Memory Management - Comes with built-in memory management for saving memory and optimizing memory use.
  • Middle-Level Programming Language - Binds the gap between a machine-level language and a high-level language. It can be used for system programming as well as application programming.
  • Platform independence/Portability - A C program written for one machine can run on other machines with little to no modifications.
  • Simplicity - Follows the structured approach. Facilitates breaking down a big, complex program into smaller, independent, and easy-to-manage modules (sub-programs).

Question: Please explain what is a header file in C? What will happen if we include a header file twice in a C program?

Answer: Header files store the definitions and set of rules governing different built-in functions of the C programming language. For instance, the printf() and scanf() functions are defined in the stdio.h header file.

Every header file contains a set of predefined functions, meant to make programming in C simpler. You need to include the specific header file in your C program to be able to use the functions defined in it. For example, you can’t use printf() and scanf() functions without including the stdio.h header file.

When a header file is included twice in a C program, the second one gets ignored. In actual, the #, called the include guard, preceding a header file ensures that it is included only once during the compilation process.

Question: Does a C program compile without the main() function succeeds in compiling? What about its execution?

Answer: Yes, a C program can be compiled even without adding the main() function. However, it will not execute without the main() function.

Question: Enumerate the basic data types in C?

Answer: There are 5 basic data types in C:

  1. int - Stores an integer number
  2. float - Stores a decimal number
  3. double - Stores a decimal number with the highest precision
  4. char - Stores a single character
  5. void - Stores no value

Question: What do you mean by reserved keywords? How many reserved keywords are there in C?

Answer: Words that are restricted for general use while writing a program, i.e., for using as a name for a variable, function, structure, etc. are called reserved keywords. Reserved keywords, also called reserved names, have special meanings, i.e., they are already defined for something. The C programming language has the following 32 reserved keywords:

  1. auto
  2. break
  3. case
  4. char
  5. const
  6. continue
  7. default
  8. do
  9. double
  10. else
  11. enum
  12. extern
  13. float
  14. for
  15. goto
  16. if
  17. int
  18. long
  19. register
  20. return
  21. short
  22. signed
  23. sizeof
  24. static
  25. struct
  26. switch
  27. typedef
  28. union
  29. unsigned
  30. void
  31. volatile
  32. while

Question: How are global variables different from static variables?

Answer: Global variables are variables with global scope, i.e., they are accessible throughout the program, unless shadowed. These variables are defined outside a function or code block. Static variables are variables allocated statically, i.e., their value can’t be changed. It is fixed for the entire run of a program. They can be defined outside as well as inside functions. Moreover, they can be accessed from anywhere inside the program.

Question: Please compare static memory allocation with dynamic memory allocation?

Answer: Following are the important differences between static and dynamic modes of memory allocation:

Memory increase:

In dynamic memory allocation, memory can be increased while executing the program. This is not the case; however, with the static memory allocation where the option of increasing memory during program execution is not available.

Memory requirement

Static memory allocation needs more memory space compared to dynamic memory allocation.

Used in

Arrays use static memory allocation while linked lists use dynamic memory allocation.

When does it happen?

Static memory allocation takes place at compile-time, while dynamic memory allocation happens at runtime.

Question: Can you explain memory leak in C? Why should it be addressed?

Answer: Memory leak happens when a memory created in a heap remains undeleted. This can lead to additional memory usage and, thus, affect the performance of a program. This is exactly why the issue of memory leak must be addressed.

Question: Please explain what do you understand by while(0) and while(1)?

Answer: while(0) means that the looping conditions will always be false, i.e., the code inside the while loop will not be executed. On the opposite, while(1) is an infinite loop. It runs continuously until coming across a break statement mentioned explicitly.

Note: Any non-zero integer inside the braces of the while loop will give an infinite loop. For example, while(-22) and while(24) will both yield an infinite loop.

Question: State the difference between prefix increment and postfix increment.

Answer: In prefix increment, the value of the variable is incremented before the program execution. The variable is incremented post the program execution in postfix increment.

++a <- Prefix increment

a++ <- Postfix increment

Question: Please explain the concept of Dangling Pointer in C? In how many ways can a pointer act as a Dangling Pointer?

Answer: A pointer that points to a memory location that is already deleted is called a dangling pointer. As per another definition, a dangling pointer is a pointer that points to a dereferenced memory location. A pointer acts as a dangling pointer in three cases:

  1. Deallocation of memory
  2. When the local variable isn’t static
  3. When the variable goes out of scope

Initially, the dangling pointer holds a valid memory address. Later, however, the held address is released.

The Dangling Pointer Problem -

When A pointer is pointing to a memory location, and a B pointer deletes the memory occupied by the A pointer, then the A pointer still points to the memory location, which is no longer available. This is called the dangling pointer problem.

Question: How is the null pointer different from a void pointer?

Answer: A pointer is initialized as NULL when its value isn’t known at the time of declaration. Generally, NULL pointers do not point to a valid location. Unlike NULL pointers, void pointers are general-purpose pointers that do not have any data type associated with them. Void pointers can contain the address of any type of variable. So, the data type that a void pointer points to can be anything.

#include <stdio.h>
#include <conio.h>
void main()
   int a = 22;
   int *notnullpointer = &a;
   int *nullpointer1;                                                       // Null because there is no initialization.
   int *nullpointer2 = 0;                                                // Null because initialized with 0.
   if (notnullpointer == 0) printf ("\nNot null pointer is null.");
   else printf ("\nNot null pointer is not null.");
   if (nullpointer1 == 0) printf ("\nNull pointer 1 is null.");
   else printf ("\nNull pointer 1 is not null.");
   if (nullpointer2 == 0) printf ("\nNull pointer 2 is null.");
   else printf ("\nNull pointer 2 is not null.");
   printf ("\nNot null pointer has stored the address %d.", &notnullpointer);
   printf ("\nNull pointer 1 has no valid address.");
   printf ("\nNull pointer 2 has no valid address.");


Not null pointer is not null.
Null pointer 1 is null.
Null pointer 2 is null.
Not null pointer has stored the address 2054804456.
Null pointer 1 has no valid address.
Null pointer 2 has no valid address.

Unlike NULL pointers, void pointers are general-purpose pointers that do not have any data type associated with them. Void pointers can contain the address of any type of variable. So, the data type that a void pointer points to can be anything. For example:

int x = 22;
char b = 'a';
void *vp = &x; // Here the void pointer is holding address of int x
vp = &b; // Here the void pointer is holding address of char b

Question: What do you understand by a pointer on a pointer in C?

Answer: A pointer on a pointer is a pointer containing the address of some other pointer. It is a form of multiple indirections. The first pointer contains the address of the second pointer, which points to the location of the actual value.

Question: Please explain calloc() and malloc() functions?

Answer: calloc() and malloc() are library functions used for allocating memory dynamically, i.e., the memory is allocated during runtime from the heap segment. In case of successful memory allocation, a pointer to the memory block is returned. In scenarios where allocation fails, the two functions return a NULL value. Important differences between the calloc() and malloc() functions are:

Accessing the content

If we try to access the content of memory while using the malloc() function, we can get either the segmentation fault error or garbage values. Doing the same will get 0 in the case of calloc() function.


The malloc() function allocates the memory and returns a pointer to the beginning of the block. It doesn’t initialize the allocated memory. The calloc() function, on the other end, not only allocates the memory but also initializes the allocated memory block to 0.

Number of arguments

Unlike the malloc() function, the calloc() function has two arguments:

  1. The number of blocks that are to be allocated
  2. The size of each memory block

Question: Compare arrays with pointers in the C programming language?

Answer: Following are the various differences between arrays and pointers in C:

  • Definition - An array is a form of data structure that stores multiple, homogeneous elements at contiguous memory locations. A pointer, however, is a variable that stores or points to the memory address of some other variable.
  • Initialization - It is possible to initialize arrays at the definition. Pointers, however, can’t be initialized at the definition.
  • Size - While a pointer can store the address of only a single variable, an array is capable of storing multiple elements. The total number of elements stored by arrays is determined by the array size.

Question: What are the important differences between a structure and a union in C?

Answer: Following are the three important differences between a structure and a union in the C programming language:

  • For the same data type, a structure requires more memory than a union.
  • Modifying the value of a member of a structure doesn’t affect other members. Doing the same in a union, however, results in affecting all the members of the union.
  • While only one element can be accessed at a time in the union, it is possible to access all elements of a structure simultaneously.

Check out the in-depth comparison of union vs. structure in C.

Question: How is pass by value different from a pass by reference?

Answer: Pass by value and pass by reference are also called call by value and call by reference, respectively. In the call by value, values are send/passed to the function as parameters. Pass by value is used when there is a requirement of not modifying the actual parameters. Address pertaining to the actual parameters are send/passed to the function in the call by reference. Pass by reference is used when there is a need for modifying the actual parameters. Changes made to the arguments in the called function are not reflected in the calling function in a pass by value. Opposite to this, changes made to the arguments in the called function are reflected in the calling function in a pass by reference.

Know about pass by reference in detail.

Question: Write a program to print Hello World! Without using the semicolon.

Answer: We can do so by using the if statement without the condition:

#include <stdio.h> 
#include <conio.h>
void main()
printf("Hello World!")

Output: Hello World!

Question: Is #include "stdio.h" correct? What’s the difference between using < > and " " for including the header file?

Answer: Yes, #include "stdio.h" is correct. The difference between using angle brackets (<>) and double quotes (" ") for including a header file is the way in which the compiler searches for the particular header file.

When we use angular brackets, the compiler searches for the header file only within the built-in include path. When, however, double quotes are used, the compiler searches for the header file first in the current working directory, and then in the built-in include path when not found in the current working directory.

Question: How will you resolve the scope of a global symbol?

Answer: We can use the extern storage specifier for resolving the scope of a global symbol. The extern keyword is used for extending the visibility or scope of variables and functions. As C functions are visible throughout the program by default, its use with function declaration or definition is not required.

Question: When do we use the register keyword?

Answer: The register storage specifier, i.e., the register keyword, is used for storing a variable in the machine register. This is typically used for heavily-used variables to the likes of a loop control variable. The main intent behind using the register keyword is to speed-up the program by minimizing variable access time.

Question: What do you understand by rvalue and ivalue?

Answer: The expression on the left of the assignment operator (=) is called an ivalue. An rvalue is an expression on the right side of the assignment operator, and it is assigned to an ivalue.

For instance,

int a = 25; 

int a is the ivalue in the above-mentioned example while 25 is the rvalue. While an ivalue persists beyond a single expression, the rvalue doesn’t persist beyond the expression using it.

Question: How are actual parameters different from formal parameters?

Answer: Actual parameters are the ones that are sent to the function at the calling end. Formal parameters, however, are the ones that are received during the function definition. A formal parameter is an identifier used in some function to stand for the value that is passed into the function by a caller. This actual value that is passed into the function by a caller is the actual parameter.

Formal parameters are bound to an actual value as long as their function is active. The formal parameters do not store any values when the function returns to its caller. For Example:

#include <stdio.h>
#include <conio.h>
int totalsum(int a, int b, int c)          // Formal parameters
   int total;
   total = a + b +c;
   return total;
int main()
   int sum;
   int a = 22;    int m = 24; int p = 28;      // Actual parameters
   sum = totalsum(a, m, p);
   printf ("The total sum is %d.", sum);


The total sum is 74.

Another major distinction between actual parameters and formal parameters is that while the latter are always variables, the former can be expressions, function calls, or even numbers. For example, in the above example, the following are also valid actual parameters (in the function call to totalsum):

sum = totalsum (10+15, 12*2, 56/2);  // Actual parameters are expressions.
sum = totalsum (a, (int) sqrt(576), p);  // One of the actual parameters is a function call.
sum = totalsum (22, 24, 28);         // Actual parameters are numbers.

Question: Please explain a self-referential structure.

Answer: A self-referential structure contains the same structure pointer variable as its element. In other words, it is a data structure in which the pointer points to the structure of the same data type. A self-referential structure is used in Graphs, Heaps, Linked Lists, Trees, et cetera.

Question: What do you understand by modular programming?

Answer: Modular approach to programming involves dividing an entire program into independent, interchangeable sub-programs, i.e., functions and modules for accomplishing the desired functionality. Each of the functions or modules involved in modular programming has everything required for executing a single aspect of the desired functionality of the entire program.

Question: Please explain tokens in C.

Answer: Tokens are the smallest, indivisible units of a C program with distinct meanings. Following are the various types of tokens in C:

  • Constants - Fixed values that can’t be changed during the program execution.
  • Identifiers - This refers to the name of the functions, variables, arrays, structures, etc.
  • Keywords/Reserved Names - Predefined words with special meanings that can’t be used as variable names.
  • Operators - Symbols that tell the C compiler to perform specific logical, mathematical, or relational operations.
  • Special Characters - All characters excluding the alphabets and digits are special characters.

Question: What do you understand by the C preprocessor?

Answer: The C compiler automatically uses the C preprocessor for transforming the program, i.e., performing certain things before the actual compilation starts. The C preprocessor is a macro processor because it enables defining brief abbreviations for longer constructs, called macros.

Question: Please explain bit fields in C.

Answer: Bit fields are variables defined with a predefined width (size) inside a structure. The general syntax of a bit field is:

struct {
   type [member_name] : width ;

type - Integer type, such as int and signed int, determining how a bit field’s value is interpreted.

member-name - Name of the bit field.

width - Number of bits in the bit field. Must be less than or equal to the bit width of the specified integer type.

Question: What are command line arguments in C?

Answer: Command line arguments are the parameters passed to the main() function in a C program during program execution. The typical use of command-line arguments is to fulfill the need of controlling the program from outside.

Observe the following code,

#include <stdio.h>
#include <conio.h>
void main (int numofargs, char *args[])

In the main() function, the count of the command line arguments is given out by the numofargs. The operating system updates it automatically. The args is a pointer array holding pointers of char type. These point to the (command line) arguments supplied to the program.

Note: When no command-line arguments are passed to the program, the numofargs parameter will be 1. args[0] holds the name of the C program, args[1] points to the first command-line argument, and args[n] points to the last command-line argument.

Question: Briefly explain the various file opening modes in C?

Answer: Files are opened in a C program using the fopen() function. It is defined in the stdio.h header file. The general syntax of fopen() is:

ptr = fopen (“file address”, “mode”); 

It is possible to open a file in the following 12 different opening modes in a C program:

  1. r - Opens a file for reading.
  2. rb - Opens a file for reading in binary mode.
  3. w - Opens a file for writing.
  4. wb - Opens a file for writing in binary mode.
  5. a - Opens a file for appending i.e. adding data to the end of the file.
  6. ab - Opens a file for appending in binary mode.
  7. r+ - Opens a file for both reading and writing.
  8. rb+ - Opens a file for both reading and writing in binary mode.
  9. w+ - Opens a file for both reading and writing.
  10. wb+ - Opens a file for both reading and writing in binary mode.
  11. a+ - Opens a file for both reading and appending.
  12. ab+ - Opens a file for both reading and appending in binary mode.

Leave for r, rb, r+, and rb+ modes; the file is created if it is not found when tried to open in other modes. The fopen() returns NULL if the file doesn’t exist in these 4 file opening modes.

Question: Compare local variables and global variables.

Answer: Following are the various differences between the local and global variables:

1. Declaration

Local variables are declared inside a function, while global variables are the variables declared outside the functions.

2. Life

The life of a local variable is tied with the function block where it is declared. The variable is destroyed when the function exits. Global variables remain for the entire lifetime of the program.

3. Scope

The scope of a local variable is confined to the function or code block where it is declared. Global variables have global scope, i.e., they are available throughout the program.

4. Storage

Unless specified explicitly, local variables are stored in a stack. The storage for a global variable is decided by the compiler itself.

Question: What is a static variable? Why do we need it?

Answer: A variable whose value is fixed and can’t be changed during program execution, i.e., it retains the stored value between multiple function calls, is called a static variable. The keyword static represents a static variable. A static variable is initially initialized to 0. When the value is updated, it gets assigned to the static variable. It is initialized only once in the memory heap. We use a static variable to:

  • Reduce memory consumption
  • Sharing a common value across all functions

Question: Please explain recursion in C.

Answer: Recursion is the process when a function calls itself, directly or indirectly. Such a function is called a recursive function. There are two phases involved with a recursive function:

  1. Winding phase - It starts when the recursive function calls itself and ends once the condition is reached.
  2. Unwinding phase - Starts when the condition is reached, i.e., when the winding phase ends, and ends when the control returns to the original call.

Question: Can you write a C program to check whether an entered number is prime or not?


#include <stdio.h> 
#include <conio.h>
void main()
int a, i, b=0, flag=0;
printf("Enter a number: ");
printf("The number you entered is not a prime number!");
printf("The number you entered is a prime number.");

Sample output:

Enter a number: 22

The number you entered is not a prime number!

Question: Please write a program for finding out the factorial of a given number using recursion.


#include <stdio.h>
#include <conio.h>
long factorial (int n)
   if (n==0)
   return 1;
   return (n * factorial(n-1)); //recursion
void main()
   int number;
   long fact;
   printf("Enter a number: ");
   scanf("%d", &number);
   fact = factorial(number);
   printf("The factorial of %d is %ld!\n", number, fact);

Sample output:

Enter a number: 5
The factorial of 5 is 120!

Question: What is a far pointer in C?

Answer: A far pointer is a 32-bit pointer capable of accessing all the 16 segments, i.e., the whole residence memory of RAM. It can access information outside the computer memory in a given segment. To use the far pointer, it is required to:

  • Allocate the sector register to store data address in the segment, and
  • Store another sector register within the most recent sector

Question: Please explain the auto keyword in C.

Answer: auto is the default storage class of all the variables declared inside a code block or function. Hence, local variables can also be referred to as automatic or auto variables. If no value is stored in an auto variable, then it gets a garbage value. Auto variables are called so because these variables allocate and deallocate memory upon entering and exiting the code block or function in which they are declared, respectively. Typically, there is no need to mention the auto keyword explicitly.

Question: Why do we use the sprintf() function?

Answer: The sprintf() function is called string print. We use the sprintf() function to store the output on a character buffer specified in the function, i.e., transferring data to the buffer. The general syntax of the function is:

int sprintf
(char *str, const char*string,.....);

The sprintf() function returns the total number of characters present in the string. Here is an example demonstrating the use of the sprintf() function: 

#include <stdio.h>
#include <conio.h>
int main()
   char buffer[25];
   sprintf(buffer, "This string is now stored in the buffer."); /* Using the sprintf() function for storing the string in the buffer.*/
   printf("%s", buffer);
   int n = sprintf(buffer, "This string is now stored in the buffer.");
   printf("\nThe total number of characters in the string is %d.", n); // The sprintf() function returns the total number of characters in the stored string.
   return 0;

Output: This string is now stored in the buffer.

The total number of characters in the string is 40.

Question: Tell the difference between getch() and getche() functions.

Answer: Both getch() and getche() functions are used for reading a single character from the keyboard. The difference between the two, however, lies in terms of displaying the output. The getche() function displays the data, the entered character, on the output screen while the getch() function doesn’t. Use Alt+F5 to see the entered character.

Question: Explain the difference between near, far, and huge pointers.

Answer: Any virtual address has the selector and offset. While a near pointer doesn’t have explicit selector, far and huge pointers do. Performing pointer arithmetic on the far pointer doesn’t result in modifying the selector. It does, however, in the case of a huge pointer.

Question: Please define typecasting.

Answer: Typecasting is the process of converting one data type into another. It is of two types:

1. Implicit type casting - Also known as an automatic conversion, implicit type conversion is performed automatically by the C compiler, i.e., it doesn’t require a casting operator. For example:

#include <stdio.h>
#include <conio.h>
void main ()
   int x = 22;
   float b = x; //implicit type conversion
   printf(“%f”, b);

Output: 22.000000

2. Explicit type casting - Unlike implicit type conversion, explicit type casting is performed by the programmer. A type casting operator is used for telling the compiler to convert (cast) one data type into another. For example:

#include <stdio.h>
#include <conio.h>
void main ()
   float x = 22.22;
   int b = (int) x; //explicit type conversion
   printf(“%d”, b);

Output: 22

Here, (int) is the typecasting operator.

Question: Write a C program to check whether an entered number is palindrome or not.

Answer: A palindrome number is one that reads the same, whether forwards or backward. 

void main()   
   int n, r, sum=0, temp;
   printf("Enter a number: ");    
   printf("It is a palindrome number!");
   printf("It is not a palindrome number!");

Sample output:

Enter a number: 12321
It is a palindrome number!


That sums up our list of the 40+ best C interview questions. How many answers you already knew? Let us know via comments. Is there some wrong/outdated information in the article? We’d be much thankful for sharing the same with us and making the article better. Want to step-up your C skills? Or wish to learn C? Check out these best C tutorials today!

People are also reading:

Vijay Singh

Vijay Singh

My name is Vijay Singh Khatri, and I enjoy meeting new people and finding ways to help them have an uplifting experience. I have had a variety of customer service opportunities, through which I was able to have fewer returned products and increased repeat customers, when compared with co-workers. Currently working with hackr.io View all posts by the Author

Leave a comment

Your email will not be published