c - 我应该为成功的函数返回 0 还是 1?

标签 c boolean

<分区>

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

我总是使用 0,但它在 ifwhile 等中并不是真正可读的。

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

最佳答案

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.

(我说的是从 mainexit 或类似调用返回到操作系统的值)

至于你的函数,只要你在你的程序中保持这种方式,就返回你想要的并使代码更具可读性。

关于c - 我应该为成功的函数返回 0 还是 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9549342/

相关文章:

c++ - 在 C++ 中将 bool 转换为文本

python - 如果字符串不在一个或多个列表中则引发错误

c - 在不修改 Windows header 的情况下无法使用 VC2015 构建 WDK 10 示例

c - 为什么fgetc函数要加回车

c - qsort函数比较让我困惑

.net - 解析 XML boolean 属性(在 .NET 中)的最佳方法是什么?

python - 在二维 boolean 数组中查找最顶层 True 值索引的有效方法(Python)

java - boolean 值与整数的内存/大小?

c - 存储和打印大于 2^64 的整数值

c - 从字符集高效地创建顺序词表