Top 35 C Programming Interview Questions and Answers
Jul 22, 2026 16 Min Read 63364 Views
(Last Updated)
This collection of C Programming Interview Questions and Answers exists because C, often hailed as the “mother of all languages,” remains a cornerstone of software development. Whether you’re coding embedded systems, designing operating systems, or delving into low-level programming, mastering C is non-negotiable.
With its efficiency, simplicity, and unparalleled control over hardware, C is a must-know language, and these C Programming Interview Questions and Answers reflect exactly what gets tested.
To help you prepare for your next technical interview, we’ve compiled 35 essential C Programming Interview Questions and Answers, organized by experience level from fresher to 5+ years.
This guide is your one-stop resource to ace your interview and stand out as a C programming expert.
Table of contents
- TL;DR Summary
- How to Prepare for a C Programming Interview?
- Topic Coverage at a Glance
- Fresher Level C Programming Interview Questions and Answers
- What is C and why is it called a middle-level language?
- What are the basic data types in C?
- What is the difference between #include <stdio.h> and #include "file.h"?
- What is the purpose of main() in C?
- What is the difference between printf() and scanf()?
- What are keywords in C, and how many are there?
- What are storage classes in C?
- What is the const keyword in C?
- How does sizeof work in C?
- What is the difference between a compiler and an interpreter? Why is C compiled?
- 2 Years Experience Level C Programming Interview Questions and Answers
- What are pointers in C?
- What is a dangling pointer? How do you prevent it?
- What is the difference between call by value and call by reference?
- What is the difference between malloc(), calloc(), and realloc()?
- What is recursion? How does it differ from iteration?
- What is a struct in C, and how does it differ from a union?
- What is the preprocessor in C?
- What is the difference between a shallow copy and a deep copy?
- What is memory alignment and padding in C structs?
- What are function pointers? How are they used?
- 5+ Years Experience Level C Programming Interview Questions and Answers
- What is undefined behavior in C? Why must you avoid it?
- What is the volatile keyword? When do you use it?
- How is memory divided in a running C program?
- What is a segmentation fault, and how does it occur?
- What is the difference between exit() and _exit()?
- What are macros? How do they differ from inline functions?
- What is a reentrant function?
- What is #pragma in C?
- What is the difference between fgets() and gets() for reading strings?
- What is the difference between C89, C99, and C11?
- Tricky Output Questions
- What is the output?
- What is the output?
- What is the output?
- What is the output?
- What is the output?
- C Pointers Interview Questions — Top 10 Most Asked
- What is a pointer, and how do you declare one?
- What is a dangling pointer, and how do you prevent it?
- What is a NULL pointer, and how is it different from an uninitialized pointer?
- What is a void pointer, and why is it useful?
- What is the difference between an array name and a pointer?
- What is pointer arithmetic, and how does it work?
- What is a pointer to a pointer (double pointer)?
- What are function pointers, and how are they used?
- What is the difference between a const pointer and a pointer to a const?
- What happens when you free a pointer twice (double free)?
- C Programming Interview Questions Asked at Embedded Systems Companies
- Why is volatile essential in embedded C, specifically?
- How do you read from and write to a memory-mapped hardware register?
- How do you set, clear, and check individual bits (bit manipulation)?
- Why should interrupt service routines (ISRs) be kept as short as possible?
- C vs C++ Interview Questions — Which Companies Ask Which?
- Common C Interview Mistakes to Avoid
- Conclusion
- FAQs
- How many C programming interview questions should I actually prepare?
- Are C programming interview questions different for freshers versus experienced candidates?
- Which C programming interview questions come up most often in written tests (TCS, Infosys, Wipro)?
TL;DR Summary
- This guide covers 35 C Programming Interview Questions and Answers, organized into Fresher, 2 Years Experience, and 5+ Years Experience difficulty tiers, every single one with a code example.
- Around 70% of C Programming Interview Questions and Answers in real interviews revolve around memory: pointers, dynamic allocation, and undefined behavior.
- Embedded systems companies ask a distinct subset of C Programming Interview Questions and Answers focused on volatile, memory-mapped I/O, and interrupt safety, covered in a dedicated section below.
- Pointers alone account for a large share of all C Programming Interview Questions and Answers, so a dedicated top-10 pointer-specific section is included separately.
- Service companies (TCS, Infosys, Wipro) lean on tricky output questions, while product and embedded companies lean on memory-model and systems-level C Programming Interview Questions and Answers, covered in the C vs C++ comparison section.

How to Prepare for a C Programming Interview?
Working through C Programming Interview Questions and Answers is most effective in this order: fundamentals → memory model → pointers → data structures → tricky output questions → coding problems.
Practical 2-week roadmap:
- Days 1–3: Data types, operators, control flow, functions, scope and storage classes
- Days 4–6: Pointers, arrays, strings, pointer arithmetic
- Days 7–9: Dynamic memory (malloc, calloc, realloc, free), memory segments
- Days 10–11: Structs, unions, enums, bitwise operations
- Days 12–13: File I/O, preprocessor directives, macros
- Day 14: Output-based tricky questions and past company interview questions
Topic Coverage at a Glance
| Topic | Questions Count | Difficulty | Interview Frequency |
|---|---|---|---|
| Fundamentals & Data Types | 6 | Fresher | Very High |
| Pointers & Memory Addresses | 10 | Fresher to 5+ Years | Very High |
| Dynamic Memory Allocation | 4 | 2 Years to 5+ Years | High |
| Structs, Unions & Padding | 4 | 2 Years to 5+ Years | High |
| Preprocessor & Macros | 3 | Fresher to 2 Years | Moderate |
| Memory Model & Undefined Behavior | 5 | 5+ Years | High |
| Tricky Output Questions | 5 | Fresher to 2 Years | Very High (service companies) |
| Embedded-Specific Concepts | 4 | 2 Years to 5+ Years | High (embedded companies only) |
Fresher Level C Programming Interview Questions and Answers
This first tier of C Programming Interview Questions and Answers checks whether your foundations are actually solid. Expect questions about syntax, data types, operators, input/output, and how a basic C program runs. Interviewers aren’t trying to trick you here, they’re making sure you understand what your code is doing instead of memorizing patterns.
1. What is C and why is it called a middle-level language?
This is one of the most fundamental C Programming Interview Questions and Answers pairs you’ll face. C programming is a compiled, procedural language that sits between assembly (low-level) and languages like Python (high-level).
It gives you hardware-level control, direct memory access, and pointer arithmetic while still supporting structured programming with functions, loops, and conditionals, making it ideal for operating systems, compilers, and embedded systems.
#include <stdio.h>
int main() {
int *ptr; // direct memory access: low-level trait
int result = (3 + 5) * 2; // structured expressions: high-level trait
printf("Middle-level in action: %d\n", result);
return 0;
}
2. What are the basic data types in C?
| Data Type | Purpose | Example |
|---|---|---|
| int | Integer values | int a = 5; |
| char | Single character | char b = ‘A’; |
| float | Single-precision decimal | float c = 3.14; |
| double | Double-precision decimal | double d = 3.14159; |
| void | No value / no type | void func() {} |
On a 64-bit system: int = 4 bytes, char = 1 byte, float = 4 bytes, double = 8 bytes, a detail worth memorizing for these C Programming Interview Questions and Answers.
#include <stdio.h>
int main() {
int a = 5;
char b = 'A';
float c = 3.14f;
double d = 3.14159;
printf("%d %c %.2f %.5f\n", a, b, c, d);
return 0;
}
3. What is the difference between #include <stdio.h> and #include “file.h”?
#include <stdio.h>, angle brackets tell the compiler to look only in the system’s standard library directories.#include "file.h", quotes tell the compiler to look in the current directory first, then fall back to system directories.
Use < > for standard library headers. Use " " for your own custom header files, a distinction that shows up often in C Programming Interview Questions and Answers.
// myheader.h
#define GREETING "Hello from custom header"
// main.c
#include <stdio.h>
#include "myheader.h"
int main() {
printf("%s\n", GREETING);
return 0;
}
4. What is the purpose of main() in C?
This is often the very first of all C Programming Interview Questions and Answers asked in a fresher interview. main() is the mandatory entry point of every C program, execution always starts here.
It returns an int (0 signals successful execution to the OS), and can accept command-line arguments via int argc (count) and char *argv[] (values). Every valid C program must have exactly one main() function.
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Number of arguments: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
5. What is the difference between printf() and scanf()?
This pairing is asked in nearly every set of fresher-level C Programming Interview Questions and Answers.
- printf() outputs formatted text to the console. Format specifiers: %d (int), %f (float), %s (string), %c (char), details worth memorizing cold for C Programming Interview Questions and Answers.
- scanf() reads formatted input from the keyboard, and always requires the address of the variable using &, a rule this entire pair of C Programming Interview Questions and Answers hinges on. int x; printf(“Enter a number: “); scanf(“%d”, &x); // & is mandatory, passes the address of x printf(“You entered: %d”, x);
A common beginner mistake, and a recurring theme across C Programming Interview Questions and Answers: forgetting & in scanf(), which causes undefined behavior.
6. What are keywords in C, and how many are there?
This is a common warm-up question in C Programming Interview Questions and Answers. Keywords in C are reserved words with fixed meanings; they cannot be used as variable or function names. C89 defines 32 keywords; C99 added 5 more. Most-tested:
| Keyword | Purpose |
|---|---|
| int, char, float | Declare data types |
| if / else | Conditional branching |
| while, for | Loops |
| return | Exit function, return value |
| struct | Define composite type |
| static | Retain value across function calls |
| const | Mark variable as read-only |
// This will NOT compile: 'int' is a reserved keyword
// int int = 5;
// Correct: keywords combine to form valid statements
static int count = 0;
const int LIMIT = 100;
7. What are storage classes in C?
This is a foundational entry among C Programming Interview Questions and Answers at the fresher level. A storage class defines a variable’s scope, lifetime, and default initial value. There are four:
| Storage Class | Scope | Lifetime | Default Value |
|---|---|---|---|
| auto | Local block | Until block ends | Garbage |
| register | Local block | Until block ends | Garbage |
| static | Local or file | Entire program | 0 |
| extern | Global (across files) | Entire program | 0 |
#include <stdio.h>
void counter() {
static int count = 0; // retains value across calls
count++;
printf("%d\n", count);
}
int main() {
counter(); // prints 1
counter(); // prints 2
counter(); // prints 3
return 0;
}
8. What is the const keyword in C?
This is one of the most frequently asked C Programming Interview Questions and Answers at every experience level. const marks a variable as read-only after initialization, the compiler rejects any attempt to modify it. It improves safety and communicates intent clearly to other developers.
const int MAX = 100; // MAX cannot be changed
const int *ptr = &MAX; // cannot modify the value through ptr
int x = 5;
int * const ptr2 = &x; // cannot change where ptr2 points
Interview tip, and one of the more common C Programming Interview Questions and Answers gotchas: the position of const relative to * matters. const int *p = can’t modify the value. int * const p = can’t change the pointer address.
9. How does sizeof work in C?
This is a near-guaranteed inclusion in fresher-level C Programming Interview Questions and Answers. sizeof is a compile-time operator that returns the size in bytes of a type or variable, it has zero runtime overhead.
printf("%zu\n", sizeof(int)); // 4
printf("%zu\n", sizeof(double)); // 8
int arr[10];
printf("%zu\n", sizeof(arr)); // 40
printf("%zu\n", sizeof(arr)/sizeof(arr[0])); // 10, useful for array length
Primary use, a recurring theme in C Programming Interview Questions and Answers: writing portable code that doesn’t hardcode byte sizes, and calculating correct sizes for malloc() calls.
10. What is the difference between a compiler and an interpreter? Why is C compiled?
| Compiler | Interpreter | |
|---|---|---|
| Process | Translates entire source to machine code before execution | Executes source line-by-line |
| Output | Standalone executable | No separate file |
| Speed | Faster at runtime | Slower at runtime |
| Examples | C, C++, Go | Python (traditional), Ruby |
C uses a compiler (GCC, Clang) because the resulting machine code runs significantly faster, essential for operating systems and embedded firmware where performance is non-negotiable.
// Compile and run manually to see the two-step process:
// Step 1 (compile): gcc program.c -o program
// Step 2 (execute): ./program
// An interpreter (like Python) skips step 1 entirely
C was originally created not as a general-purpose language, but to rewrite the Unix operating system. Before C, Unix was written in assembly , making it tied to one specific processor. Rewriting it in C meant Unix could be ported to different hardware by simply recompiling. That single decision in 1973 made C the backbone of modern computing infrastructure. Today, the Linux kernel, which powers over 90% of the world’s servers, is still written primarily in C.
Looking to move beyond basic ChatGPT skills? Here’s your chance to be a part of Be part of the Bharat AI Initiative, a nationwide movement by HCL GUVI, in association with OpenAI, built to help India’s youth develop advanced ChatGPT skills absolutely free! Learn structured prompting, refine responses with clarity, and apply ChatGPT more effectively in projects, assignments, and everyday work. Learn in English, Hindi, Marathi, Tamil, or Telugu and start your free AI journey!
2 Years Experience Level C Programming Interview Questions and Answers
This second tier of C Programming Interview Questions and Answers shifts focus from writing programs to understanding how they behave in memory. Pointers, structs, dynamic allocation, and function behavior dominate this stage. You’ll need to reason about what happens behind the scenes, not just what the code prints.
11. What are pointers in C?
This might be the single most important entry across every list of C Programming Interview Questions and Answers. This might be the single most important entry across every list of C Programming Interview Questions and Answers.
Pointers enable direct memory access, the foundation of dynamic memory allocation, data structures, and efficient parameter passing.
int num = 10;
int *ptr = # // ptr stores the address of num
printf("%d", *ptr); // dereference: prints 10
printf("%p", (void*)ptr); // prints the memory address
12. What is a dangling pointer? How do you prevent it?
A dangling pointer points to memory that has already been freed or gone out of scope, one of the most-tested memory bugs across C Programming Interview Questions and Answers. Dereferencing it causes undefined behavior or a crash.
int *ptr = (int *)malloc(sizeof(int));
free(ptr);
*ptr = 10; // DANGER: dangling pointer, memory already freed
// Prevention: NULL the pointer immediately after freeing
free(ptr);
ptr = NULL; // now it's a safe null pointer, not a dangling one
13. What is the difference between call by value and call by reference?
| Call by Value | Call by Reference | |
|---|---|---|
| What’s passed | A copy of the variable | The address of the variable |
| Modifies original? | No | Yes |
| When to use | When the function shouldn’t alter caller’s data | When the function must update the caller’s variable |
void byValue(int x) { x = 20; } // original unchanged
void byRef(int *x) { *x = 20; } // original modified
int a = 10;
byValue(a); // a is still 10
byRef(&a); // a is now 20
14. What is the difference between malloc(), calloc(), and realloc()?
| Function | Purpose | Initializes Memory? |
|---|---|---|
| malloc(size) | Allocate size bytes | No, contains garbage |
| calloc(n, size) | Allocate n elements × size bytes | Yes, zero-initialized |
| realloc(ptr, size) | Resize an existing allocation | No |
| free(ptr) | Release allocated memory | N/A |
int *arr = (int *)malloc(5 * sizeof(int));
if (arr == NULL) { exit(1); } // always check!
// use arr...
free(arr);
arr = NULL; // prevent dangling pointer
Always check if the return value is NULL, and always pair every malloc/calloc with a free(), a rule tested constantly in C Programming Interview Questions and Answers.
15. What is recursion? How does it differ from iteration?
This is one of the more universally asked C Programming Interview Questions and Answers, since recursion appears in nearly every programming language interview. Recursion is when a function calls itself to solve a smaller sub-problem. Every recursive function needs a base case to stop.
// Recursive factorial
int factorial(int n) {
if (n == 0) return 1; // base case
return n * factorial(n - 1); // recursive call
}
16. What is a struct in C, and how does it differ from a union?
| Struct | Union | |
|---|---|---|
| Memory | Each member has its own space | All members share the same space |
| Size | Sum of all members (+ padding) | Size of the largest member |
| Access | All members accessible simultaneously | Only one member active at a time |
struct Person { char name[50]; int age; }; // size = 54 bytes (+ padding)
union Data { int i; float f; char s[20]; }; // size = 20 bytes
Use union when a variable needs to hold one of several types at a time, saves memory in embedded systems and protocol parsing, a common follow-up in C Programming Interview Questions and Answers.
Some Interesting Facts:
- 70% of C interview questions revolve around memory. Across service companies and product companies, the same themes keep repeating: pointers, arrays vs pointers, dynamic allocation, and undefined behavior. If your memory model is clear, half the interview becomes predictable.
- Output questions matter more than long programs. Written rounds (especially TCS, Infosys, Wipro style) rarely ask you to build large programs. Instead, they test reasoning speed. Being able to trace 5–10 lines of code accurately is usually more valuable than knowing 50 library functions.
17. What is the preprocessor in C?
This is one of the more conceptual C Programming Interview Questions and Answers, often glossed over by candidates. The C preprocessor runs before compilation; it handles file inclusion, macro expansion, and conditional compilation. It never sees types or variables, only text.
| Directive | Purpose | Example |
|---|---|---|
| #include | Include header file | #include <stdio.h> |
| #define | Define macro or constant | #define PI 3.14159 |
| #ifdef/#endif | Conditional compilation | #ifdef DEBUG |
| #pragma once | Prevent duplicate header inclusion | #pragma once |
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
#ifdef DEBUG
printf("Debug mode is on\n");
#endif
printf("Area: %f\n", PI * SQUARE(5));
Macros have no type safety, prefer const variables and inline functions in C99+ where possible, a point worth remembering for C Programming Interview Questions and Answers.
18. What is the difference between a shallow copy and a deep copy?
This distinction is one of the subtler C Programming Interview Questions and Answers at the 2-year mark. Shallow copy duplicates the pointer (both point to the same memory). Deep copy duplicates the actual data (each has independent memory).
// Shallow, p1 and p2 share the same memory block
struct Person *p2 = p1;
// Deep, p2 has its own independent copy of the data
struct Person *p2 = malloc(sizeof(struct Person));
*p2 = *p1;
Shallow copy is dangerous: modifying one affects the other, and freeing one while accessing the other causes a use-after-free bug, a classic trap in C Programming Interview Questions and Answers.
19. What is memory alignment and padding in C structs?
This is a genuinely tricky topic among C Programming Interview Questions and Answers, often skipped by weaker candidates. Compilers insert padding bytes between struct members to ensure each member is aligned to its own size, misaligned access forces the CPU into multiple memory reads, degrading performance.
struct Unoptimized { char a; int b; char c; };
// sizeof = 12 (not 6), 3 bytes padding after 'a', 3 bytes after 'c'
struct Optimized { int b; char a; char c; };
// sizeof = 8, ordering from largest to smallest minimizes padding
Rule of thumb, and a common follow-up in C Programming Interview Questions and Answers: declare struct members from largest to smallest type to minimize wasted space.
20. What are function pointers? How are they used?
This is one of the more advanced C Programming Interview Questions and Answers at the 2-year mark. A function pointer stores the address of a function, allowing functions to be passed as arguments and called dynamically at runtime.
int add(int a, int b) { return a + b; }
int (*func_ptr)(int, int) = &add; // declare and assign
printf("%d", func_ptr(3, 4)); // call, prints 7
Real-world uses that come up in C Programming Interview Questions and Answers: callbacks (qsort comparator), strategy pattern (swap algorithms without if/else chains), event handlers in embedded systems and GUIs.
Looking to move beyond basic ChatGPT skills? Here’s your chance to be a part of Be part of the Bharat AI Initiative, a nationwide movement by HCL GUVI, in association with OpenAI, built to help India’s youth develop advanced ChatGPT skills absolutely free! Learn structured prompting, refine responses with clarity, and apply ChatGPT more effectively in projects, assignments, and everyday work. Learn in English, Hindi, Marathi, Tamil, or Telugu and start your free AI journey!
5+ Years Experience Level C Programming Interview Questions and Answers
This final tier of C Programming Interview Questions and Answers is where C becomes a systems language. Topics move into memory layout, undefined behavior, compiler interaction, and low-level execution details. Interviewers use these questions to see if you can write reliable, production-grade code rather than classroom code.
21. What is undefined behavior in C? Why must you avoid it?
This concept underlies a huge share of advanced C Programming Interview Questions and Answers. Undefined behavior (UB) is any operation the C standard explicitly leaves unpredictable; the compiler can generate any result, including code that appears to work but silently corrupts data.
int *ptr = NULL; *ptr = 5; // null pointer dereference
int arr[5]; arr[10] = 1; // out-of-bounds write
int x; printf("%d", x); // uninitialized variable read
int a = 2147483647; a = a + 1; // signed integer overflow (INT_MAX + 1)
UB doesn’t always crash immediately, a nuance many C Programming Interview Questions and Answers probe; it can produce wrong results silently, create security vulnerabilities, or behave differently across compilers and optimization levels. Avoiding UB is a core professional C discipline.
22. What is the volatile keyword? When do you use it?
This keyword-specific question shows up in both general and embedded-focused C Programming Interview Questions and Answers. volatile tells the compiler that a variable’s value can change at any time from outside normal program flow, never cache it in a register, always re-read from memory.
volatile int sensor = 0;
// Compiler will always read from memory, never use a cached register value
while (sensor == 0) {
// waits for hardware/interrupt to change sensor
}
Use cases: hardware registers in embedded/firmware code, variables modified by interrupt service routines (ISRs), and memory-mapped I/O. Without volatile, an aggressive optimizer might convert while (flag == 0) {} into an infinite loop that ignores real memory updates.
23. How is memory divided in a running C program?
This memory-segment breakdown is one of the most detailed C Programming Interview Questions and Answers at the senior level. A running C program’s memory is divided into five segments:
| Segment | Contents | Managed By |
|---|---|---|
| Text | Compiled machine code (read-only) | OS |
| Data | Initialized global & static variables | Compiler |
| BSS | Uninitialized global & static variables (zero-filled) | Compiler |
| Heap | Dynamic allocations (malloc, calloc) | Programmer |
| Stack | Local variables, function frames, return addresses | CPU/OS |
int global_initialized = 5; // Data segment
int global_uninitialized; // BSS segment
int main() {
int local = 10; // Stack
int *heap_var = malloc(sizeof(int)); // Heap
*heap_var = 20;
free(heap_var);
return 0;
}
Stack grows downward; heap grows upward, and stack overflow occurs when deep recursion exhausts stack space, a fact worth knowing cold for C Programming Interview Questions and Answers.
24. What is a segmentation fault, and how does it occur?
This is one of the most practical C Programming Interview Questions and Answers, since segfaults happen constantly in real development. A segmentation fault is a runtime crash that occurs when a program accesses memory it doesn’t own or isn’t permitted to access.
int *ptr = NULL;
*ptr = 10; // crash: NULL pointer dereference
int arr[5];
arr[10] = 1; // crash: out-of-bounds write
char *str = "hello";
str[0] = 'H'; // crash: write to read-only string literal
Debug with valgrind ./program or compile with -fsanitize=address -g, tools worth naming in C Programming Interview Questions and Answers. Segfaults are always a memory access violation, trace back to where the invalid pointer originated.
25. What is the difference between exit() and _exit()?
This comparison is a smaller but real entry among senior-level C Programming Interview Questions and Answers.
| exit() | _exit() | |
|---|---|---|
| Cleanup | Flushes I/O buffers, calls atexit() handlers, closes files | None, terminates immediately |
| Use case | Normal program termination | Child process after fork() to avoid flushing parent’s buffers |
exit(0); // clean shutdown, buffers flushed, handlers called
_exit(0); // immediate termination, no cleanup whatsoever
Reading about pointers and memory management is one thing — writing bug-free C code under interview pressure is another. Put your skills to the test with HCL GUVI’s Code Kata, where you can solve real C programming challenges, get instant feedback, and build the muscle memory that actually gets you through technical rounds.
26. What are macros? How do they differ from inline functions?
| Macros | Inline Functions | |
|---|---|---|
| Expansion | Preprocessor text substitution | Compiled inline by the compiler |
| Type checking | None | Yes, compiler enforces types |
| Side effects | Can cause bugs with ++/– | Safe |
| Debugging | Harder | Easy, appears in stack traces |
#define SQUARE(x) ((x) * (x))
int a = 5;
int result = SQUARE(a++); // BUG: expands to ((a++) * (a++)), a incremented twice
inline int square(int x) { return x * x; } // safe, type-checked
int result2 = square(a++); // a incremented exactly once
Always wrap macro arguments in parentheses, and prefer inline functions in C99+, standard advice in most C Programming Interview Questions and Answers guides.
27. What is a reentrant function?
This is a senior-level concept that separates strong C Programming Interview Questions and Answers responses from average ones. A reentrant function can be safely interrupted and called again before its first execution finishes; it produces correct results regardless of concurrent execution.
A function is reentrant if it uses only local variables, calls no non-reentrant functions, and shares no static or global state. Critical for multi-threaded programs and interrupt service routines.
// Non-reentrant: uses static state
char *strtok(char *s, const char *delim); // internal static buffer, not thread-safe
// Reentrant version:
char *strtok_r(char *s, const char *delim, char **saveptr); // caller provides state
28. What is #pragma in C?
This is a smaller but still recurring topic in C Programming Interview Questions and Answers. #pragma is a compiler-specific directive that gives special instructions to the compiler without changing C language semantics.
#pragma once // prevent duplicate header inclusion (modern guard)
#pragma pack(1) // force struct members with no padding
#pragma GCC optimize("O3") // GCC-specific: maximum optimization level
#pragma once is the modern, cleaner alternative to the traditional #ifndef/#define/#endif header guard pattern, another detail worth knowing for C Programming Interview Questions and Answers.
29. What is the difference between fgets() and gets() for reading strings?
| fgets() | gets() | |
|---|---|---|
| Buffer limit | Yes, specify max characters | No, reads until newline |
| Safety | Safe | Causes buffer overflow |
| C11 status | Standard | Removed, never use |
fgets(buffer, sizeof(buffer), stdin); // safe: won't overflow buffer
// gets(buffer); // NEVER USE, removed from C11, causes security vulnerabilities
gets() was officially removed in the C11 standard because it has caused countless real-world buffer overflow vulnerabilities, which is exactly why this shows up in nearly every list of C Programming Interview Questions and Answers. Always use fgets().
30. What is the difference between C89, C99, and C11?
| Standard | Key Additions |
|---|---|
| C89/C90 | Original standard. Variables must be declared at top of block. |
| C99 | // comments, bool type, declare variables anywhere, inline, stdint.h, variadic macros |
| C11 | Multi-threading (<threads.h>), _Generic, _Static_assert, anonymous structs/unions, removed gets() |
// C89 style: all declarations at the top
int add_c89(int a, int b) {
int result;
result = a + b;
return result;
}
// C99 style: declare where needed, plus // comments
int add_c99(int a, int b) {
// C99 allows this comment style and inline declarations
int result = a + b;
return result;
}
For most C Programming Interview Questions and Answers and system work, C99 is the practical baseline. Embedded C projects often target C89 for toolchain compatibility.
Looking to move beyond basic ChatGPT skills? Here’s your chance to be a part of Be part of the Bharat AI Initiative, a nationwide movement by HCL GUVI, in association with OpenAI, built to help India’s youth develop advanced ChatGPT skills absolutely free! Learn structured prompting, refine responses with clarity, and apply ChatGPT more effectively in projects, assignments, and everyday work. Learn in English, Hindi, Marathi, Tamil, or Telugu and start your free AI journey!
Tricky Output Questions
These C Programming Interview Questions and Answers appear regularly in TCS, Wipro, and Infosys written tests. Try to predict the output before reading the answer.
31. What is the output?
#include <stdio.h>
int main() {
int x = 5;
printf("%d %d %d\n", x++, x++, x++);
return 0;
}
Answer: Undefined behavior. This single fact resolves one of the trickiest C Programming Interview Questions and Answers on this list. Function argument evaluation order is unspecified in C. You might see 7 6 5, 5 6 7, or anything else depending on the compiler. Never modify a variable multiple times within the same expression, a rule this exact question tests in nearly every set of C Programming Interview Questions and Answers.
32. What is the output?
#include <stdio.h>
void foo(int arr[]) {
printf("%zu\n", sizeof(arr));
}
int main() {
int arr[10];
printf("%zu\n", sizeof(arr)); // Line A
foo(arr); // Line B
return 0;
}
Answer: Line A prints 40 (10 × 4 bytes). Line B prints 8 (pointer size on a 64-bit system). When an array is passed to a function, it decays into a pointer, sizeof inside the function returns the pointer’s size, not the array’s. This is one of the most commonly asked tricky output C Programming Interview Questions and Answers.
33. What is the output?
#include <stdio.h>
int main() {
static int x = 5;
x--;
if (x) main();
printf("%d ", x);
return 0;
}
Answer: 0 0 0 0 0, five zeros. static means x is shared across all recursive calls. When x reaches 0, the recursion stops and all 5 pending printf calls execute, each printing the current (shared) value of x, which is 0 by then.
34. What is the output?
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
printf("%zu\n", sizeof(str)); // A
printf("%zu\n", strlen(str)); // B
return 0;
}
Answer: A prints 6, B prints 5. sizeof counts the null terminator \0. strlen counts only printable characters and stops at, but does not count, \0. This distinction matters whenever you allocate memory for strings, and it’s a near-guaranteed inclusion in C Programming Interview Questions and Answers.
35. What is the output?
#include <stdio.h>
int main() {
int i = 0;
for (i = 0; i < 5; i++) {
if (i == 3) continue;
printf("%d ", i);
}
return 0;
}
Answer: 0 1 2 4, 3 is skipped. continue skips the rest of the current loop iteration and jumps to the increment step (i++), so the loop keeps running. This differs from break, which exits the loop entirely. A variant asking the difference between continue and break is very common in Infosys and Wipro written tests, making it a staple of service-company C Programming Interview Questions and Answers.
C Pointers Interview Questions — Top 10 Most Asked
Pointers dominate C Programming Interview Questions and Answers more than any other single topic, so here’s a focused, ranked list. Several of these are already covered above; the rest are new additions specific to this section.
1. What is a pointer, and how do you declare one?
A pointer is a variable that stores the memory address of another variable, rather than a value itself. You declare one by writing the base type followed by an asterisk and the variable name.
int num = 10;
int *ptr = # // declares ptr, assigns it the address of num
printf("%d", *ptr); // dereference: prints 10 (the value at that address)
printf("%p", (void*)ptr); // prints the address itself
2. What is a dangling pointer, and how do you prevent it?
A dangling pointer points to memory that has already been freed (or has gone out of scope). The pointer variable itself still holds the old address, but that memory is no longer valid, so dereferencing it causes undefined behavior.
int *ptr = (int *)malloc(sizeof(int));
free(ptr);
*ptr = 10; // DANGER: dangling pointer, memory already freed
// Prevention: set the pointer to NULL immediately after freeing
free(ptr);
ptr = NULL; // now it's a safe, checkable null pointer, not a dangling one
3. What is a NULL pointer, and how is it different from an uninitialized pointer?
int *ptr1 = NULL; // explicitly points to nothing, safe to check
int *ptr2; // uninitialized, points to garbage, unsafe
if (ptr1 == NULL) {
printf("ptr1 is safely NULL\n");
}
// Checking ptr2 here would be undefined behavior, it was never initialized
4. What is a void pointer, and why is it useful?
void *generic_ptr;
int x = 10;
generic_ptr = &x;
printf("%d\n", *(int *)generic_ptr); // must cast before dereferencing
This is one of the more nuanced C Programming Interview Questions and Answers about pointer flexibility. A void pointer can hold the address of any data type, which is why functions like malloc() return void* and library functions like qsort() use it for generic comparisons, a point tested often in C Programming Interview Questions and Answers.
5. What is the difference between an array name and a pointer?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // arr decays into a pointer to arr[0]
printf("%d %d\n", arr[2], *(ptr + 2)); // both print 3
// Key difference: sizeof(arr) is 20, sizeof(ptr) is 8 (on 64-bit)
// arr cannot be reassigned; ptr can point elsewhere
6. What is pointer arithmetic, and how does it work?
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;
printf("%d\n", *(ptr + 2)); // prints 30, moves 2*sizeof(int) bytes forward
ptr++; // now points to arr[1]
printf("%d\n", *ptr); // prints 20
Pointer arithmetic scales automatically by the size of the pointed-to type, ptr + 1 on an int* moves 4 bytes, not 1, a detail that trips up many candidates in C Programming Interview Questions and Answers.
7. What is a pointer to a pointer (double pointer)?
int x = 10;
int *p = &x;
int **pp = &p; // pp holds the address of p
printf("%d\n", **pp); // dereference twice: prints 10
Common use, and a favorite among advanced C Programming Interview Questions and Answers: modifying a pointer itself inside a function (like reallocating memory and updating the caller’s pointer variable).
8. What are function pointers, and how are they used?
A function pointer stores the address of a function instead of a variable, letting you pass functions as arguments and call them dynamically at runtime.
int add(int a, int b) { return a + b; }
int (*func_ptr)(int, int) = &add; // declare and assign
printf("%d", func_ptr(3, 4)); // call through the pointer, prints 7
Common real-world uses: callback functions (like the comparator passed to qsort()), and swapping algorithms in and out without long if/else chains.
9. What is the difference between a const pointer and a pointer to a const?
The position of const relative to the asterisk changes what’s actually protected, the value being pointed to, or the pointer’s own address.
const int MAX = 100;
const int *p1 = &MAX; // pointer to a const: can't change *p1, but p1 can point elsewhere
int x = 5;
int * const p2 = &x; // const pointer: can change *p2, but p2 can't point elsewhere
// *p1 = 200; // ERROR: can't modify value through p1
// p2 = &MAX; // ERROR: can't change where p2 points
10. What happens when you free a pointer twice (double free)?
int *ptr = malloc(sizeof(int));
free(ptr);
free(ptr); // DANGER: double free, undefined behavior, often crashes or corrupts heap metadata
// Prevention: always NULL a pointer immediately after freeing it
free(ptr);
ptr = NULL;
free(ptr); // now safe: freeing NULL is explicitly a no-op in C
Double-free bugs are a classic source of security vulnerabilities and are exactly why the “NULL immediately after free” habit matters so much in C Programming Interview Questions and Answers on memory safety.
C Programming Interview Questions Asked at Embedded Systems Companies
Embedded systems companies (think automotive, IoT, telecom hardware) ask a noticeably different subset of C Programming Interview Questions and Answers than typical service companies, focused on hardware interaction rather than general application logic.
1. Why is volatile essential in embedded C, specifically?
Covered conceptually in Q22 above, but embedded interviewers push further: they’ll ask you to identify which of several given variables genuinely need volatile (hardware registers, ISR-modified flags) versus which don’t (a simple loop counter), since overusing volatile hurts optimization unnecessarily.
2. How do you read from and write to a memory-mapped hardware register?
#define STATUS_REGISTER (*(volatile unsigned int *)0x40000000)
void wait_for_ready() {
while ((STATUS_REGISTER & 0x01) == 0) {
// busy-wait until the ready bit is set by hardware
}
}
This pattern, casting a fixed memory address to a volatile pointer, is the standard way to interact directly with hardware registers, and it’s a staple of embedded-focused C Programming Interview Questions and Answers.
3. How do you set, clear, and check individual bits (bit manipulation)?
#define SET_BIT(reg, bit) ((reg) |= (1 << (bit)))
#define CLEAR_BIT(reg, bit) ((reg) &= ~(1 << (bit)))
#define CHECK_BIT(reg, bit) (((reg) >> (bit)) & 1)
unsigned char flags = 0;
SET_BIT(flags, 3); // flags is now 00001000
int is_set = CHECK_BIT(flags, 3); // is_set = 1
CLEAR_BIT(flags, 3); // flags is back to 00000000
Bitwise manipulation is a near-guaranteed topic in embedded interviews specifically, since register configuration in firmware happens almost entirely at the bit level, more so than in generic C Programming Interview Questions and Answers.
4. Why should interrupt service routines (ISRs) be kept as short as possible?
This concept is central to embedded-focused C Programming Interview Questions and Answers. An ISR blocks other interrupts (or at least that interrupt line) while it runs. A long-running ISR can cause missed hardware events, timing violations, or watchdog resets.
volatile int data_ready_flag = 0;
void ISR_DataReceived() {
data_ready_flag = 1; // just set a flag, do minimal work
}
int main() {
while (1) {
if (data_ready_flag) {
data_ready_flag = 0;
// do the actual heavy processing HERE, outside the ISR
}
}
}
The correct pattern, one of the more nuanced embedded C Programming Interview Questions and Answers: the ISR sets a flag or copies minimal data, and the main loop (or a task scheduler) does the actual heavy processing afterward.
Embedded interviewers, in their version of C Programming Interview Questions and Answers, also frequently ask about stack size constraints, since embedded systems often have only kilobytes of RAM.
Other common topics: fixed-width integer types from stdint.h (uint8_t, int16_t) for portability across microcontrollers, and avoiding dynamic memory allocation (malloc/free) entirely in safety-critical firmware due to fragmentation risk.
C vs C++ Interview Questions — Which Companies Ask Which?
Since C and C++ Programming Interview Questions and Answers are often tested in the same interview loop, it helps to know which questions belong to which language, and which company types lean which way.
This table sums up how C Programming Interview Questions and Answers differ by employer type.
| Company Type | Typically Asks | Why |
|---|---|---|
| IT Services (TCS, Infosys, Wipro) | Pure C, output-based tricky questions | Written aptitude-style tests dominate; focus on trace-the-code speed |
| Embedded/Automotive/IoT firms | Pure C, plus hardware-specific topics | Firmware is almost always written in C for size and predictability |
| Product companies (fintech, SaaS) | C++ (classes, STL, memory management) | Backend services are usually C++ or a higher-level language, not raw C |
| Systems/Infrastructure companies | Both C and C++ | Operating systems and low-level tooling mix both, depending on the specific team |
| Competitive programming-heavy interviews | C++ | STL containers and algorithms save significant time under a clock |
Key conceptual differences interviewers probe:
These conceptual differences show up constantly across both C and C++ Programming Interview Questions and Answers.
- Memory management: C requires manual malloc/free; C++ adds
new/deleteplus RAII and smart pointers (unique_ptr,shared_ptr) that manage memory automatically. - Paradigm: C is purely procedural; C++ adds object-oriented programming (classes, inheritance, polymorphism) on top.
- Standard Library: C has a minimal standard library; C++’s STL (vectors, maps, algorithms) is a major reason product companies default to C++ over C for new backend code.
- Function overloading: Not possible in C (same function name = same signature only); C++ allows multiple functions with the same name and different parameter lists. // C: no function overloading, names must be unique int add_int(int a, int b) { return a + b; } float add_float(float a, float b) { return a + b; } // C++: function overloading is allowed // int add(int a, int b) { return a + b; } // float add(float a, float b) { return a + b; }
The practical takeaway: if you’re interviewing at an embedded, firmware, or classic IT services company, prioritise this guide’s pure C content.
If you’re interviewing at a product company for a backend or infrastructure role, expect the interview to pivot into C++-specific territory (classes, STL, RAII) fairly quickly, even if it opens with a few C fundamentals.
Looking to move beyond basic ChatGPT skills? Here’s your chance to be a part of Be part of the Bharat AI Initiative, a nationwide movement by HCL GUVI, in association with OpenAI, built to help India’s youth develop advanced ChatGPT skills absolutely free! Learn structured prompting, refine responses with clarity, and apply ChatGPT more effectively in projects, assignments, and everyday work. Learn in English, Hindi, Marathi, Tamil, or Telugu and start your free AI journey!
Common C Interview Mistakes to Avoid
- Forgetting & in scanf(), passes the value instead of the address; causes undefined behavior
- Not checking malloc() return value, if allocation fails, NULL is returned; using it crashes your program
- Forgetting free(), causes memory leaks that accumulate in long-running programs
- Using == to compare strings , compares addresses, not content; use strcmp(s1, s2) == 0 instead
- Modifying string literals , char *s = “hello”; s[0] = ‘H’; is undefined behavior; use char s[] = “hello”;
- Off-by-one in arrays , valid indices for int arr[n] are 0 to n-1; arr[n] is out of bounds
Master C programming with HCL GUVI’s C Programming Course, designed for both aspiring programmers and those preparing for C programming interviews. This course offers hands-on practice, real-world problem-solving examples, and detailed guidance on essential concepts like loops, arrays, and functions. With lifetime access, interactive learning modules, and industry-relevant projects, it’s perfect for building a solid foundation in C and excelling in technical interviews.
Conclusion
In conclusion, C interviews reward deep understanding over surface memorization. Companies want to see that you know why things work the way they do across every one of these C Programming Interview Questions and Answers, why pointers are powerful and dangerous, why memory management is manual, and why undefined behavior exists.
Master these 35 C Programming Interview Questions and Answers across all three experience tiers, practice writing code on paper, and trace through programs mentally. That combination is what gets you hired.
FAQs
How many C programming interview questions should I actually prepare?
There’s no fixed number, but working through all 35 questions in this guide, spanning fresher, 2-year, and 5+ year difficulty, covers the core topics that come up repeatedly across service companies, product companies, and embedded firms.
Depth matters more than breadth: understanding why pointers, memory, and undefined behavior work the way they do is worth more than memorizing 100 shallow answers.
Are C programming interview questions different for freshers versus experienced candidates?
Yes, significantly. Fresher-level questions focus on syntax, data types, and basic I/O. At 2 years of experience, the focus shifts to pointers, dynamic memory, and structs.
At 5+ years, expect questions on memory segments, undefined behavior, reentrant functions, and compiler-level details, the kind of systems-level knowledge senior roles actually require.
Which C programming interview questions come up most often in written tests (TCS, Infosys, Wipro)?
Service company written tests lean heavily on the tricky output questions covered in this guide, tracing what a piece of code prints, especially around operator precedence, static variables, and loop control (continue vs break). They rarely ask you to write large programs; they test how quickly and accurately you can trace existing code.



Did you enjoy this article?