为成功执行功能,我应该返回0还是1? [英] Should I return 0 or 1 for successful function?
问题描述
Possible Duplicate:
Error handling in C code
What return value should you use for a failed function call in C?
我总是使用0,但在if
,while
等中却无法真正读取.
I always use 0, but its not really readable in if
, while
, etc.
我应该返回1吗?为什么主功能return 0
成功?
Should I return 1? Why main function return 0
for success?
推荐答案
C标准将其定义为成功的0
(贷记至hvd).
It's defined by the C standard as 0
for success (credits go to hvd).
要获得更大的可移植性,可以使用宏
EXIT_SUCCESS
和EXIT_FAILURE
表示成功的常规状态值, 失败,分别.它们在文件stdlib.h
中声明.
For greater portability, you can use the macros
EXIT_SUCCESS
andEXIT_FAILURE
for the conventional status value for success and failure, respectively. They are declared in the filestdlib.h
.
(我说的是从 main , exit 或类似调用返回到操作系统的值)
(I'm talking about the value returned to the OS from main, exit or similar calls)
对于您的函数,只要您在程序中保持这种方式,就返回您想要的内容并使代码更具可读性.
As for your function, return what you wish and makes code more readable, as long as you keep it that way along your programs.
这篇关于为成功执行功能,我应该返回0还是1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!