对于成功的函数,我应该返回0还是1 ?(复制)

时间:2022-09-10 23:44:05

Possible Duplicate:
Error handling in C code
What return value should you use for a failed function call in C?

可能的重复:错误处理在C代码中,您应该为失败的函数调用使用什么返回值?

I always use 0, but its not really readable in if, while, etc.

我总是用0,但是在if, while,等等中不是很容易读懂。

Should I return 1? Why main function return 0 for success?

我应该返回1吗?为什么main函数成功返回0 ?

3 个解决方案

#1


38  

It's defined by the C standard as 0 for success (credits go to hvd).

C标准将成功定义为0(学分转到hvd)。

But

For greater portability, you can use the macros EXIT_SUCCESS and EXIT_FAILURE for the conventional status value for success and failure, respectively. They are declared in the file stdlib.h.

为了获得更大的可移植性,您可以分别使用宏EXIT_SUCCESS和EXIT_FAILURE来表示成功和失败的常规状态值。它们在文件stdlib.h中声明。

(I'm talking about the value returned to the OS from main, exit or similar calls)

(我说的是从main、exit或类似调用返回给操作系统的值)

As for your function, return what you wish and makes code more readable, as long as you keep it that way along your programs.

至于你的函数,返回你想要的并且使代码更可读,只要你在你的程序中保持它那样。

#2


12  

The reason why main use 0 for success is that it is used as the exit code of the application to the operating system, where 0 typically means success and 1 (or higher) means failure. (Of course, you should always use the predefined macros EXIT_SUCCESS and EXIT_FAILURE.)

成功使用0的主要原因是它被用作操作系统的应用程序的退出代码,其中0通常表示成功,1(或更高)表示失败。(当然,您应该始终使用预定义的宏EXIT_SUCCESS和EXIT_FAILURE。)

Inside an application, however, it's more natural to use zero for failure and non-zero for success, as the return value can directly be used in an if as in:

然而,在应用程序中,更自然的做法是在失败时使用0,而在成功时使用非0,因为返回值可以直接在if中使用,如:

if (my_func())
{
  ...
}

#3


-4  

If you are returning a boolean status, then you could typedef an int and use that. This makes the return type of your functions obvious and you (and others) won't have to check if it returns 0 or 1 for good.

如果您返回的是布尔状态,那么您可以定义一个int类型并使用它。这使得函数的返回类型很明显,您(和其他人)不必检查它是否返回0或1。

typedef int BOOL;
#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

BOOL myfunc()
{
   ...
   return TRUE;
}

#1


38  

It's defined by the C standard as 0 for success (credits go to hvd).

C标准将成功定义为0(学分转到hvd)。

But

For greater portability, you can use the macros EXIT_SUCCESS and EXIT_FAILURE for the conventional status value for success and failure, respectively. They are declared in the file stdlib.h.

为了获得更大的可移植性,您可以分别使用宏EXIT_SUCCESS和EXIT_FAILURE来表示成功和失败的常规状态值。它们在文件stdlib.h中声明。

(I'm talking about the value returned to the OS from main, exit or similar calls)

(我说的是从main、exit或类似调用返回给操作系统的值)

As for your function, return what you wish and makes code more readable, as long as you keep it that way along your programs.

至于你的函数,返回你想要的并且使代码更可读,只要你在你的程序中保持它那样。

#2


12  

The reason why main use 0 for success is that it is used as the exit code of the application to the operating system, where 0 typically means success and 1 (or higher) means failure. (Of course, you should always use the predefined macros EXIT_SUCCESS and EXIT_FAILURE.)

成功使用0的主要原因是它被用作操作系统的应用程序的退出代码,其中0通常表示成功,1(或更高)表示失败。(当然,您应该始终使用预定义的宏EXIT_SUCCESS和EXIT_FAILURE。)

Inside an application, however, it's more natural to use zero for failure and non-zero for success, as the return value can directly be used in an if as in:

然而,在应用程序中,更自然的做法是在失败时使用0,而在成功时使用非0,因为返回值可以直接在if中使用,如:

if (my_func())
{
  ...
}

#3


-4  

If you are returning a boolean status, then you could typedef an int and use that. This makes the return type of your functions obvious and you (and others) won't have to check if it returns 0 or 1 for good.

如果您返回的是布尔状态,那么您可以定义一个int类型并使用它。这使得函数的返回类型很明显,您(和其他人)不必检查它是否返回0或1。

typedef int BOOL;
#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

BOOL myfunc()
{
   ...
   return TRUE;
}