3 ms·
> I think the better question is, why does C (and C derived languages) require a main function? That's not the true question. The true question would be, "why
by cesarb 6y ago
> I think the better question is, why does C (and C derived languages) require a main function?
That's not the true question. The true question would be, "why does C (and C derived languages) require that all executable statements be within a function?" Requiring a main function is just a natural consequence of that.
- crazygringo 6y agoAgreed. So... why does it?
- adonovan 6y agoBecause it's simpler. C has no concept of nested functions. If it instead allowed users to write the statements of 'main' at the top level, it would complicate the rules for scoping. For example, can an inner function access the outer function's variables? Or jump to its labels? Simpler just to say: all statements live in a function; functions don't nest; and execution starts at main.