31 Most-Asked C Programming Interview Questions (with Code)
The 31 C programs that appear most in product-company and service-company technical rounds, with verified code and recruiter-level explanations.
C technical interviews test three things: whether you know where data lives in memory, whether you can reason about pointer arithmetic, and whether your code handles edge cases that don’t show up in the happy path.
Syntax recall is not what gets candidates shortlisted. This guide covers 31 questions and programs: the ones that appear with the highest frequency across service-company online tests and product-company technical rounds.
If you’re starting from scratch with C fundamentals, the beginner C programs guide covers loops, conditionals, and simple I/O first. Come back here once the basics are solid.
What interviewers actually check in a C round
Most students prepare C by memorising syntax. Interviewers test something else entirely.
The three layers an interviewer evaluates in a C round:
- Correctness — does the program produce the right output for both typical input and edge cases (zero, negative numbers, empty strings)?
- Memory safety — does the candidate know what happens at a null pointer dereference, or when
free()is called twice on the same address? - Efficiency awareness — can the candidate state the time complexity of their own solution and name one improvement?
A candidate who writes a working prime-check function and says “I know this is O(n) but you can reduce it to O(sqrt(n)) by stopping the loop at the square root” will advance past one who writes the same function without that note.
The C data structure programs guide goes deeper on linked-list and tree implementations once you’re past the logic-program stage.
Pointer and memory concepts: the shortlisting questions
These six questions appear in shortlisting rounds as MCQs or output-prediction questions; no IDE, just reasoning.
-
Q1: What is a null pointer? A pointer that holds the value
NULL(which is 0 on all common architectures). Dereferencing a null pointer causes a segmentation fault on Linux/macOS and an access violation on Windows. The SEI CERT C Coding Standard lists null-pointer dereference as a top memory-safety defect. -
Q2: What is a dangling pointer? A pointer that still holds the address of memory that has already been freed. Reading or writing through a dangling pointer is undefined behaviour: the program may crash, produce wrong output, or appear to work correctly until a later allocation reuses that address.
-
Q3: Difference between malloc() and calloc()?
malloc(size)allocatessizebytes without initialising them.calloc(n, size)allocatesn * sizebytes and zero-initialises every byte. Both returnvoid*; both require a matchingfree(). -
Q4: Stack vs. heap — when do you use each? Stack memory is automatically managed: local variables are pushed on entry and popped on exit. Stack size is limited (typically 1 to 8 MB on Linux). Heap memory persists until explicitly freed; use it for data whose lifetime extends beyond a single function or whose size is determined at runtime.
-
Q5: What is a pointer to pointer? A variable that stores the address of another pointer. It appears in functions that need to modify a pointer passed by reference (such as a function that updates a linked-list head node) and in dynamic 2D arrays.
#include <stdio.h>
int main() {
int a = 42;
int *p = &a;
int **pp = &p;
printf("Value via pointer-to-pointer: %d\n", **pp); /* prints 42 */
return 0;
}
- Q6: What does
staticmean inside a function? Astaticlocal variable is initialised only once and retains its value between function calls. It lives in the data segment, not the stack. A common interview question: write a function that counts how many times it has been called; the answer uses a static counter.
Interview programs: logic and math
These are the programs that appear across the widest range of C technical tests. Each program is followed by a note on what the interviewer watches for.
Swap two integers without a third variable
#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b; /* a = 15 */
b = a - b; /* b = 5 (original a) */
a = a - b; /* a = 10 (original b) */
printf("a = %d, b = %d\n", a, b);
return 0;
}
- Output:
a = 10, b = 5 - Interviewer note: XOR swap also works; state the overflow risk of the arithmetic method for large integers.
Reverse a number
#include <stdio.h>
int reverseNumber(int n) {
int rev = 0;
while (n != 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}
int main() {
printf("%d\n", reverseNumber(12345)); /* prints 54321 */
return 0;
}
Check if a number is a palindrome
#include <stdio.h>
int isPalindrome(int n) {
int original = n, rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return original == rev;
}
int main() {
printf("%d\n", isPalindrome(121)); /* prints 1 (true) */
printf("%d\n", isPalindrome(123)); /* prints 0 (false) */
return 0;
}
Factorial using recursion
- Base case: factorial(1) = 1
- Recursive case: factorial(n) = n times factorial(n minus 1)
- factorial(5) = 5 times 4 times 3 times 2 times 1 = 120
#include <stdio.h>
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main() {
printf("%d\n", factorial(5)); /* prints 120 */
return 0;
}
- Interviewer note: Know both recursive and iterative versions. Be ready to state that deep recursion risks a stack overflow for large n.
Check if a number is prime (optimised)
The naive approach checks all divisors from 2 to n. The optimised version checks only up to the square root of n, reducing the loop from O(n) to O(sqrt(n)).
#include <stdio.h>
#include <math.h>
int isPrime(int n) {
if (n < 2) return 0;
for (int i = 2; i <= (int)sqrt((double)n); i++) {
if (n % i == 0) return 0;
}
return 1;
}
int main() {
printf("%d\n", isPrime(17)); /* prints 1 (prime) */
printf("%d\n", isPrime(18)); /* prints 0 (not prime) */
return 0;
}
Fibonacci series (first n terms)
First 8 terms: 0, 1, 1, 2, 3, 5, 8, 13
#include <stdio.h>
void fibonacci(int n) {
int a = 0, b = 1, c;
for (int i = 0; i < n; i++) {
printf("%d ", a);
c = a + b;
a = b;
b = c;
}
printf("\n");
}
int main() {
fibonacci(8); /* prints 0 1 1 2 3 5 8 13 */
return 0;
}
Armstrong number check
A 3-digit Armstrong number equals the sum of the cubes of its digits. 153 is Armstrong because 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.
#include <stdio.h>
int isArmstrong(int n) {
int sum = 0, temp = n;
while (temp != 0) {
int d = temp % 10;
sum += d * d * d;
temp /= 10;
}
return sum == n;
}
int main() {
printf("%d\n", isArmstrong(153)); /* prints 1 */
printf("%d\n", isArmstrong(100)); /* prints 0 */
return 0;
}
Reverse a string in-place
#include <stdio.h>
#include <string.h>
void reverseString(char *str) {
int left = 0, right = (int)strlen(str) - 1;
while (left < right) {
char temp = str[left];
str[left] = str[right];
str[right] = temp;
left++;
right--;
}
}
int main() {
char s[] = "hello";
reverseString(s);
printf("%s\n", s); /* prints olleh */
return 0;
}
Bit manipulation programs that separate candidates
These three programs are standard at embedded, semiconductor, and product-company rounds. They test whether you can read and reason about binary representation, a skill that does not come from writing for-loops.
Count set bits (Brian Kernighan algorithm)
The trick: n & (n - 1) clears the lowest set bit. Repeat until n is zero; the number of iterations equals the number of set bits.
For n = 13 (binary 1101): 13 & 12 = 1100, then 12 & 11 = 1000, then 8 & 7 = 0000. That is three iterations and three set bits.
#include <stdio.h>
int countSetBits(int n) {
int count = 0;
while (n) {
n = n & (n - 1);
count++;
}
return count;
}
int main() {
printf("%d\n", countSetBits(13)); /* prints 3 */
return 0;
}
Check if a number is a power of 2
If n is a power of 2, exactly one bit is set in its binary representation. So n & (n - 1) equals zero.
#include <stdio.h>
int isPowerOfTwo(int n) {
return (n > 0) && ((n & (n - 1)) == 0);
}
int main() {
printf("%d\n", isPowerOfTwo(8)); /* prints 1 (true) */
printf("%d\n", isPowerOfTwo(6)); /* prints 0 (false) */
return 0;
}
Find the only non-repeating element (XOR method)
XOR of a number with itself is 0. XOR of a number with 0 is the number itself. XOR all elements: paired duplicates cancel out, leaving only the unique element.
For the array [2, 3, 5, 3, 2]: 2^3^5^3^2 = 0^0^5 = 5.
#include <stdio.h>
int findUnique(int arr[], int n) {
int result = 0;
for (int i = 0; i < n; i++) {
result ^= arr[i];
}
return result;
}
int main() {
int arr[] = {2, 3, 5, 3, 2};
printf("%d\n", findUnique(arr, 5)); /* prints 5 */
return 0;
}
The full 31 at a glance
| # | Question / Program | Type | Difficulty |
|---|---|---|---|
| 1 | Swap two integers without a third variable | Program | Easy |
| 2 | Reverse a number | Program | Easy |
| 3 | Palindrome check (number) | Program | Easy |
| 4 | Factorial using recursion | Program | Easy |
| 5 | Factorial using iteration | Program | Easy |
| 6 | Prime check (optimised to sqrt) | Program | Easy |
| 7 | Fibonacci series (first n terms) | Program | Easy |
| 8 | Armstrong number check | Program | Easy |
| 9 | Reverse a string in-place | Program | Easy |
| 10 | Count set bits — Brian Kernighan | Program | Medium |
| 11 | Power-of-2 check via bit trick | Program | Medium |
| 12 | XOR trick for unique element in array | Program | Medium |
| 13 | Pointer to pointer example | Conceptual | Medium |
| 14 | Null pointer dereference behaviour | Conceptual | Easy |
| 15 | malloc() vs. calloc() | Conceptual | Easy |
| 16 | Dangling pointer | Conceptual | Easy |
| 17 | Stack vs. heap memory | Conceptual | Easy |
| 18 | static variable inside a function | Conceptual | Easy |
| 19 | *ptr++ vs. (*ptr)++ | Conceptual | Medium |
| 20 | Reverse a linked list (iterative) | Program | Medium |
| 21 | Find middle element of a linked list | Program | Medium |
| 22 | strlen() without standard library | Program | Medium |
| 23 | strcmp() without standard library | Program | Medium |
| 24 | Count vowels in a string | Program | Easy |
| 25 | Check if two strings are anagrams | Program | Medium |
| 26 | Find largest and second largest in an array | Program | Easy |
| 27 | Matrix multiplication in C | Program | Medium |
| 28 | Identify undefined behaviour in a code snippet | Conceptual | Hard |
| 29 | Memory leak detection and prevention | Conceptual | Medium |
| 30 | Compilation process in C (4 stages) | Conceptual | Easy |
| 31 | Reverse a linked list (recursive) | Program | Hard |
Items 1 through 12 have full code in this article. Items 20 through 27 have implementations in the C data structure programs guide.
Interview preparation strategy
Cover the first 12 programs until you can write them from memory without referencing documentation. Then add the four pointer-and-memory conceptual items (13 through 18). That is the full coverage for a service-company C test.
For product-company rounds, extend to items 19 through 31. Items 28 and 31 are the ones most candidates drop: spotting undefined behaviour in someone else’s code, and implementing recursive linked-list reversal. Practise these in isolation before combining them with the rest.
One concrete step: use the cppreference.com C reference to verify every function signature before your interview, because the standard library has edge cases that differ between man pages and actual standards.
Mastering C pointer arithmetic builds exactly the mental model that shows up again in systems-level AI work: tracking where tensors live in GPU memory, how KV-cache is laid out, why the XOR trick for bit indexing appears inside token lookup tables. If you found the bit manipulation section useful and want to go further, TinkerLLM at ₹299 is the entry point: it lets you build and run small LLM inference experiments on real data, where the memory-layout intuitions from C apply directly.
Primary sources
Frequently asked questions
Is C still asked in technical interviews in 2026?
Yes. Product companies and semiconductor firms use C to test pointer reasoning and memory awareness, not just syntax. Service-tier companies like TCS and Infosys include C programs in their online tests, particularly swap, palindrome, and prime-check variants.
Which C programs appear most often in TCS and Infosys tech tests?
Swap without a third variable, palindrome check, Fibonacci series, prime check, and Armstrong number are the five that appear across the widest range of service-company C tests. Factorial (recursive) is a close sixth.
How do I prepare for pointer questions in a C interview?
Practise writing pointer-to-pointer examples, dereferencing chains, and the difference between *ptr++ and (*ptr)++ until you can predict the output without running the code. Pointer questions are almost always conceptual at shortlisting stage, not coding.
What is the difference between malloc and calloc in C?
malloc(size) allocates a block of the given byte count without initialising the memory — the bytes contain whatever was there before. calloc(n, size) allocates n elements of the given size and zero-initialises all bytes. Both return a void pointer; both require free() when done.
Are bit manipulation questions asked in fresher C interviews?
They appear in semiconductor, embedded, and product-company rounds — Qualcomm, Texas Instruments, Zoho, and similar firms include count-set-bits and power-of-2 checks regularly. They are less common in pure service-company screening tests.
What is the difference between *ptr++ and (*ptr)++ in C?
In *ptr++, the ++ applies to the pointer (ptr advances to the next address), then the value at the original address is read. In (*ptr)++, the value at the current address is incremented. The difference becomes observable inside loops that traverse an array.
How many C programs should I practise before a product-company interview?
Cover the 12 core programs in this article plus linked-list reversal and a basic sort. That is 14 programs. For product companies, also practise explaining time complexity and identifying undefined behaviour — the code is the easy part.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)