Explain C printf pointers and memory layout
AristaSoftware Engineer · Technical ScreenLast updated
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.
You are given the following C code:
const char *c = "12345";
Answer the following (assume typical modern 64-bit Linux/macOS with GCC/Clang, unless a behavior is undefined/implementation-defined):
- 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?
- Explain how you would use
gdb/lldbto inspect:
- the value of
c(as an address) - the bytes at that address
- the string starting at that address
- Memory and allocation fundamentals:
- Compare
malloc/freevsnew/deletein 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?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.