|
| 1 | +# 🎯 C Programming Interview Preparation |
| 2 | + |
| 3 | +## �� **Quick Navigation** |
| 4 | +- [Common Questions](#common-questions) |
| 5 | +- [Problem-Solving Examples](#problem-solving-examples) |
| 6 | +- [Practice Problems](#practice-problems) |
| 7 | +- [Resources](#resources) |
| 8 | + |
| 9 | +## �� **Quick Reference: Key Concepts** |
| 10 | +- **Pointers & Memory**: Address arithmetic, pointer arithmetic, void pointers |
| 11 | +- **Volatile & Const**: When to use, common misconceptions |
| 12 | +- **Memory Management**: Stack vs heap, memory leaks, fragmentation |
| 13 | +- **Embedded C**: Bit manipulation, register access, optimization |
| 14 | + |
| 15 | +## �� **Common Interview Questions** |
| 16 | + |
| 17 | +### **Question 1: Explain the difference between volatile and const** |
| 18 | +**Why this matters**: Understanding these qualifiers is crucial for embedded systems. |
| 19 | + |
| 20 | +**Answer Structure**: |
| 21 | +- `const`: Prevents modification, enables compiler optimization |
| 22 | +- `volatile`: Prevents optimization, ensures memory access |
| 23 | +- Combined usage: `const volatile` for read-only hardware registers |
| 24 | + |
| 25 | +**Example**: |
| 26 | +```c |
| 27 | +// Read-only hardware register |
| 28 | +const volatile uint32_t* const STATUS_REG = (uint32_t*)0x40000000; |
| 29 | + |
| 30 | +// Compiler cannot optimize away this read |
| 31 | +uint32_t status = *STATUS_REG; |
| 32 | +``` |
| 33 | + |
| 34 | +**Follow-up Questions**: |
| 35 | +- When would you use `const volatile`? |
| 36 | +- How does this affect compiler optimization? |
| 37 | + |
| 38 | +### **Question 2: Implement a function to reverse bits in a byte** |
| 39 | +**Problem**: Write a function that reverses the bit order of an 8-bit value. |
| 40 | + |
| 41 | +**Solution Approach**: |
| 42 | +1. Use bit shifting and masking |
| 43 | +2. Consider lookup table for optimization |
| 44 | +3. Handle edge cases |
| 45 | + |
| 46 | +**Solution**: |
| 47 | +```c |
| 48 | +uint8_t reverse_bits(uint8_t byte) { |
| 49 | + uint8_t result = 0; |
| 50 | + for (int i = 0; i < 8; i++) { |
| 51 | + result = (result << 1) | (byte & 1); |
| 52 | + byte >>= 1; |
| 53 | + } |
| 54 | + return result; |
| 55 | +} |
| 56 | +``` |
| 57 | +
|
| 58 | +**Optimization**: Use lookup table for O(1) performance |
| 59 | +**Follow-up**: How would you optimize this for 32-bit values? |
| 60 | +
|
| 61 | +## 🧪 **Practice Problems** |
| 62 | +
|
| 63 | +### **Problem 1: Memory Layout Analysis** |
| 64 | +**Scenario**: Given this structure, what is the memory layout and size? |
| 65 | +```c |
| 66 | +struct example { |
| 67 | + char a; |
| 68 | + int b; |
| 69 | + char c; |
| 70 | +}; |
| 71 | +``` |
| 72 | + |
| 73 | +**Expected Answer**: |
| 74 | +- Size: 12 bytes (due to padding) |
| 75 | +- Layout: a(1) + padding(3) + b(4) + c(1) + padding(3) |
| 76 | + |
| 77 | +**Key Points**: Structure alignment, padding rules, memory efficiency |
| 78 | + |
| 79 | +### **Problem 2: Interrupt Safety** |
| 80 | +**Scenario**: Is this code safe to call from an interrupt handler? |
| 81 | +```c |
| 82 | +void isr_function(void) { |
| 83 | + static int counter = 0; |
| 84 | + counter++; |
| 85 | + // ... more code |
| 86 | +} |
| 87 | +``` |
| 88 | +
|
| 89 | +**Analysis**: |
| 90 | +- ✅ Safe: Static variable, no function calls |
| 91 | +- ⚠️ Consider: Volatile if shared with main loop |
| 92 | +- ❌ Avoid: Dynamic allocation, complex operations |
| 93 | +
|
| 94 | +## ✅ **Self-Assessment Checklist** |
| 95 | +
|
| 96 | +### **Basic Understanding** ✅ |
| 97 | +- [ ] Explain pointer arithmetic |
| 98 | +- [ ] Describe memory layout of structures |
| 99 | +- [ ] Understand volatile and const qualifiers |
| 100 | +
|
| 101 | +### **Problem Solving** ✅ |
| 102 | +- [ ] Can implement bit manipulation functions |
| 103 | +- [ ] Can analyze memory usage and optimization |
| 104 | +- [ ] Can identify potential issues in code |
| 105 | +
|
| 106 | +### **Advanced Concepts** ✅ |
| 107 | +- [ ] Understand compiler optimization effects |
| 108 | +- [ ] Can design memory-efficient data structures |
| 109 | +- [ ] Can implement interrupt-safe code |
| 110 | +
|
| 111 | +## 🔗 **Related Topics** |
| 112 | +- [Memory Management](../Embedded_C/Memory_Management.md) |
| 113 | +- [Type Qualifiers](../Embedded_C/Type_Qualifiers.md) |
| 114 | +- [Bit Manipulation](../Embedded_C/Bit_Manipulation.md) |
| 115 | +
|
| 116 | +## 📚 **Additional Resources** |
| 117 | +- **Books**: "C Programming: A Modern Approach" by K.N. King |
| 118 | +- **Online**: [Embedded.com C Programming](https://www.embedded.com/) |
| 119 | +- **Practice**: [LeetCode Embedded Problems](https://leetcode.com/) |
0 commit comments