C语言 那么“返回 0”究竟是什么意思呢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14928520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:22:54  来源:igfitidea点击:

So what does "return 0" actually mean?

creturn

提问by Starkers

I'm pretty proficient in PHP, but I've started dabbling with C. I've seen the code

我对PHP相当精通,但我开始涉足C。我看过代码

return 0;

at the end of functions that don't return a value. This isn't used in PHP, because if a function is doesn't have a return, a value NULL is automatically returned.

在不返回值的函数的末尾。这在 PHP 中不使用,因为如果函数没有返回值,则会自动返回值 NULL。

All I'm asking is, in simple English, what does the return 0actually do? Is it like PHP, where it returns its argument as the value of the function call? Is it just good practice?

我要问的是,用简单的英语,return 0实际上是做什么的?它是否像 PHP 一样,将其参数作为函数调用的值返回?这只是好的做法吗?

I know this question has been asked many times before, but I'm asking it from the point of view of a PHP developer. The answers google throws up haven't been that concise.

我知道这个问题以前被问过很多次,但我是从 PHP 开发人员的角度提出的。谷歌抛出的答案并没有那么简洁。

回答by Aniket Inge

Is it like php, where it returns its argument as the value of the function call? Is it just good practise?

它是否像 php 一样,将其参数作为函数调用的值返回?这只是好的做法吗?

Yes, PHP and many other languages borrowed the returnkeyword from 'C'. And in all the languages, the returnkeyword has the same function - to return from the function. Anything that follows returnkeyword is the value that is returned to the caller.

是的,PHP 和许多其他语言return从“C”中借用了关键字。并且在所有语言中,return关键字都具有相同的功能——从函数中返回。return关键字后面的任何内容都是返回给调用者的值。

Is it a good practise? Yes and No. Not all functions shouldreturn a value. And quite a few in the standard library even, do not return any value. Hence their return type is void.

这是一个好习惯吗?是和否。不是所有的函数都应该返回一个值。甚至相当多的标准库中,都不返回任何值。因此它们的返回类型是void.

But mainfunction should return 0(also EXIT_SUCCESS) to identify that the program has executed successfully. And -1 otherwise (also EXIT_FAILURE)

但是main函数应该返回0(也EXIT_SUCCESS)以识别程序已成功执行。和 -1 否则(也EXIT_FAILURE

EDIT: (Thanks to @KeithThompson):

编辑:(感谢@KeithThompson):

EXIT_FAILUREis implementation defined. 1is a common value of EXIT_FAILUREbut the whole point is, you need not know.

EXIT_FAILURE是实现定义的。1是一个共同的价值,EXIT_FAILURE但重点是,你不需要知道。

回答by Brooks Moses

For historic reasons, it is possible to write return 0;to return from a function that has been declared as void, like so:

由于历史原因,可以编写return 0;从已声明为 的函数返回void,如下所示:

void foo( /* arguments */ )
{
  /* do things */
  return 0;
}

This does nothing, and the 0(or whatever you put there) is thrown away. Also, sensible compilers will give you a warning message if you do this. So don't do this.

这没有任何作用,并且0(或您放在那里的任何东西)都被扔掉了。此外,如果你这样做,明智的编译器会给你一个警告信息。所以不要这样做。

回答by John

Functions in C return int by default, if no other return type is defined. return 0 would be good practice to make sure the function returns a known value, as opposed to some random value, in case the caller is looking at the return value.

如果没有定义其他返回类型,C 中的函数默认返回 int。return 0 将是一个很好的做法,以确保函数返回一个已知值,而不是一些随机值,以防调用者正在查看返回值。

回答by rocky

It is literally returning an int of 0.

它实际上是返回一个整数 0。

回答by KRUPASINDHU MOHANTY

The C programming language allows programs exiting or returning from the main function to signal success or failure by returning an integer, or returning the macros EXIT_SUCCESSand EXIT_FAILURE. On Unix these are equal to 0 and 1 respectively. A C program may also use the exit()function specifying the integer status or exit macro as the first parameter.

C 编程语言允许程序退出或从主函数返回,以通过返回整数或返回宏EXIT_SUCCESS和来表示成功或失败EXIT_FAILURE。在 Unix 上,它们分别等于 0 和 1。AC 程序也可以使用exit()指定整数状态或退出宏的函数作为第一个参数。

Apart from the macros EXIT_SUCCESSand EXIT_FAILURE, the C standard does not define the meaning of return codes. Rules for the use of return codes vary on different platforms.

除了宏EXIT_SUCCESSEXIT_FAILURE,C 标准没有定义返回码的含义。返回码的使用规则因平台而异。

回答by enjoytech

In C you don't have to return a value only if you declare a function with void at the start of it. First example:

在 C 中,只有在声明一个以 void 开头的函数时才必须返回值。第一个例子:

#include <stdio.h>
 int main()
{
   printf("Hello World!");
    return 0; // you have to use return because main starting with int
}

Second Example:

第二个例子:

#include <stdio.h>
void main()
{
printf("Hello World!");
//in this case return is useless, main is a void function

}