C Notes
Comprehensive collection of 50 most asked C programming interview questions with detailed answers covering basics, pointers, memory, strings, and advanced topics.
Whether you're preparing for a campus placement, a technical interview at a product company, or a systems programming role, these 50 C programming questions cover everything interviewers commonly ask. Each answer is explained clearly with examples where needed.
Basic C Programming Questions
Q1: What is C programming language?
C is a general-purpose, procedural programming language developed by Dennis Ritchie in 1972 at Bell Labs. It provides low-level memory access, efficient compilation, and is used for system programming, embedded systems, operating systems (Linux, Windows kernel), and performance-critical applications.
Q2: What are the key features of C?
- Procedural (structured) language
- Low-level memory access via pointers
- Fast execution speed
- Rich set of operators
- Portable across platforms
- Extensible through libraries
- Middle-level language (combines high and low-level features)
Q3: What is the difference between compiler and interpreter?
| Compiler | Interpreter |
|---|---|
| Translates entire program at once | Translates line by line |
| Generates object code | No object code generated |
| Faster execution | Slower execution |
| Shows all errors after compilation | Stops at first error |
| C, C++ use compilers | Python, JavaScript use interpreters |
Q4: What are data types in C?
// Primary types and their sizes (typical 64-bit system)
char c = 'A'; // 1 byte
int i = 42; // 4 bytes
float f = 3.14f; // 4 bytes
double d = 3.14159; // 8 bytes
// Modifiers
short int si = 32767; // 2 bytes
long int li = 2147483647L; // 8 bytes
unsigned int ui = 4294967295U; // 4 bytesQ5: What is the difference between = and ==?
= is the assignment operator (assigns a value). == is the equality comparison operator (checks if two values are equal and returns 1 or 0).
Q6: What is a variable? What are the naming rules?
A variable is a named memory location that stores data. Rules: must start with a letter or underscore, can contain letters/digits/underscores, cannot be a keyword, is case-sensitive.
Q7: What are storage classes in C?
- auto – default, local scope, stored on stack
- register – hints compiler to store in CPU register
- static – retains value between function calls, exists for program lifetime
- extern – declared in one file, defined in another
Q8: What is the difference between while and do-while?
while checks condition before executing the body (may execute 0 times). do-while executes the body first, then checks (always executes at least once).
Q9: What is a ternary operator?
A shorthand for if-else: condition ? value_if_true : value_if_false. Example: int max = (a > b) ? a : b;
Q10: What is type casting?
Converting one data type to another. Implicit (automatic): int to float. Explicit (manual): (int)3.14 gives 3.
Pointer Questions
Q11: What is a pointer?
A variable that stores the memory address of another variable. Declared with *: int *ptr = &x;
Q12: What is a NULL pointer?
A pointer initialized to NULL (address 0). It points to nothing and dereferencing it causes undefined behavior (usually a crash).
Q13: What is a dangling pointer?
A pointer that points to memory that has been freed or is no longer valid. Accessing it causes undefined behavior.
Q14: What is a void pointer?
A generic pointer (void *) that can hold the address of any data type. Must be cast before dereferencing: *(int*)void_ptr.
Q15: What is pointer arithmetic?
Adding/subtracting integers from pointers. ptr + 1 moves to the next element (not the next byte). The increment is sizeof(pointed_type) bytes.
Q16: Difference between int *p[10] and int (*p)[10]?
int *p[10]– array of 10 integer pointersint (*p)[10]– pointer to an array of 10 integers
Q17: What is a wild pointer?
An uninitialized pointer that contains a garbage address. Using it leads to unpredictable behavior.
Q18: Can you have a pointer to a function?
Yes. Syntax: int (*fptr)(int, int) = &add;. Call: fptr(3, 5);
Memory Management Questions
Q19: What is dynamic memory allocation?
Allocating memory at runtime using malloc(), calloc(), realloc(), and freeing with free(). Memory is allocated on the heap.
Q20: Difference between malloc and calloc?
malloc(size) allocates size bytes without initialization. calloc(n, size) allocates n*size bytes and initializes all to zero.
Q21: What is a memory leak?
Memory that was allocated with malloc/calloc but never freed. It remains occupied until the program terminates, reducing available memory.
Q22: What is a segmentation fault?
A runtime error when the program tries to access memory it doesn't have permission to access (NULL dereference, buffer overflow, use-after-free).
Q23: What is the difference between stack and heap?
| Stack | Heap |
|---|---|
| Automatic allocation | Manual allocation |
| LIFO order | No specific order |
| Limited size (~1-8 MB) | Limited by system RAM |
| Fast allocation | Slower allocation |
| Stores local variables | Stores dynamic data |
Q24: What does free(NULL) do?
Nothing – it's defined to be a no-op. It's safe to call free(NULL).
String Questions
Q25: What is a string in C?
An array of characters terminated by a null character '\0'. char str[] = "hello"; occupies 6 bytes (5 chars + null).
Q26: Difference between char str[] and char *str?
char str[] = "hello" – creates a modifiable array on stack. char *str = "hello" – points to a string literal in read-only memory (cannot be modified).
Q27: How do you find string length without strlen?
Q28: What is strncpy and why use it over strcpy?
strncpy(dest, src, n) copies at most n characters, preventing buffer overflow. Always ensure null termination: dest[n-1] = '\0'.
Array and Structure Questions
Q29: Can arrays be passed by value to functions?
No. Arrays always decay to pointers when passed to functions. The function receives a pointer to the first element.
Q30: What is a structure in C?
A user-defined data type that groups related variables of different types under one name. Defined with struct keyword.
Q31: What is structure padding?
The compiler adds extra bytes between members to align them on natural boundaries for faster access. Use __attribute__((packed)) or #pragma pack to disable.
Q32: Difference between structure and union?
| Structure | Union |
|---|---|
| All members have own memory | All members share memory |
| Size = sum of all members + padding | Size = largest member |
| All members accessible simultaneously | Only one member valid at a time |
Q33: What is a self-referential structure?
A structure that contains a pointer to itself. Used in linked lists: struct Node { int data; struct Node *next; };
Preprocessor and Compilation
Q34: What is a preprocessor directive?
Instructions processed before compilation. Start with #. Examples: #include, #define, #ifdef, #pragma.
Q35: Difference between #include <file> and #include "file"?
<file> searches system directories. "file" searches current directory first, then system directories.
Q36: What is a macro? What are its disadvantages?
A text substitution defined with #define. Disadvantages: no type checking, side effects with expressions (#define SQ(x) x*x – SQ(a+b) expands wrong), harder to debug.
Q37: What are compilation stages?
- Preprocessing (macro expansion, file inclusion)
- Compilation (C to assembly)
- Assembly (assembly to object code)
- Linking (combines object files, resolves symbols)
Advanced Questions
Q38: What is the volatile keyword?
Tells the compiler not to optimize access to this variable – it may change unexpectedly (by hardware, another thread, or signal handler). Used in embedded and multithreaded programming.
Q39: What is the const keyword?
Declares a variable as read-only after initialization. const int x = 5; – x cannot be modified.
Q40: What is a static variable?
A variable that retains its value between function calls and exists for the program's lifetime. Has local scope but permanent storage.
Q41: What is recursion?
A function calling itself. Must have a base case to stop. Uses stack memory for each call – can cause stack overflow for deep recursion.
Q42: What is the difference between break and continue?
break exits the entire loop. continue skips the rest of the current iteration and moves to the next.
Q43: What is a file pointer?
A pointer of type FILE* returned by fopen() that tracks the current position and state of a file being read/written.
Q44: What are command line arguments?
Parameters passed to the program at execution. Accessed via int main(int argc, char *argv[]). argc = count, argv = array of strings.
Q45: What is the sizeof operator?
A compile-time operator that returns the size in bytes of a type or variable. sizeof(int) is typically 4.
Q46: What is an enum?
A user-defined type with named integer constants. enum Color { RED, GREEN, BLUE }; – RED=0, GREEN=1, BLUE=2.
Q47: What is a typedef?
Creates an alias for an existing type. typedef unsigned long ulong; – now you can use ulong instead of unsigned long.
Q48: What is the difference between i++ and ++i?
i++ (post-increment) returns the original value then increments. ++i (pre-increment) increments first then returns the new value. In standalone statements, both are equivalent.
Q49: What is undefined behavior in C?
Code whose behavior is not defined by the C standard. Examples: accessing out-of-bounds array, dereferencing NULL, signed integer overflow. The compiler can do anything – crash, produce wrong results, or appear to work.
Q50: What are the differences between C and C++?
| C | C++ |
|---|---|
| Procedural | Object-oriented + procedural |
| No classes/objects | Full OOP support |
| malloc/free | new/delete + RAII |
| No function overloading | Supports overloading |
| No references | Has reference types |
| No exceptions | Exception handling |
| No STL | Rich Standard Template Library |
Tips for C Programming Interviews
- Practice writing code on paper – many interviews don't provide IDEs
- Understand memory management – this is where C interviews go deep
- Know pointer arithmetic – essential for C-specific roles
- Be ready for output prediction – trace code manually
- Understand undefined behavior – know what's legal and what's not
- Practice tracing recursion – draw the call stack
- Know the compilation process – from source to executable
Summary
These 50 questions cover the breadth of C programming concepts tested in interviews. Focus on understanding the "why" behind each answer rather than memorizing. Interviewers appreciate candidates who can explain trade-offs, identify undefined behavior, and write clean, correct code. Practice implementing data structures from scratch and solving pointer-manipulation problems to build confidence.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Top 50 C Programming Interview Questions with Answers (2025).
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, interview, preparation, questions, top 50 c programming interview questions with answers (2025)
Related C Programming Topics