Search code examples
cpowershellexit-code

Powershell return value after execute file.exe


I created a simple test.c code:

int main()
{
    int a;
    return -1;
}

Compiling with: gcc -o test test.c

Later I executed it: .\test.exe

Is it possible to see the return value in the terminal? not a log file.


Solution

  • PowerShell reports the process exit code of the most recently executed console application in the automatic $LASTEXITCODE variable.

    The related automatic $? variable is an abstract success indicator, reporting $true or $false, for all commands; in the case of console applications, a process exit code of 0 maps to $true, any other value to $false.

    For reasons explained in this answer, it is better to use $LASTEXITCODE directly for console applications ($LASTEXITCODE -eq 0 to test for success) , because in PowerShell versions prior to 7.2, $? can yield false negatives.