Quiz Entry - updated: 2026.06.23
What is the typical top-to-bottom structure of a C++ source file?
Includes → namespace declaration → preprocessor #defines → global variables → function definitions → main().
A C++ source file is read top-to-bottom by the compiler, and names must be declared before they are used. That dictates a conventional layout:
#include <iostream> // 1. include files
using namespace std; // 2. namespace declaration
#define LIMIT 50 // 3. preprocessor defines
int A = 0; // 4. global variables
int fib(int n) { // 5. function definitions
if (n > 1) return fib(n-1) + fib(n-2);
else return 1;
}
int main(void) { // 6. main function definition
int n; // (local variables live inside it)
cout << "Enter n: ";
cin >> n;
cout << "Fib(" << n << ") = " << fib(n) << endl;
return 0;
}
Why this order matters:
- The preprocessor (
#include,#define) runs first, pasting in headers and substituting macros before compilation. - Helper functions like
fib()appear abovemain()so they're already declared whenmain()calls them (otherwise you'd need a forward declaration/prototype). main()is the program's entry point and conventionally comes last.
Tip: This layout is essentially identical to C — C++ inherits the same single-pass, declare-before-use model.