is there any advantage in using int main() over void main()?.
and when you return a value in int main(), where does the returned value go?

Recommended Answers

All 6 Replies

The definition void main() { /* ... */ } is not and never has been C++, nor has it even been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1. A conforming implementation accepts int main() { /* ... */ } and int main(int argc, char* argv[]) { /* ... */ } A conforming implementation may provide more versions of main(), but they must all have return type int.

The int returned by main() is a way for a program to return a value to "the system" that invokes it. On systems that doesn't provide such a facility the return value is ignored, but that doesn't make "void main()" legal C++ or legal C. Even if your compiler accepts "void main()" avoid it, or risk being considered ignorant by C and C++ programmers.

Link with FAQs and other information

Member Avatar for iamthwee

Is the advice different for devices that do not necessarily have an operating system.

Like embedded devices though?

Is the advice different for devices that do not necessarily have an operating system.

Like embedded devices though?

Yes it would be different -- programs written for embedded devices without operating systems don't call exit() or return from main() because there is no place to return to. Embedded programming has a different set of standards then other ISO standards.

>Is the advice different for devices that do not necessarily have an operating system.
Freestanding implementations are excepted from a lot of the rules of hosted implementations. But since a programmer on a freestanding implementation is highly unlikely to be asking this question, and int main is always correct, the advice stands.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.