Programming Fundamentals · Interview question

Why is int main() not the same as int main(void)?

A strong answer

In C, an empty parameter list () historically means "unspecified parameters", the compiler won't check the call site at all, and you can pass any arguments without warning. An explicit (void) means "no parameters", the compiler verifies that callers pass nothing. For your own functions, this means int square() will accept any arguments without complaint, including the wrong number or types, hiding bugs at compile time. Always write (void) when a function takes no parameters. (C2x is finally making () mean (void), but until everyone's on that, be explicit.) The same applies to main: int main(void) is the unambiguous "main takes no arguments" form.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Functions

Naming a chunk of logic so you can call it from many places. Parameters in, return value out, scope rules: the building block of every program.

More Functions questions

Browse all 472 interview questions
Why is int main() not the same as int main(void)? | EmbeddedPrep.io