Why did void main() return a zero?

closed account (10oTURfi)
So... I had to see for myself that void main() doesnt return anything, so I wrote a simple batch file:
(note that return.exe is a program which does nothing but return value 1 and zero.exe is program with void main() {return;})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@ECHO OFF
CLS
start /w return
ECHO.Input file name of C++ program.
SET /P variable=File name: 
ECHO.Content of errorlevel before running program was %errorlevel%
ECHO.Starting C++ Program and waiting for it to finish...
start /w %variable%
IF ERRORLEVEL 1 GOTO code1
IF ERRORLEVEL 0 GOTO code0
GOTO end
:code0
ECHO.Program has finished successfully. (Exit code %errorlevel%)
GOTO end
:code1
ECHO.Program has failed to finish successfully. (Exit code %errorlevel%)
:end
PAUSE
CLS

This was on the screen:
Input file name of your C++ program.
File name: zero
Content of errorlevel before running program was 1
Starting C++ Program and waiting for it to finish...
Program has finished successfully. (Exit code 0)
Press any key to continue...


My question is: WHY?
void main() actually isn't legal in C++. A strict compiler would give you an error.

The compiler is probably just having main return 0 automatically.
Because the system expects and needs the program to return something, the compiler can't produce a program that returns nothing. Since you wrote an (illegal) main() that returns nothing, the compiler assumes you don't care what is returned and always returns success to the system.
Topic archived. No new replies allowed.