Why define output 0 or 1 at the end of a program?

6

I've noticed that some programs always have a code snippet, which is always executed last, which receives as a parameter a number, usually 0 or 1 .

For a better understanding, I put this example of a PyQt application:

#-*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)

win = QtGui.QWidget()

win.show()

ret = app.exec_()

#quando eu fecho a aplicação, retorna 0
print ret

# todos os tutoriais que li, ensinam a colocar essa linha 
# com esse retorno
sys.exit(ret)

My curiosities

In C, I already noticed that the int main method usually returns a int . Generally, they return 0 (I do not know if it's related too).

In some PHP scripts, I have already seen 0 or 1 being returned also at the end of a program's execution.

As for example

exit(0);

Anyway, I'd like to know what these numbers mean, which are usually defined in the "last line" of a program's code.

Questions

  • What are the meanings of ending a program with 0 or 1 ?

  • Only 0 and 1 should be used? Or are there other numbers, with specific meanings?

  • What is the name given to this kind of output from the application? I read in some related answer here on the site that the term state code , but I'm not sure if that is it.

asked by Wallace Maxters 02.01.2017 в 19:06
source

1 answer

7

These are called Return Codes , which became popular in Windows as ERRORLEVEL which is the name of the environment variable that gets its value when running an application. I believe the most correct translation is return codes .

These codes are useful for indicating that a program has been finalized as expected or not. They are very useful in calls within scripts, or other command line programs, where the execution result needs to be acknowledged or taken in case of failure.

There is no pattern, but it has been agreed that output 0 is an output indicating success, codes greater than 0 indicate that there has been a problem.

The programmer can create a list of outputs to encode if an error occurred or which error occurred.

An example below the compactor 7zip :

  

0 No error 1 Warning (Non fatal error (s)). For example, one or more   files are locked by some other application, so they were not   
2 Fatal error 7 Command line error 8 Not enough memory for   operation 255 User stopped the process

A curiosity for Linux is that error codes are represented by a byte. In some cases programmers return error codes with negative value to point errors. In this case if the error code is -1 for linux it will be 255.

Sources: link

    
answered by 02.01.2017 в 19:38