Brak return 0 w funkcji main

0

Witam.
Dostałem zadanie napisania programu... wszystko ok, ale miałem zastrzeżone żeby nie zmieniać nic w funkcji main... (miałem tylko napisać odpowiednie funkcje). Zastanowiło mnie jedno... funkcja main była typu int a nie było pod koniec "return 0" . Mimo to program działa... czy to jest błąd autora zadania, czy ten "return" jest opcjonalny? Dodam, że program jest w Cpp, a nie czystym C.

Z góry dziękuję i pozdrawiam. :D

3

Zarówno w C¹ jak i w C++ main jest wyjątkowy, brak instrukcji return jest równoznaczny return 0.

¹ za wyjątkiem prawie 30-letniego C89.

0

Dzięki! :D

2

"if control reaches the end of main function

return 0;

is executed"
http://en.cppreference.com/w/cpp/language/return

Taki hack dla leniwych wpisany zdaje się w standardzie.
Osobiście wolę jednak wpisywać ten return nawet w main.
Kiedyś straciłem z tydzień na szukanie błędu który wynikał z braku return (nie w main).

2

C11
5.1.2.2.3 Program termination
1 If the return type of the main function is a type compatible with int, a return from the
initial call to the main function is equivalent to calling the exit function with the value
returned by the main function as its argument; reaching the } that terminates the
main function returns a value of 0.

C++17
3.6 Start and termination [basic.start]
3.6.1 Main function [basic.start.main]
...
A return statement in main has the effect of leaving the main function (destroying any objects with automatic
storage duration) and calling std::exit with the return value as the argument. If control flows off the end
of the compound-statement of main, the effect is equivalent to a return with operand 0

C++14
A return statement in main has the effect of leaving the main function (destroying any objects with automatic
storage duration) and calling std::exit with the return value as the argument. If control reaches the end
of main without encountering a return statement, the effect is that of executing
return 0;

Czyli jak wyżej - return w main jest opcjonalny.

1 użytkowników online, w tym zalogowanych: 0, gości: 1