Katkat

Explain C printf pointers and memory layout

AristaSoftware Engineer · Technical Screen

Last 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):

  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?
  1. 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
  1. 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?