Explain C printf pointers and memory layout
Company: Arista
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Technical Screen
You are given the following C code:
```c
const char *c = "12345";
```
Answer the following (assume typical modern 64-bit Linux/macOS with GCC/Clang, unless a behavior is undefined/implementation-defined):
1. What does each of the following intend to print, and what actually happens?
- `printf("%s", c);`
- `printf("%d", c);`
- `printf("%c", c);`
- `printf("%c", *c);`
- `printf("%c", *(c+1));`
- What is the correct way to print the address stored in `c`?
2. Explain how you would use `gdb`/`lldb` to inspect:
- the value of `c` (as an address)
- the bytes at that address
- the string starting at that address
3. Memory and allocation fundamentals:
- Compare `malloc/free` vs `new/delete` in C/C++ (initialization, constructors/destructors, type safety, failure behavior, arrays, etc.).
- Describe what typically lives in the **stack**, **heap**, **data/BSS**, and **text (code)** segments. Where does a string literal like `"12345"` typically reside?
Overview: This question evaluates understanding of C pointer semantics, printf format-specifier behavior, memory layout and allocation differences, and practical debugger usage for inspecting addresses and bytes, within the Software Engineering Fundamentals domain.
Read the full Arista Software Engineer interview experience this question came from