简体   繁体   English

return 0 在 c 程序内部到底做了什么?

[英]what exactly return 0 does internally in a c program?

I came to know that while writing a c program we write "return 0" to tell the os that the program is executed successfully.我了解到,在编写 c 程序时,我们编写“return 0”来告诉操作系统程序执行成功。 My question is how can we tell the os while writing the program itself without even executing the program that the program had executed successfully.我的问题是我们如何在编写程序本身时告诉操作系统,甚至不执行程序已成功执行的程序。 Can someone tell me what exactly "return 0" does.谁能告诉我“return 0”到底是做什么的。

The return value of the main function is passed to the exit() function by the C startup code, providing the OS an exit status available to its parent process. main function 的返回值由 C 启动代码传递给exit() function,为操作系统提供对其父进程可用的退出状态。

The OS itself does not do anything with this exit status, but shell scripts and other programs invoking multiple processes may use the exit status to determine what to do next.操作系统本身不会对这个退出状态做任何事情,但是 shell 脚本和其他调用多个进程的程序可能会使用退出状态来确定下一步要做什么。 For example the make utility uses the exit status of the commands, eg: when invoking the C compiler, to proceed with the next command or stop the build process with an error status.例如, make实用程序使用命令的退出状态,例如:在调用 C 编译器时,继续执行下一个命令或以错误状态停止构建过程。

Returning 0 or EXIT_SUCCESS at the end of the main function is a convention to tell the calling process that the program completed successfully.main function 的末尾返回0EXIT_SUCCESS是告诉调用进程程序成功完成的约定。 It has become implicit in C99 so legacy programs that do not have a return statement can be considered to have completed successfully when recompiled with a C99 compiler.它在 C99 中变得隐含,因此当使用 C99 编译器重新编译时,可以认为没有return语句的遗留程序已成功完成。 Nevertheless, it is considered good style to provide the exit status explicitly.尽管如此,明确地提供退出状态被认为是一种很好的风格。

return 0 means returning the integer value 0 from a function, which is generally done inside a program. return 0表示从 function 中返回 integer 值 0,这通常在程序内部完成。

You are referring to exit(0) , which means that you stop a program successfully.您指的是exit(0) ,这意味着您成功停止了程序。 Generally, when you do exit(i) with i an integer value, then that i value is the error code, explaining what happened wrongly.通常,当您使用 integer 值执行exit(i) i ,该i值是错误代码,解释了发生的错误。 More information can be found here .更多信息可以在这里找到。

This is the well known for this question on stackoverflow :这是 stackoverflow 上这个问题众所周知的

The main function is generally supposed to return a value and after it returns something it finishes execution.主要的 function 通常应该返回一个值,并在它返回某些内容后完成执行。 The return 0 means success and returning a non-zero number means failure.返回 0 表示成功,返回非零数表示失败。 Thus we "return 0" at the end of main function.因此我们在主 function 的末尾“返回 0”。 But you can run the main function without the return 0.It works the same.但是您可以运行主 function 而无需返回 0。它的工作原理相同。

See discussion: What is the significance of return 0 in C and C++?见讨论: C 和 C++ 中返回 0 的意义是什么? . .

Once you finish writing your code and compiling it you could execute it wherever you want correct?一旦你完成编写代码并编译它,你可以在任何你想要的地方执行它吗? Once it is finished executing that piece of software you made will return a number (in your example zero).完成执行您制作的该软件后,将返回一个数字(在您的示例中为零)。

In most cases that do absolutely nothing because there is nobody actively looking for that return code.在大多数情况下,什么都不做,因为没有人积极寻找返回码。

But you might find yourself in situations where you want to execute that software in an automated environment or something like that.但是您可能会发现自己处于想要在自动化环境或类似环境中执行该软件的情况。 let's say a script, that calls your software, and if everything goes ok, it goes on to do something else, but if it is not ok it tries again.比方说一个脚本,它调用你的软件,如果一切正常,它会继续做其他事情,但如果不正常,它会再试一次。 That script or other software that is calling your first software has the power to read and interpret that return.调用您的第一个软件的脚本或其他软件有权读取和解释该返回。

You can also do that manually to actually see that a certain software return code is:您也可以手动执行此操作,以实际查看某个软件返回码是:

Windows: Windows:

  • open cmd打开 cmd
  • run the software运行软件
  • run echo Return Code: %errorlevel%运行echo Return Code: %errorlevel%

Linux: Linux:

  • open terminal打开终端
  • run software运行软件
  • run echo Return Code: $?运行echo Return Code: $?

Those pre-defined variables will give you the value your last command returned and you can do something about it.这些预定义变量将为您提供上一个命令返回的值,您可以对其进行处理。

By convention (as mentioned before) 0 means that everything was ok, and anything other than that indicates that there has been an error.按照惯例(如前所述),0 表示一切正常,除此之外的任何内容都表示出现错误。 If you write your code with multiple error checks, and when something goes wrong return different values.如果您编写的代码带有多个错误检查,并且当出现问题时返回不同的值。 you will know straight away what went wrong with your execution.你会马上知道你的执行出了什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM