Every C++ program needs an entry point: main. The simplest program prints a line and returns an exit status to the operating system.
Minimal program
#include <iostream>
int main() {
std::cout << "Hello, World\n";
return 0;
}
#include <iostream> pulls in stream I/O. std::cout writes to stdout; return 0; signals success to the shell.
Reading main
int main()— playground default entry pointint main(int argc, char* argv[])— receives command-line arguments (covered in systems lessons)- Return type
intis the process exit code
Important interview questions and answers
- Q: Why include iostream?
A: Declaresstd::cout,std::cin, and related stream types; without it you get compile errors. - Q: cout vs printf?
A:std::coutis type-safe and extensible via operator overloading;printfis C-style and requires format strings.
Self-check
- Which header declares std::cout?
- What return value means success?
Tip: Always #include <iostream> for std::cout—unlike C's stdio.h, streams are type-safe.
Interview prep
- Why include iostream?
Declares
std::cout,std::cin, and stream types used for I/O.- What does return 0 mean?
Conventionally signals successful program termination to the operating system shell.