TRT
Sign in
easyFoundationsconcept

Who calls main and where does its return value go?

Explain startup code, chained insertions, namespaces, and the exit status of a minimal C++ program.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstAnatomy of Your First ProgramRead a six-line program token by token: who calls main, what std:: and the insertion operator really mean, how a chain of insertions works, and where the returned number goes.

A strong answer

A strong candidate frames Who calls main and where does its return value go? around the exact memory layout, compiler semantics, and hardware execution model covered in Anatomy of Your First Program. Explain startup code, chained insertions, namespaces, and the exit status of a minimal C++ program.

Six lines, and almost every token raises a question. Nobody in this file calls main, so what runs it? What is std:: doing in front of cout? How can one statement mix a piece of text, an addition and a line break, and print something sensible? And when the program says return 0, who is on the receiving end of that zero?

The last lesson followed a file through the build. This lesson reads the program itself, token by token, until none of those six lines is a mystery. Each answer connects to something you already know: the startup code the linker added, the header the preprocessor pasted in, and the symbol the linker had to resolve.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Name what each line of a minimal C++ program does, from the include to the closing brace.
  • Mastery of core invariant: Explain why the std:: prefix exists and what a namespace prevents.
  • Mastery of core invariant: Trace a chain of insertion operators and predict the text it writes.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01What happens if main has no return statement, and what status does the process report?

Not started

Sign in to save your learning progress.

Sign in to save