C programming questions are common in fresher, embedded and systems interviews. These C interview questions cover data types, operators, arrays and functions for beginners, then pointers, memory allocation, structures, storage classes, the preprocessor and undefined behaviour for experienced C programmers.
30 questions · Updated
Reading answers is not the same as saying them.Practise C Programming questions out loud and get a score, what you missed and a model answer for each one.
Practise free with AIC Programming Interview Questions: 30 questions with model answers on this page.
Basic C Programming interview questions
Fundamentals, definitions and simple scenarios. Good for freshers and warm-ups.
1. What are the main features of C?
Answer flow · 3 steps
1
C is a procedural, compiled language that is fast and close to the hardware, with direct memory access through pointers.
2
It is portable across platforms, has a small set of keywords and a standard library, and is used for operating systems, embedded systems, compilers and drivers.
3
Many languages, including C++, Java and Python's interpreter, are built on or influenced by it.
2. What is the difference between call by value and call by reference?
C passes arguments by value: the function receives a copy, so changes inside the function do not affect the original. To change the caller's variable, you pass its address (a pointer), which simulates call by reference, as in swap(&a, &b) where the function modifies *a and *b.
3. What is the difference between ++i and i++?
++i (pre-increment) increases i and then returns the new value. i++ (post-increment) returns the current value and then increases i. For example, with i = 5, a = ++i makes a 6, while a = i++ makes a 5 and i 6. Modifying a variable twice in one expression, such as i = i++, is undefined behaviour.
4. What are the stages of compiling a C program?
Answer flow · 4 steps
1
Preprocessing handles directives such as #include and #define and removes comments.
2
Compilation turns the preprocessed code into assembly.
3
Assembly converts it into machine code in object files.
4
Linking combines object files and libraries into an executable, resolving references to functions such as printf. gcc runs all four stages, and flags such as -E, -S and -c stop after each one.
5. What are the basic data types and format specifiers in C?
char (usually 1 byte, %c), int (usually 4 bytes, %d), float (4 bytes, %f), double (8 bytes, %lf with scanf, %f with printf), with modifiers short, long, long long and unsigned (%u, %ld, %lld). Sizes depend on the platform, so sizeof should be used, and <stdint.h> provides exact-width types such as int32_t. %s prints strings and %p prints pointers.
6. What are header files?
Answer flow · 3 steps
1
Header files (.h) contain declarations of functions, macros, constants and types that several source files share, included with #include.
2
Angle brackets (<stdio.h>) search system directories, while quotes ("myfile.h") search the project first.
3
Include guards (#ifndef ... #define ... #endif) or #pragma once prevent a header from being included twice.
7. How are strings stored in C?
Answer flow · 3 steps
1
A C string is an array of characters ending with a null terminator \0, so "hi" needs three bytes.
2
Functions in <string.h> such as strlen, strcpy, strcat and strcmp rely on the terminator.
3
Forgetting space for it or copying a longer string into a smaller array causes buffer overflows, so safer functions such as snprintf and strncpy are preferred.
8. What is the difference between while, do-while and for loops?
Answer flow · 4 steps
1
A while loop checks its condition before each iteration, so it may run zero times.
2
A do-while loop checks after each iteration, so it always runs at least once, which suits menus and input validation.
3
A for loop puts initialisation, condition and update in one line, which suits counting loops.
4
Any of them can be written as another.
9. How do break, continue and switch work?
break exits the nearest loop or switch immediately. continue skips the rest of the current loop iteration. A switch compares an integer or character expression with case labels and jumps to the matching one; without break, execution falls through to the next case, which is a common bug. default handles values with no matching case.
10. Why should you never use gets()?
Answer flow · 3 steps
1
gets() reads a line into a buffer without knowing its size, so long input overflows the buffer and can corrupt memory or allow attacks.
2
It was removed from the C11 standard.
3
Use fgets(buffer, sizeof buffer, stdin), which limits the number of characters read, and check scanf's return value and use width limits such as %19s.
11. Write a recursive function to calculate a factorial.
A recursive function calls itself with a smaller input until it reaches a base case. Factorials grow very fast, so use a large type such as unsigned long long and note that values overflow beyond 20!.
unsigned long long factorial(unsigned int n) {
if (n <= 1) return 1; /* base case */
return n * factorial(n - 1); /* recursive case */
}
Intermediate C Programming interview questions
Applied problems, trade-offs and questions about your own projects.
12. What is a pointer, and what is a NULL pointer?
Answer flow · 3 steps
1
A pointer is a variable that stores a memory address. & gives a variable's address and * dereferences a pointer to access the value.
2
A NULL pointer points to nothing and is used to show that a pointer is not set; dereferencing it causes a crash.
3
Pointers are used for dynamic memory, arrays, strings, passing large data efficiently and data structures such as linked lists.
13. What is the difference between malloc, calloc, realloc and free?
malloc(size) allocates a block of uninitialised memory. calloc(n, size) allocates memory for n elements and sets it to zero. realloc(ptr, size) resizes a previously allocated block, possibly moving it. free(ptr) releases the memory. Every allocation must be freed exactly once, the return value must be checked for NULL, and pointers should be set to NULL after free to avoid dangling pointers.
14. What is the difference between a structure and a union?
Answer flow · 3 steps
1
A structure gives each member its own memory, so its size is at least the sum of its members (plus padding), and all members can hold values at the same time.
2
A union shares the same memory among all members, so its size is that of the largest member and only one member holds a valid value at a time.
3
Unions save memory and are used in embedded systems and variant data types.
15. What are the storage classes in C?
auto (the default for local variables, stored on the stack), register (a hint to store in a CPU register), static (keeps its value between function calls, or limits a global variable or function to its file), and extern (declares a variable defined in another file). They control a variable's scope, lifetime and linkage.
16. What is pointer arithmetic?
Answer flow · 3 steps
1
Adding an integer to a pointer moves it by that many elements, not bytes, so if p points to an int array, p + 1 points to the next int (4 bytes further on most systems).
2
Subtracting two pointers into the same array gives the number of elements between them. arr[i] is equivalent to *(arr + i).
3
Arithmetic outside the bounds of an array is undefined behaviour.
17. What is the difference between an array and a pointer?
An array name usually converts to a pointer to its first element, but they differ: sizeof(array) gives the total size of the array while sizeof(pointer) gives the size of the pointer itself; an array cannot be reassigned while a pointer can; and an array declared in a function has fixed storage, while a pointer can point anywhere. When passed to a function, an array becomes a pointer, losing its size.
18. What is a pointer to a pointer?
A double pointer (int **pp) stores the address of another pointer. It is used to let a function change a caller's pointer, for example allocating memory and returning it through an argument, to build dynamic two-dimensional arrays, and for arrays of strings such as char **argv in main.
19. What are function pointers?
A function pointer stores the address of a function, declared like int (*op)(int, int), so functions can be passed as arguments and chosen at runtime. They are used for callbacks, such as the comparison function passed to qsort, for tables of handlers in state machines and command dispatchers, and to imitate polymorphism in C.
20. What is the difference between const int *p, int *const p and const int *const p?
Read declarations from right to left. const int *p is a pointer to a constant int: you cannot change the value through p, but p can point elsewhere. int *const p is a constant pointer to an int: the value can change, but p cannot point elsewhere. const int *const p is a constant pointer to a constant int, where neither can change.
21. How do you work with files in C?
fopen opens a file with a mode such as "r" (read), "w" (write, truncating), "a" (append) or "rb" for binary, and returns NULL on failure, which must be checked. fprintf, fscanf, fgets and fputs handle text, fread and fwrite handle binary data, fseek and ftell move within the file, and fclose closes it and flushes buffers.
22. How are bitwise operators used?
& (AND), | (OR), ^ (XOR), ~ (NOT), << and >> (shifts) work on individual bits. They are used to set a bit (x |= 1 << n), clear it (x &= ~(1 << n)), toggle it (x ^= 1 << n) and test it (x & (1 << n)), which is common in embedded systems for hardware registers and flags, and for compact storage and fast arithmetic.
High level C Programming interview questions
System design, deep internals, leadership and tough follow-ups.
23. What are memory leaks, dangling pointers and buffer overflows?
Answer flow · 4 steps
1
A memory leak happens when allocated memory is never freed, so the program uses more and more memory.
2
A dangling pointer points to memory that has been freed or gone out of scope, and using it causes unpredictable behaviour.
3
A buffer overflow writes past the end of an array, corrupting memory and creating security vulnerabilities.
4
Tools like Valgrind and AddressSanitizer detect them.
24. What is the volatile keyword used for?
Answer flow · 3 steps
1
volatile tells the compiler that a variable can change outside the program's normal flow, such as a hardware register, a variable changed by an interrupt handler or memory shared with another thread, so it must not optimise away reads or writes.
2
It is important in embedded systems.
3
It does not make operations atomic or thread-safe.
25. What is the difference between a macro and an inline function?
Answer flow · 3 steps
1
A macro, defined with #define, is replaced by text by the preprocessor before compilation, with no type checking, and arguments can be evaluated more than once, such as SQUARE(x++).
2
An inline function is a real function that the compiler may expand in place for speed, with type checking, normal scope and single evaluation of arguments.
3
Inline functions are usually safer.
26. What is undefined behaviour? Give examples.
Answer flow · 3 steps
1
Undefined behaviour means the C standard places no requirements on what happens: the program may crash, give wrong results or appear to work until optimisations change it.
2
Examples are dereferencing NULL or freed pointers, accessing arrays out of bounds, signed integer overflow, using uninitialised variables, modifying a variable twice without a sequence point, and data races.
3
Sanitisers such as -fsanitize=address,undefined help find it.
27. What is the memory layout of a C program?
From low to high addresses: the text segment holds the compiled code (usually read-only), the initialised data segment holds global and static variables with initial values, the BSS segment holds uninitialised globals and statics (set to zero), the heap grows upwards for malloc allocations, and the stack grows downwards for function calls, local variables and return addresses.
28. What is structure padding?
Answer flow · 3 steps
1
Compilers insert padding bytes between structure members so each member is aligned to an address suited to its type, which makes memory access faster.
2
So a struct with a char, an int and a char may take 12 bytes rather than 6.
3
Ordering members from largest to smallest reduces padding; #pragma pack or __attribute__((packed)) removes it, for example for network protocols, at a performance cost.
29. What is endianness, and how do you check it?
Answer flow · 3 steps
1
Endianness is the order in which bytes of a multi-byte value are stored.
2
Little-endian systems such as x86 and most ARM store the least significant byte first; big-endian systems store the most significant byte first, which is also network byte order.
3
You can check by storing 1 in an int and reading its first byte through a char pointer. htonl and ntohl convert values for networking.
30. How do you organise a C program across several files?
Answer flow · 3 steps
1
Put function declarations, types and extern variable declarations in header files, and definitions in .c files, each compiled separately and linked together. static at file level keeps functions and variables private to their file.
2
Static linking copies library code into the executable, while dynamic linking loads shared libraries (.so or .dll) at runtime.
3
Make or CMake automates builds.
C Programming machine coding round questions
Tasks you build live in 30 to 60 minutes. What interviewers look for, plus a compact solution to compare with yours.
Reverse a string in place.
A common fresher question that tests pointers or indexes and the null terminator. Swap characters from both ends towards the middle.
#include <string.h>
void reverse(char *s) {
size_t i = 0, j = strlen(s);
if (j == 0) return;
for (j--; i < j; i++, j--) {
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
Count the number of set bits in an integer.
Tests bit manipulation. Brian Kernighan's trick n & (n - 1) clears the lowest set bit, so the loop runs once per set bit.
unsigned int count_bits(unsigned int n) {
unsigned int count = 0;
while (n) {
n &= n - 1;
count++;
}
return count;
}
Ready to test yourself?Pick your topics and level, answer by voice or text, and get instant feedback. Free.