0

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?

6
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by Narue
Featured Replies
  • 1

    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* … Read More

1

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.

Edited by happygeek: fixed formatting

0

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

Like embedded devices though?

0

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.

0

>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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.