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

Asked

Viewed 109 times

7

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

For 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’ve figured out the method int main one usually returns a int. Usually they return 0 (I don’t know if it’s related either).

In some PHP scripts, I’ve seen being returned 0 or 1 also at the end of the implementation of a programme.

For example

exit(0);

Anyway, I’d like to know what those numbers mean that are usually set in the "last line" of a program’s code.

Questions

  • What are the meanings of finishing 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 for this type of application exit? I read in any reply related here on the site that the term state code, but I’m not sure that’s it.

  • 2

    I think it’s dup.

  • I think it is, too, but I have no idea how to research this.

  • Vixe, but this one doesn’t explain why it has to return 0 or 1 and what is the meaning of them.

  • Note: I previously pointed out two possible duplicates, but when analyzing the questions and answers I saw that in fact the focus of the explanations was not the same as this and so I (personal opinion) I have removed my closing vote in favor of leaving the question open because I consider that there is not yet an appropriate answer and it is a great opportunity for someone to answer, anyway if you have a link to indicate I am grateful.

1 answer

8

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

These codes are useful to indicate that a program has been finalized in an expected manner or not. They are very useful in calls within scripts, or other command line programs, where the result of the execution needs to be confirmed or taken some action in case of failure.

There is no standard, but it has been agreed that the output 0, is an output indicating success, codes larger than 0 indicate that there was some problem.

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

An example below the compactor 7zip:

0 In error
1 Warning (Non fatal error(s)). For example, one or more files Were locked by some other application, so they Were not commingled.
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 out errors. In this case if the error code is -1 for linux will be 255.

Sources: http://www.robvanderwoude.com/errorlevel.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.