Mainly we used to write "static void main" for entry point in console application. Placement of void denotes return type. In main function we could have "int" too but what does it really mean. "int main" signifies return type as integer.
The return type of main function tells about execution status of application. Even if we have specified void as return type then it would be marked as successful program execution. If we mark int as return type then we are able to control the execution status.
Now, what is the benefit of making main function as int. Windows OS saves result in %ERRORLEVEL% environment variable of OS. If we create batch file and execute application through it then we will able to get status and based on result we can trigger something else through batch file.
Let's suppose we have created program called TEST.EXE.
Batch file script:
@echo off
REM Execute main program
REM TEST.EXE
@if "%ERRORLEVEL%" == "0" goto success
:fail
echo This application has failed!
goto end
:success
echo This application has succeeded!
goto end
:end
echo return value = %ERRORLEVEL%
echo All Done.
Save above code as "run.bat".
Now, if try changing returning value in main function then we can get execution status accordingly.
0 is meant for successful execution.
C# code:
internal class Program
{
private static int Main()
{
return -1;
}
}
The return type of main function tells about execution status of application. Even if we have specified void as return type then it would be marked as successful program execution. If we mark int as return type then we are able to control the execution status.
Now, what is the benefit of making main function as int. Windows OS saves result in %ERRORLEVEL% environment variable of OS. If we create batch file and execute application through it then we will able to get status and based on result we can trigger something else through batch file.
Let's suppose we have created program called TEST.EXE.
Batch file script:
@echo off
REM Execute main program
REM TEST.EXE
@if "%ERRORLEVEL%" == "0" goto success
:fail
echo This application has failed!
goto end
:success
echo This application has succeeded!
goto end
:end
echo return value = %ERRORLEVEL%
echo All Done.
Save above code as "run.bat".
Now, if try changing returning value in main function then we can get execution status accordingly.
0 is meant for successful execution.
C# code:
internal class Program
{
private static int Main()
{
return -1;
}
}
Comments
Post a Comment