• Articles
  • Tutorials
  • Interview Questions

Top C Interview Questions and Answers 2025

CTA

C is a mid-level programming language. It is called mid-level because it can use the features of both the high-level and low-level languages as it can be used for System programming as well as for Application Programming. It was created by Dennis Ritchie in 1972 at Bell Laboratories of AT&T. Below is the list of the top 30 C Interview Questions asked in most of the Interviews.

Frequently asked C Interview Questions and Answers:

Q1. List the data types supported in the C Language.
Q2. Explain the working of printf() and scanf() functions in C Programming language?
Q3. What is the use of volatile keyword?
Q4. What are Reserved Keywords in C?
Q5. What are differences between sizeof operator and strlen function?
Q6. What are the different storage class specifiers in C Language?
Q7. When does the compiler not implicitly generate the address of the first element of an array?
Q8. What is the difference between deep copy and shallow copy in C?
Q9. What is an lvalue?
Q10. Explain the difference between Type Casting and Type Conversion in C with examples?

Basic C Programming Interview Questions for Freshers

1. List the data types supported in the C Language.

List the data typesThe data types supported in C Programming Language are:

int, float, double, char, void.

  • int is used to store integers.

For example:

int var = 10;
  • float is used to store floating/decimal numbers.

For example:

float weight = 10.5;
  • char is used to store characters.

For example:

char Grade = ‘A’;
  • double is used to store double precision floating point numbers.

For example:

double pi = 3.14159265359;
  • void is used for functions that don’t return any value.

For example:

void function_name()
{
return ;
}

2. Explain the working of printf() and scanf() functions in C Programming language?

printf() is used to print the value or display the output on the screen.

printf("Hello, Intellipaat !");

scanf() is used to take input from the user.

 
int intellipaat_marks; 
scanf("%d", &intellipaat_marks);

3. Difference between = and == operator in C programming.

= operator is used to assign values.

int var = 10;

== operator is used to compare two values.

if(var1 == var2)
{printf(“%d”, var1);

4. What are Reserved Keywords in C?

Keywords in C

Reserved keywords are the special keywords that can not be used as a variable name.

For example: int, return, for, void, if, else, switch, etc.

5. What is the use of static variables in C?

static variables in C

Static variables are initialized only once and retain their previous values which were assigned or initialized in the previous scope.

void counter() {
    static int count = 0;
    count++;
    printf("%d\n", count);
}
int main() {
    counter(); // Output: 1
    counter(); // Output: 2
    return 0;
}

6. What do you mean by the scope of the variable?

scope of the variable

The scope of a variable means the section of the code in which the variable is accessible or valid.

int globalVar = 10; // Global scope
void example() {
int localVar = 5; // Local scope
}

7. Explain the difference between #include "..." and #include <...> header files?

#include "..." is used to include user-defined header files.
#include "myHeader.h"
#include <...> is used to include standard library header files.
#include <stdio.h>

8. What are the different storage class specifiers in C Language?

storage class specifiers in C

There are 4 storage class specifiers in C Language i.e. auto, register, static and extern.

  • Auto is used as Default storage class for local variables.
  • Register is used to store variables in the CPU register for faster access.
  • static is used to retain the value of the variables between function calls.
  • extern is used to declare a global variable or function in another file.

9. What is a structure?

A structure is a user-defined data type to combine or merge multiple data types together.

struct Person {
char name[50];
int age;
};
struct Person person1;

10. What is a UNION?

A Union is similar to a Structure but in structure the variables don’t share memory whereas in union the multiple variables with different data types use shared memory.

union Data {
int intVal;
float floatVal;
char charVal;
};
union Data data;

11. What is the difference between macro and functions?

Macros are preprocessor directives written before the main function which means these statements are executed first then the main function is executed. It is defined using #define keyword.

#include <stdio.h>
// Defining a macro
#define SQUARE(x) (x * x)

int main() {
    int num = 5;
    printf("Square of %d is: %d\n", num, SQUARE(num)); // Output: 25
    return 0;
}
Functions are the blocks of code which are executed during the run-time.
#include <stdio.h>
// Defining a function
int square(int x) {
    return x * x;
}
int main() {
    int num = 5;
    printf("Square of %d is: %d\n", num, square(num)); // Output: 25
   return 0;
}

12. What is the difference between deep copy and shallow copy in C?

deep copy and shallow copy in C

In a shallow copy, if the object contains pointers or references to other data structures, then it will only copy the current pointer, not the data it is pointing to. It is more memory efficient but the data is shared in the shallow copy.

In Deep copy, it copies the pointer along with the data it is referring to. It uses more memory as the data is completely copied.

13. Write a program to print "Hello-World" without using a semicolon.

#include <stdio.h>
int main() {
    if (printf("Hello-World\n")) { }
    return 0;
}

14. How to convert a string to a number in C?

To convert a string into a number, atoi() function is used.

#include <stdlib.h>
int main()
{
    char str[] = "456";
    int num = atoi(str); // Converts string to integer
    printf("%d", num);   // Output: 456
    return 0;
}

15. How to convert a number to a string in C?

To convert a number into a string, sprintf() can be used:

#include <stdio.h>
int main() {
   int num = 456;       // Number to convert
   char str[10];        // Buffer to store the string
 // Convert the number to a string using sprintf
 sprintf(str, "%d", num);

// Print the string
printf("%s", str);   // Output: 456
    return 0;
}

16. What is the difference between malloc() and calloc() in the C programming language?

malloc() and calloc()

Here are a few differences between malloc() and calloc()

Malloc() is used to dynamically allocate the memory but doesn’t initialize it.
int* ptr = (int*)malloc(4 * sizeof(int));

Calloc() is used to dynamically allocate the memory and initialize it with 0.
int* ptr = (int*)calloc(4, sizeof(int));

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

An object that has a specified memory location and is written on the left side of the assignment is known as L-value.

int var = 5; (var is a l-value)

An object that doesn’t have a specified memory location and is written on the right side of the assignment is known as R-value.

int var = var1 + 5; (var1+5 is a r-value)

18. What is the difference between a null pointer and a void pointer?

A pointer that points to nothing is NULL Pointer.

For eg-

int* ptr = NULL;

A pointer that can point to any data type is Void Pointer.

For eg-

void* ptr;

19. What is the difference between const char* p and char const* p?

There is no difference in const char* p and char const* p. These statements means p is a pointer to a constant character, meaning the character cannot be modified through p.

20. What is the difference between getc(), getchar(), getch(), and getche() in C?

getc() is used to read a single character from a file or standard input(stdin) or any input stream.

Syntax:

int getc(FILE *stream);

getchar() is used to read a single character from the standard input

Syntax:

int getchar(void);

getch() also reads a single character from the keyboard but it does not display the character on the screen.

Syntax:

int getch();

getche() function reads a single character from the keyboard and displays the character immediately on the output screen.

Syntax:

int getche(void);

21. Why is it not preferred to use gets()?

gets() is used to read a complete line of input until a new line is encountered which can lead to overflow and characters overwrite at adjacent memory locations as the size of the input doesn’t matter in the case of gets().

char buffer[10];

gets(buffer); // Unsafe: No limit on input size, buffer overflow can occur

22. How to generate random numbers in C ?

Random numbers are generated using the rand() function. Below is the code to generate a random number

#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Random number: %d\n", rand());
return 0;
}

Below is the code to generate random numbers in a range:

#include <stdio.h>
#include <stdlib.h>
int main() {
int min = 10, max = 50;
int random_number = (rand() % (max - min + 1)) + min;
printf("Random number between %d and %d: %d\n", min, max, random_number);
return 0;
}

23. What is a memory leak in C?

What is a memory leak in C

When the allocated memory is not freed, memory leak occurs. To avoid memory leak, we can use free() function to release the memory.

int* ptr = (int*)malloc(10 * sizeof(int));
free(ptr); // Frees the allocated memory

24. What is the difference between call by value and call by reference in C?

When the copies of variables are passed through a function and no changes are done in the values of original variables then it is known as Call by Value.

void func(int x) {
x = 5; // Changes do not affect the original variable
}

When the addresses of the variables are passed in the function to make changes in the values of original variables then it is known as Call by Reference.

void func(int* x) {
*x = 10; // Changes affect the original variable
}

25. Explain the difference between Type Casting and Type Conversion in C with examples?

Type Conversion in C with examples

Typecasting is done to convert a data type into another data type. It is an explicit conversion as it is done by the user.

float a = 6.5;
int b = (int)a; // b is now 6

Type Conversion is also done to convert a data type into another data type but it is an implicit conversion as it is done by the compiler itself.

int x = 5;
float y = x; // Implicitly converts int to float

26. Can we compile a program without a main() function in C?

Yes, a program can be compiled without a main() function but can’t be executed.

27. How to create an Infinite loop in C?

  1. Infinite loop using a while Loop: Set the condition 1 (or any non-zero value) which is always true, so the loop runs indefinitely.
while (1) {
// Code block
}
  1. Infinite loop Using a for Loop: Does not specify any initialization, condition, or increment in the loop. Without a condition, it is always considered true, which will result in an infinite loop.
for (;;) {
// Code block
}
  1. Using a do-while Loop: Similar to while loop, Set the loop condition value to 1 which is always true, so the loop continues indefinitely.
do {
// Code block
} while (1);

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

#include <stdio.h>

int main() {
    int a, b;
    
    // Taking input from the user
    printf("Enter two numbers:\n");
    scanf("%d %d", &a, &b);

    printf("Before swapping: a = %d, b = %d\n", a, b);

    // Swapping without using a third variable
    a = a + b; // Step 1: a now becomes a + b
    b = a - b; // Step 2: b now becomes (a + b) - b = a
    a = a - b; // Step 3: a now becomes (a + b) - a = b

    printf("After swapping: a = %d, b = %d\n", a, b);

    return 0;
}

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

Flowchart of checking prime number

Here is a program to check if a number is prime or not

#include 
#include 
#include 
// Function to check if a number is prime
bool isPrime(int num) {
if (num <= 1) return false; // Numbers less than or equal to 1 are not prime
if (num == 2) return true; // 2 is the only even prime number
if (num % 2 == 0) return false; // Other even numbers are not prime
// Check divisibility from 3 to the square root of the number
for (int i = 3; i <= sqrt(num); i += 2) {
if (num % i == 0) return false;  // If divisible, the number is not prime
}
return true; // If no divisors found, the number is prime
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (isPrime(number)) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}
return 0;
}

30. Write a C program to add two numbers without using the addition operator.

#include <stdio.h>
int add(int a, int b) {
// Loop until there is no carry
while (b != 0) {
// Carry now contains common set bits of a and b
int carry = a & b;
// Sum of bits of a and b where at least one of the bits is not set
a = a ^ b;
// Carry is shifted by one so that adding it to a gives the required sum
b = carry << 1;
}
return a;
}
int main() {
int num1, num2;
// Taking input from the user
printf("Enter two numbers:\n");
scanf("%d %d", &num1, &num2);
int sum = add(num1, num2);
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
return 0;
}

31. What are different techniques for making hash function?

Techniques for making hash function.

Truncation Method

This is the simplest method for computing address from a key.In this method we take only a part of the key as address.

Midsquare Method

In this method the key is squared and some digits from the middle of this square are taken as address.

Folding Method

In this technique, the key is divided into different part where the length of each part is same as that of the required address, except possibly the last part.

Division Method (Modulo-Division)

In Modulo-Division method the key is divided by the table size and the remainder is taken as the address of the hash table.

Let the table size is n then

H (k) =k mod n

32. What are the issues that hamper the efficiency in sorting a file?

The issues are:

  •  Length of time required by the programmer in coding a particular sorting program.
  •  Amount of machine time necessary for running the particular program.
  •  The amount of space necessary for the particular program.

Get 100% Hike!

Master Most in Demand Skills Now!

33. What is the use of volatile keyword?

The modifier ‘volatile’ tells the compiler that a variable’s value may be changed in ways not explicitly specified by the program. For example, a global variable’s address may be passed to the operating system’s clock routine and used to hold the system time.
In this situation, the contents of the variable are altered without any explicit assignment statements in the program.

This is important because most C compilers automatically optimize certain expressions by assuming that a variable’s content is unchanging if it does not occur on the left side of an assignment statement. Thus, it may not be reexamined each time it is referenced. Also, some compilers change the order of evaluation of an expression during the compilation process. The volatile modifier prevents these changes.

34. What are differences between sizeof operator and strlen function?

sizeof is keyword of C that can find size of a String constant including null character, but strlen is function which has been defined string.h and can find number of characters in a string excluding null character.

#include
void main(){
    int a,b;
    a=strlen("cquestionbank");
    b=sizeof("cquestionbank");
    printf("%d  %d",a,b);
    getch();
}

35. When does the compiler not implicitly generate the address of the first element of an array?

The compiler does not implicitly generate the address of the first element of an array whenever an array name appears:

– as an operand of the sizeof operator
– as an operand of & operator
– as a string literal initialize for a character array

36. Is using exit() the same as using return?

No, the exit() function is used to exit your program and return() controls the operating system.

The return statement is used to return from a function and return control to the calling function. If you make a return from the main() function, you are essentially returning control(operating system) to the calling function. In this case, the return statement and exit() function are similar.

37. What is an lvalue?

An lvalue is an expression to which a value can be assigned. The lvalue expression is located on the left side of an assignment statement whereas an rvalue is located on the right side of an assignment statement.

Each assignment statement must have an lvalue and an rvalue. The lvalue expression must refer a storable variable in memory. It cannot be a constant.

38. What is the difference between goto, longjmp() and setjmp()?

  • A goto statement implements a local jump of program execution whereas the longjmp() and setjmp() functions implement a nonlocal or far jump of the program execution.
  • Generally, a jump in any execution should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program.
  •  A goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement goto between functions.

However, when your program calls setjmp(), the current state of your program is saved in a structure of type jmp_buf. Later, your program can call the longjmp() function to restore the program’s state as it was when you called setjmp().Unlike the goto statement, the longjmp() and setjmp() functions do not need to be implemented in the same function.

There is a major drawback of using these functions: your program, when restored to its previously saved state, it will lose its references to any dynamically allocated memory between the longjmp() and the setjmp(). This means you will waste memory for every malloc() or calloc() you have implemented between your longjmp() and setjmp(), and your program will be inefficient.

It is highly recommended that you avoid using functions such as longjmp() and setjmp() because they, like the goto statement, are quite often an indication of poor programming practice.

39. What is XOR linked list?

XOR linked list is a Memory Efficient Doubly Linked List. An ordinary Doubly Linked List requires space for two address fields to store the addresses of previous and next nodes. A memory efficient version of Doubly Linked List can be created using only one space for address field with every node. This memory efficient Doubly Linked List is called XOR Linked List or Memory Efficient as the list uses bitwise XOR operation to save space for one address.

In the XOR linked list, instead of storing actual memory addresses, each node stores the XOR of addresses of previous and next nodes.

XOR List Representation:

Let us call the address variable in XOR representation npx (XOR of next and previous)

Node A:
npx = 0 XOR add(B) // bitwise XOR of zero and address of B
Node B:
npx = add(A) XOR add(C) // bitwise XOR of address of A and address of C
Node C:
npx = add(B) XOR add(D) // bitwise XOR of address of B and address of D
Node D:
npx = add(C) XOR 0 // bitwise XOR of address of C and 0

40. What is ‘trie’ in data structure?

Trie is efficient information retrieval data structure. Using trie, search complexities can be brought to optimal limit (key length). If we store keys in binary search tree, a well balanced BST will need time proportional to M * log N, where M is maximum string length and N is number of keys in tree.

  • Using trie, we can search the key in O(M) time. However, the penalty is on trie storage requirements.
  • Each node of trie consists of multiple branches. Each branch represents a possible character of keys.
  • We need to mark the last node of every key as leaf node.
  • A trie node field value will be used to distinguish the node as leaf node (there are other uses of the value field).
  • A simple structure to represent nodes of English alphabet can be as follows:
struct trie_node
{
   	 	int value; /* Used to mark leaf nodes */
   		 trie_node_t *children[ALPHABET_SIZE];
};

41. What do you understand by splay tree?

Splay tree is a self-balancing Binary Search Tree (BST). The main idea of splay tree is to bring the recently accessed item to root of the tree. This makes the recently searched item to be accessible in O (1) time if accessed again. The idea is to use locality of reference (In a typical application: 80% of the access are to 20% of the items).

Imagine a situation, where we have millions or billions of keys and only few of them are accessed frequently, which is very likely in many practical applications.

All splay tree operations run in O(log n) time on average, where n is the number of entries in the tree. Any single operation can take Theta(n) time in the worst case.

42. What is Treap?

Treap is a Balanced Binary Search Tree, but not guaranteed to have height as O(Log n). The idea is to use Randomization and Binary Heap property to maintain balance with high probability. The expected time complexity of search, insert and delete is O(Log n).

Each node of Treap maintains two values.

  1.  Key follows standard BST ordering (left is smaller and right is greater)
  2.  Priority Randomly assigned value that follows Max-Heap property.

Certification in Full Stack Web Development

43. How to implement LRU caching scheme? What data structures should be used?

We are given total possible page numbers that can be referred. We are also given cache (or memory) size (Number of page frames that cache can hold at a time). The LRU caching scheme is to remove the least recently used frame when the cache is full and a new page is referenced which is not there in cache.

We use two data structures to implement an LRU Cache.

  1.  A Queue: which is implemented using a doubly linked list. The maximum size of the queue will be equal to the total number of frames available (cache size).The most recently used pages will be near front end and least recently pages will be near rear end.
  2.  A Hash: with page number as key and address of the corresponding queue node as value. When a page is referenced, the required page may be in the memory. If it is in the memory, we need to detach the node of the list and bring it to the front of the queue. If the required page is not in the memory, we bring that in memory. In simple words, we add a new node to the front of the queue and update the corresponding node address in the hash. If the queue is full, i.e. all the frames are full, we remove a node from the rear of queue, and add the new node to the front of queue.

44. Suppose, there are two linked lists: L1 and L2 (of same lengths) that intersect at a particular node N1, which is a common endpoint to all other nodes. What are the possibilities to find N1?

Linear solution is possible. Have two pointers say P1 pointing to the first node of L1 and P2 to that of L2. Traverse through both the lists. If P1 reaches L1’s last node, point it to the first node of L2 and continue traversing.

Do the same thing for P2 when it reaches L2’s last node. (By doing this, we are balancing the difference in the length between the linked lists. The shorter one will get over soon and by redirecting to longer list’s head, it will traverse the extra nodes also). Finally, they will meet at the Intersection node.

45. Given two keys K1 & K2, write an algorithm to print all the elements between them with K1<=K2 in a BST.

  • Linear solution is possible without using any extra space.
  •  Perform an inorder traversal.
  •  Once you find K1, print it and continue traversal now.
  •  Print all other traversed elements until you reach K2.

46. How many stacks are required to implement a Queue.

Two stacks are required to implement a Queue.

  • For Enqueue: Take two stacks S1 and S2 and perform push on S1.
  • For Dequeue: If S2 is empty, pop all the elements from S1 and push it to S2. The last element you popped from S1 is an element to be dequeued. If S2 is not empty, then pop the top element in it.

Course Schedule

Name Date Details
Python Course 07 Dec 2024(Sat-Sun) Weekend Batch View Details
14 Dec 2024(Sat-Sun) Weekend Batch
21 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.