VOOZH about

URL: https://www.geeksforgeeks.org/interview-prep/miscellaneous-interview-questions-c-programming/

⇱ C Miscellaneous Interview Questions - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Miscellaneous Interview Questions

Last Updated : 15 Nov, 2025

C programming isn't just about printf() and for loops. For technical interviews, especially in system-level, embedded, or product-based roles, you're expected to master the language’s advanced concepts: low-level memory operations, file access, type definitions, and even how programs interact with the OS.

1. What is recursion in C and write a recursive code to compute factorial?

Recursion is the process of making the function call itself directly or indirectly. A recursive function solves a particular problem by calling a copy of itself and solving smaller subproblems that sum up the original problems. Recursion helps to reduce the length of code and make it more understandable. The recursive function uses a LIFO ( Last In First Out ) structure like a stack. Every recursive call in the program requires extra space in the stack memory.

For more information, refer to the article - Recursion

👁 Factorial of a Number

2. What is a stack overflow in recursion? How can it be avoided?

Stack overflow occurs when too many recursive calls exceed the function call stack limit. Avoid by:

  • Adding base cases correctly
  • Limiting recursion depth
  • Using iterative approach (tail recursion or loops)

Example:

void recurse() {
recurse(); // No base case -> infinite calls -> crash
}

3. Implement structure & union in C?

  • Structure: The structure is a keyword that is used to create user-defined data types. The structure allows storing multiple types of data in a single unit. The structure members can only be accessed through the structure variable.
  • Union: A union is a user-defined data type that allows users to store multiple types of data in a single memory location. A union does not occupy the sum of the memory of all members. It holds the memory of the largest member only. Since the union allocates one common space for all the members we can access only a single variable at a time.

Below is the C program to implement Union:

4. How do you read a whole text file line by line in C?

To read a file line by line, you can use the fgets() function, which reads until a newline character or the specified buffer size is reached. This is useful for processing text files without loading the entire file into memory.

How it works:

  • fgets() reads at most sizeof(line): 1 characters from the file, stopping at a newline or EOF.
  • Loop continues until EOF (end of file) is reached.
  • Always check if fopen() succeeds before reading.

5. What is an r- value and l-value?

  • An l-value (locator value) refers to an object that has an identifiable memory location (i.e., an address). A modifiable l-value can appear on the left-hand side of an assignment.
  • An r-value (read value) is a temporary value that does not persist beyond the expression and usually does not have a distinct memory address. R-values can appear only on the right-hand side of an assignment.

You cannot take the address of an r-value like &20 or assign to it directly like 20 = val.

6. What is the sleep() function?

The sleep() function in C suspends execution of the current thread for the specified number of seconds. It takes an unsigned int as an argument. For sub-second delays, use usleep() (microseconds) or nanosleep().

7. What are enumerations?

In C, enumerations (or enums) are user-defined data types. Enumerations allow integral constants to be named, which makes a program easier to read and maintain. For example, the days of the week can be defined as an enumeration and can be used anywhere in the program.

enum enumeration_name{constant1, constant2, ... };


Output
2

In the above example, we declared “day” as the variable, and the value of “Wed” is allocated to day, which is 2. So as a result, 2 is printed.

For more information, refer to the article - Enumeration (or enum) in C

8: What is a volatile keyword?

Volatile keyword is used to prevent the compiler from optimization because their values can’t be changed by code that is outside the scope of current code at any time. The System always reads the current value of a volatile object from the memory location rather than keeping its value in a temporary register at the point it is requested, even if previous instruction is asked for the value from the same object. 

Without volatile, the compiler might optimize the while(flag == 0) loop into an infinite loop, assuming flag never changes, because it thinks no code inside main() changes it.

9. Write a C program to print the Fibonacci series using recursion and without using recursion.

👁 Fibonacci Numbers
Fibonacci Numbers

Output
Fibonacci Series with the help of Recursion:
0 1 1 2 3 5 8 
Fibonacci Series without Using Recursion:
0 1 1 2 3 5 8 

10. Write a C program to check whether a number is prime or not.

👁 Number Prime or not
Number Prime or not

11. How is source code different from object code?

Source CodeObject Code
Source code is generated by the programmer.object code is generated by a compiler or another translator.
High-level code which is human-understandable.Low-level code is not human-understandable.
Source code can be easily modified and contains less number of statements than object code.Object code cannot be modified and contains more statements than source code.
Source code can be changed over time and is not system specific.Object code can be modified and is system specific.
Source code is less close to the machine and is input to the compiler or any other translator.Object code is more close to the machine and is the output of the compiler or any other translator.
Language translators like compilers, assemblers, and interpreters are used to translate source code to object code.Object code is machine code so it does not require any translation.

12. What will be the output of this code and why?

Explanation:

  • The function increment takes a pointer to an integer.
  • Inside the function, (*p)++ increments the value at the memory location pointed to by p.
  • Since main passes the address of x, the actual variable x is incremented.
  • Hence, output will be: 6

13. Explain modifiers.

Modifiers are keywords that are used to change the meaning of basic data types in C language. They specify the amount of memory that is to be allocated to the variable. There are five data type modifiers in the C programming language:

  • long
  • short
  • signed
  • unsigned
  • long long

Output
Factorial of 5 is 120

14. Write a program to check an Armstrong number.

An Armstrong number (also called a narcissistic number) is a number where the sum of its digits raised to the power of the number of digits equals the number itself.

For example: 153 is an Armstrong number because:
13+53+33=1+125+27=1531^3 + 5^3 + 3^3 = 1 + 125 + 27 = 15313+53+33=1+125+27=153

Steps:

  1. Count the number of digits.
  2. Extract each digit and raise it to the power of the digit count.
  3. Add the results.
  4. If the sum equals the original number, it's an Armstrong number.


15. Write a program to reverse a given number.

To reverse a number:

  1. Initialize rev = 0.
  2. Extract the last digit using num % 10.
  3. Add it to rev after shifting its digits left: rev = rev * 10 + digit.
  4. Remove the last digit from num using num / 10.
  5. Repeat until num becomes 0.

This way, digits are added in reverse order to rev.

16. Mention file operations in C.

In C programming Basic File Handling Techniques provide the basic functionalities that programmers can perform against the system. 

👁 File Operations in C
File Operations in C

17. Write a Program to check whether a linked list is circular or not.

A circular linked list is one where the last node points back to the first node, forming a loop. To check if a linked list is circular:

  1. Start from the head node.
  2. Traverse the list using a loop.
  3. If during traversal you come back to the head, it’s circular.
  4. If you reach NULL, it’s not circular.

Alternatively, Floyd’s Cycle Detection Algorithm (Tortoise and Hare) can be used to detect loops more efficiently.

For more information, refer to the article - Circular Linked List

18. Write a program to Merge two sorted linked lists.

To merge two sorted linked lists into a single sorted list:

  1. Use two pointers, one for each list (l1, l2).
  2. Compare current nodes of both lists
  3. Append the smaller node to the result list.
  4. Move that list's pointer forward.
  5. Repeat until one list ends.
  6. Append the remaining nodes of the other list.
  7. Return the head of the merged sorted list.

Output
Merged Linked List is: 
2 3 5 10 15 20 

19. What is the difference between fread() and fscanf() in C?

fread() is used for binary file reading, and it reads raw bytes.

fread(buffer, sizeof(char), 100, fp); // Binary

  • fscanf() is used for text file reading and parses input according to a format string.

fscanf(fp, "%s", buffer); // Text

20. How do you open a file in append mode? What happens if the file doesn't exist?

Use "a" mode in fopen():

FILE *fp = fopen("data.txt", "a");

  • If the file exists: It is opened and the file pointer is moved to the end.
  • If the file doesn’t exist: A new file is created.

21. What is typedef in C?

In C programming, typedef is a keyword that defines an alias for an existing type. Whether it is an integer variable, function parameter, or structure declaration, typedef will shorten the name.

Syntax:

typedef <existing-type> <alias-name>

  • existing type is already given a name. 
  • alias name is the new name for the existing variable.

22. How do command-line arguments work in C? Give an example.

In C, command-line arguments allow you to pass values (inputs) to the main() function when a program is run from the command line or terminal.

The main() function has two special parameters:

int main(int argc, char *argv[])

  • argc: (Argument Count) – Total number of arguments passed, including the program name.
  • argv: (Argument Vector) – An array of strings (character pointers), where: argv[0] is the name of the program & argv[1] to argv[argc-1] are the actual command-line arguments

If the executable is named a.out, run it like this in terminal:

./a.out hello world 123

23. What happens if you forget to close a file in C?

  • File descriptors remain open until the program exits.
  • May lead to memory leaks, file lock issues, or OS-level file handle limits.
  • Always use fclose(fp); to properly release the resource.
Comment