Q1. Explain the difference between `volatile` and `const` keywords in C for embedded systems, and provide a scenario where each is critical.
Why you'll be asked this: This question assesses your understanding of fundamental C language features crucial for embedded programming, especially when dealing with hardware registers, memory-mapped I/O, and compiler optimizations that can lead to unexpected behavior.
Start by defining `volatile` (prevents compiler optimization for variables that can change unexpectedly, e.g., hardware registers, shared memory in multi-threaded contexts). Provide an example like reading a status register. Then define `const` (declares a variable whose value cannot be changed after initialization, useful for read-only data in ROM/Flash). Give an example like a lookup table or configuration parameters. Emphasize how both prevent common embedded bugs.
- Confusing the two keywords or providing incorrect definitions.
- Failing to provide practical, embedded-specific use cases.
- Not mentioning compiler optimizations in relation to `volatile`.
- How does `static` differ from `const` in scope and lifetime?
- Can a `volatile const` variable exist? If so, why would you use it?