Top 50+ C Interview Questions – The 2024 Guide
C is a general-purpose, procedural programming language developed in the early 1970s. It is simple, easy to learn, and efficient for computer programs. While C has been superseded by more modern languages, like Java and Python, it remains popular due to its versatility and capability of running on different types of hardware platforms, including embedded systems. In this blog, we will look at some C interview questions for freshers, intermediates, and experienced individuals.
Basic C Interview Questions for Freshers
The following are some simple C interview questions and answers:
1. What is C programming language?
C is a mid-level programming language, also known as the structured programming language. It divides large programs into smaller modules, where each module employs structured code.
2. How is C related to other programming languages?
As the majority of compilers and JVMs are written in C, languages created after C have substantially drawn from it, including C++, Python, Rust, Javascript, etc. It presents fundamental ideas to these languages, including arrays, functions, and file management.
3. Why is C called a mid-level programming language?
Due to its ability to combine low-level and high-level programming, C is referred to as a mid-level programming language. This is because programs written in C are converted into assembly code but are machine-independent, which is a high-level language tendency.
4. When was the C language developed? Name its prominent features.
The C language was developed in 1972 at Bell Laboratories of AT&T by Dennis Ritchie. Some of its prominent features are:
- It is easy and effective.
- It is machine-independent or portable.
- It is a programming language with structure and dynamic memory management.
- It can be expanded and its library is functionally rich.
- In C, pointers can be used.
5. What is the difference between local and global variables in C?
Some of the key differences between local and global variables in C are as follows:
Local Variables | Global Variables |
They are declared inside a block or function. | They are declared outside a block or function. |
They are stored inside a stack, unless a different storage location is specified by the programmer. | The compiler decides the storage location of global variables. |
They function until the block or function ends. | They function until the execution of the program. |
To use them in other functions, parameter passing is required. | They do not require any parameter passing and are visible throughout the program. |
6. What is an array?
A collection of similar-type components is known as an array. It has an uninterrupted memory space. It optimizes the code and makes it simple to sort and navigate through.
7. What is a token?
The individual elements of a program are called tokens. The following 6 types of tokens are available in C:
- Identifiers
- Keywords
- Constants
- Operators
- Special Characters
- Strings
8. What is the use of printf() and scanf() functions?
The printf() and scanf() functions are in-built library functions used for input and output purposes in the C language. The printf() function is used to print the output on the display and the scanf() is used to read formatted data from the keyboard.
9. Explain some data type format specifiers.
- %d: It is used for printing and scanning an integer value.
- %s: It is used for printing and scanning a string.
- %c: It is used for displaying and scanning a character value.
- %f: It is used to display and scan a float value.
10. What is a pointer and NULL pointer in C.
A pointer is a variable that points to a value’s address. It speeds up performance and optimizes the code. A NULL pointer is a pointer that does not point to any address of value other than NULL. Any pointer that receives a value of ‘0’ is converted to a null pointer.
11. What are some of the uses of pointers in C?
The pointers are used in C language for the following purposes:
- Save memory space and execution time.
- To access array elements.
- To pass arguments by reference.
- To implement data structures.
- For dynamic memory allocation.
- For system-level programming that requires memory addresses.
- To return multiple values.
12. What is a built-in function?
The built-in functionality, usually referred to as library functions, is a feature offered by the system that helps developers complete a number of frequently carried out predefined operations. Some of the commonly used built-in functions are scanf(), printf(), strcpy, strlwr, strcmp, etc.
13. What is a preprocessor?
A preprocessor is a software program that processes a source file before sending it to be compiled. It is also known as macro processor as it allows you to define macros or brief abbreviations for longer constructs. It also allows conditional compilation, line control, and inclusion of header files.
14. What is the difference between calloc() and malloc()?
calloc() and malloc() are dynamic memory allocating functions. The sole distinction between them is that malloc() does not load a value into any of the designated memory addresses, but calloc() does.
15. Can a C program be compiled or executed in the absence of a main()?
Although the program won’t run, it will be compiled. Any C program must have main() in order to run because this function acts as the entry point of the program where the execution begins. By default, the return type of main() function is int.
16. Can we use int datatype to store the 32768 value?
No, the integer data type will support the range between -32768 and 32767. Any value exceeding that will not be stored.
17. What is a dangling pointer variable in C?
The term “dangling pointer variable” refers to a pointer variable that has been erased but it still refers to the same position in memory.
18. What is pointer to pointer in C?
A pointer can also be used in C to store another pointer’s address. The first pointer contains the address of a variable, whereas the second pointer contains the address of the first pointer.
19. What are enumerations?
Enumeration is a user-defined data type, often known as enum in C. It is made up of constant integrals or integers with user-assigned names. You can learn more about enumeration through this in-depth C programming course.
Intermediate-Level C Programming Interview Questions
The following are some C language interview questions and answers for intermediate candidates:
20. How is a function defined in C language?
The function in C language is declared as:
return_type function_name(formal parameter list)
{
Function_Body;
}
21. What is the difference between getc(), getch(), getche(), and getchar()?
The following is the difference between these four functions in C:
getc(): It reads a single character from the input. If it succeeds, it returns an integer value. Otherwise, it returns end-of-file (EOF).
getch(): It reads a single character and does not use any buffer. The entered character is returned immediately without the need to press enter. It is a non-standard function and is mostly used by MS-DOS compilers.
getche(): It reads a character from the keyboard and displays it on the output screen. It does not wait for the enter key.
getchar(): It can read a single character from a standard input stream and return it. You can find this basic function in the stdio.h header file.
22. What is typecasting?
The typecasting process is the change of one data type into another. This conversion is done either manually or automatically. The programmer can manually convert the data type, while the compiler does the automatic conversion.
23. What is dynamic memory allocation?
The process of assigning memory to a program and its variables during execution is known as dynamic memory allocation. Three functions are used in the dynamic memory allocation process to allocate memory and one function is used to release memory that has been used.
24. What are r-value and i-value?
A data value kept in memory at a specific location is referred to as an “r-value”. A value cannot be assigned to an expression that has an r-value. Hence, this expression can only occur on the right side of the assignment operator (=).
The term “i-value” describes a memory address used to identify an object. Either the left or right side of the assignment operator (=) contains the i-value. In many cases, i-value is used as an identification.
25. What is the difference between struct and union in C?
A struct is a collection of intricate data structures kept together in memory and given independent memory locations so that they can all be accessed simultaneously. In contrast, all of the member variables in a union are kept in the same location in memory, which means that changing the value of one member would also modify the values of all the other members.
26. What is a simple example of structure in C language?
Structure or struct in C language is a user-defined data type used to group different variables together in one place. Here is a simple example of structure in C.
#include <stdio.h>
struct employee {
char name[10];
int age;
} e1;
int main() {
printf("Enter the name: ");
scanf("%s", e1.name);
printf("\n");
printf("Enter the age: ");
scanf("%d", &e1.age);
printf("\n");
printf("Name and age of the employee: %s, %d", e1.name, e1.age);
return 0;
}
The output of the code will depend on the values you input during runtime. For example, if you enter name as John and age as 25, the result will be as follows:
Name and age of the employee: John, 25
27. What is Call by reference?
Call by reference is the term used when the calling function calls a function without using the addresses of the given parameters. Because all actions in call by reference are conducted on the value stored in the address of real parameters, each operation made on formal parameters has an impact on the value of actual parameters.
28. What is Pass by reference in C?
The callee receives the address in Pass by reference and copies the address of an argument into the formal parameter. The address is used by the callee function to retrieve the actual argument and perform manipulations. The caller function will also be aware of any modifications made to the value referenced at the given address by the callee function.
29. Explain toupper() with an example.
toupper() is a function designed to convert lowercase words/characters into uppercase. Here is an example of this function:
#include <stdio.h>
#include <ctype.h>
int main() {
char c;
c = 'a';
printf("%c after conversion: %c\n", c, toupper(c));
c = 'B';
printf("%c after conversion: %c\n", c, toupper(c));
return 0;
}
The following is the output for the above program:
a after conversion: A
B after conversion: B
The toupper() function does not modify the characters that are not in lowercase. So ‘B’ remains ‘B’ and only ‘a’ changes to ‘A’.
30. What is a memory leak? How can it be avoided?
Memory leaks occur when a program allots dynamic memory to a program but forgets to release or destroy the memory once the code has run its course. To avoid memory leaks, memory allocated on the heap should always be cleared when it is no longer needed.
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr;
int n, i, sum = 0;
n = 5;
printf("Enter the number of elements: %d\n", n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
} else {
printf("Memory successfully allocated using malloc.\n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
printf("\n");
}
free(ptr); // Freeing the allocated memory
return 0;
}
//Output
Enter the number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,
31. What is typedef?
typedef provides an alias name to the existing complex type definition. With typedef, you can simply create an alias for any type. Whether it is a simple integer to complex function pointer or structure declaration, typedef will shorten your code.
32. What is the difference between ‘g’ and “g” in C?
Single-quoted variables in C are identified as the character and double-quoted variables are identified as a string. The string (double-quoted) variables end with a null terminator that makes it a 2-character array.
33. What is the difference between declaring a header file with < > and ” “?
The compiler looks for the header file in the built-in path if the header file is defined using <>. If the header file is specified with the character ” “, the compiler will first look in the current working directory for the file before moving on to other locations if it cannot be found.
34. When is the register storage specifier used?
If a particular variable is utilized frequently, we use the register storage specifier. Since the variable will be declared in one of the CPU registers, this helps the compiler in finding the variable.
35. Write a program to swap two numbers without using the third variable.
Here is a program to swap two numbers without using the third variable:
#include <stdio.h>
#include <conio.h>
int main() {
int a = 10, b = 20;
clrscr();
printf("Before swapping a=%d b=%d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping a=%d b=%d\n", a, b);
getch();
return 0;
}
Output:
Before swapping a=10 b=20
After swapping a=20 b=10
36. Which structure is used to link the program and the operating system?
The operating system and the program are joined by the file structure. The file is defined by the “stdio.h” header file (standard input/output header file). It includes details about the file, including its size and placement in memory. It also includes a character pointer that directs the user to the currently opened character. When a file is opened, the connection is made between the program and the operating system.
37. What is a near and far pointer?
A near pointer is used to hold addresses with a maximum size of 16 bits. It is no longer used because it lets you access only 64kb of data at a time, which is seen as insufficient.
A 32-bit pointer is regarded as a far pointer. It can make use of the current segment to access data kept outside of the computer’s memory.
38. What are the two types of loops used in C programming?
Loops in C programming are used for repeated execution of a block of statements. The statement is repeated and executed ‘n’ number of times until the given condition is met. There are two types of loops in C programming. These are:
- Entry-Controlled Loops: Here, the test condition is checked before entering the main body of the loop. Examples: For and While loops.
- Exit-Controlled Loops: Here, the test condition is checked at the end of the loop body. Example: do-while loop.
39. What is the difference between source code and object code?
Some of the key differences between source code and object code are:
Source Code | Object Code |
A programmer generates the source code. | A compiler or another translator generates the object code. |
It can be easily modified. | It cannot be modified. |
It acts as the input for the compiler or translator. | It is the output from the compiler or translator. |
It is a high-level code and humans can easily understand it. | It is low-level code and humans cannot easily understand it. |
C Interview Questions for Experienced Candidates
Here are some C interview questions and answers for experienced candidates:
40. How to remove duplicates in an array?
Here is a program that takes an array of integers as input, removes the duplicate elements, and prints the resulting array.
#include <stdio.h>
int main() {
int n, a[100], b[100], calc = 0, i, j, count;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter %d integers: ", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n; i++) {
for (j = 0; j < calc; j++) {
if (a[i] == b[j])
break;
}
if (j == calc) {
b[calc] = a[i];
calc++;
}
}
printf("Array obtained after removing duplicate elements: ");
for (i = 0; i < calc; i++) {
printf("%d ", b[i]);
}
return 0;
}
Suppose you enter the following values during runtime:
Enter the number of elements in the array: 8
Enter 8 integers: 1 2 3 4 2 3 5 6
You will get the following output:
Array obtained after removing duplicate elements: 1 2 3 4 5 6
41. What is a bubble sort algorithm in C? Explain with a sample program.
Bubble sort is a sorting algorithm in C language that is commonly used to sort elements in a list or array. It compares adjacent elements and swaps them if they are in the wrong order. Refer to this example for a better understanding of the bubble sort algorithm:
#include <stdio.h>
int main() {
int array[100], n, i, j, swap;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d Numbers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
swap = array[j];
array[j] = array[j + 1];
array[j + 1] = swap;
}
}
}
printf("Sorted Array:\n");
for (i = 0; i < n; i++) {
printf("%d\n", array[i]);
}
return 0;
}
The output of the code depends on the values you put during runtime. Here is what the output can look like:
Enter number of elements: 5
Enter 5 Numbers:
8 3 5 1 2
Sorted Array:
1
2
3
5
8
42. What are reserved keywords?
Each keyword in C programming is related to a specific task in a program. These are called reserved keywords. They have a defined meaning and you cannot use them for other purposes than already defined. C language has 32 keywords. Some of these are int, auto, switch, else, if, etc.
43. Can a program be compiled without the main() function?
Yes, you can compile a program without the main() function. Instead, you can use macro, which is a small code written to make the code readable by removing repetitive expressions.
#include<stdio.h>
#define abc main
int abc ()
{
printf("Hello World ");
return 0;
}
The output for the above-given program is:
Hello World
44. How do you override a macro?
The #ifdef and #undef preprocessors can be used to override a declared macro.
45. What are the limitations of scanf() and how is it avoided?
The scanf() function cannot be used to insert a multi-word string into a single variable. The gets() method is used to prevent this.
46. Write a program to check whether a number is in binary format or not.
You can check whether a number is in binary format or not with the following program.
#include <stdio.h>
int main() {
int num;
printf("Please enter a number: ");
scanf("%d", &num);
while (num > 0) {
int j = num % 10;
if (j != 0 && j != 1) {
printf("The number is not binary\n");
break;
}
num = num / 10;
if (num == 0) {
printf("The number is binary\n");
}
}
return 0;
}
The output of this program will vary depending on the number you enter. For example, if you enter the number 1010, the output will be:
Please enter a number: 1010
The number is binary
If you enter the number 1234, the output will be:
Please enter a number: 1234
The number is not binary
47. Write a C program to find out a sum of numbers using recursion.
Recursion in C language is a process in which the program repeats a certain section of code in a similar way. When a program allows you to call a function inside the same function, it is called a recursive call of the function. Here is an example of how this function is used to find out a sum of numbers:
#include<stdio.h>
#include<conio.h>
int sumOfDigits(int num, int sum) {
int rem;
sum = sum + (num % 10);
rem = num / 10;
if (rem > 0) {
return sumOfDigits(rem, sum);
}
return sum;
}
int main() {
int num;
printf("Please enter a number: ");
scanf("%d", &num);
printf("Sum of digits of the number = %d", sumOfDigits(num, 0));
getch();
return 0;
}
The output will depend on the number you input during runtime. For example, if you enter the number ‘123’, the output will be:
Please enter a number: 123
Sum of digits of the number = 6
48. What is the use of a semicolon (;) at the end of a program?
The semicolon in C serves as a border between two sets of instructions. It is a key factor in how the compiler analyses (or parses) the complete code and separates it into a series of instructions (or statements).
49. What is the difference between macros and functions?
Macros | Functions |
It is preprocessed. | It is compiled. |
Length of code is increased. | Code length remains the same. |
Execution is faster. | Execution is comparatively slower. |
Useful when a short section of code needs to be used repeatedly across a program. | Useful when a lengthy piece of code needs to be repeated. |
50. What is a dynamic data structure?
A dynamic data structure (DDS) is an arrangement or grouping of data in memory that can expand or contract in size. It gives a programmer complete control over how much memory is used.
DDS can allocate or deallocate the unused memory according to the requirement. Dynamic memory allocation can be done on both stack and heap as opposed to the static memory allocation, which is done only on the stack. Examples of dynamic data structures are queues, linked lists, stacks, and trees.
51. Write a program to write two numbers without using the addition operator.
Here is a program to add two numbers without using the addition operator:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x, y;
printf("Enter two number: ");
scanf("%d %d",&x,&y);
// method 1
printf("%d\n", x-(-y));
// method 2
printf("%d\n", -(-x-y));
// method 3
printf("%d\n", abs(-x-y));
// method 4
printf("%d", x-(~y)-1);
return 0;
}
If we enter values ‘5’ and ‘7’, the output of the above program will be:
Enter two numbers: 5 7
Method 1: 12
Method 2: 12
Method 3: 12
Method 4: 12
52. Write a program to find the nth Fibonacci number.
The following is the code to calculate nth Fibonacci number using a recursive approach. It defines a function ‘fib()’ that takes an integer ‘n’ as input and returns the corresponding Fibonacci number.
#include <stdio.h>
// Function to find the nth Fibonacci number
int fib(int n) {
if (n <= 1) {
return n;
}
int a = 0, b = 1, temp;
for (int i = 2; i <= n; i++) {
temp = a + b;
a = b;
b = temp;
}
return b;
}
int main() {
int n = 8;
printf("nth Fibonacci number is %d", fib(n));
return 0;
}
The following is the output for the above code:
nth Fibonacci number is 21
The output indicates that the 8th Fibonacci number is 21. It is based on the Fibonacci sequence F(n) = F(n-1) + F(n-2) with base values F(0) = 0 and F(1) = 1.
Conclusion
The C interview questions listed in this blog can be a valuable tool for job seekers. They can help you improve your problem-solving abilities and further understand the concepts on a deeper level. A thorough understanding of the key concepts, preparation, and practice will make it extremely simple for you to ace an interview.
Do you think we missed out on any crucial questions here? Let us know in the comments below. For more information about C and how it differs from C++, check out this blog on the differences between C and C++.