Frage

I have seen many programs that has either int main() or void main(). I know both are used as the starting point of a program. I am just confused of when to use one and avoid another. I would really appreciate if you could explain it to me.

War es hilfreich?

Lösung

In the current ISO standard, it doesn't change anything, use what the other developers you work with expect. (credits to Lundin to remind me of that)

For ANSI C, int main means you will end your program with return 0; (or other value, 0 is the standard for "everything's fine").

void main will allow you to skip that line, and have some other effect, basically, depending on compiler, you may not be able to access argc and argv since the main take 0 arguments.

Although it doesn't do a lot of bad, it's better in my opinion to use int main, so you don't have to worry about the side effect. It's also the norm in ANSI C.

Andere Tipps

int main() simply returns a result code to the OS after the execution of your code. For example, returning 0 usually means a successful run, and returning anything else means there was probably an error. Other than debugging and maybe bug reporting, there really isn't much use for returning an int from the main function. And of course, void means that you don't return anything. Good luck!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top