我应该为成功的功能返回 0 还是 1? [英] Should I return 0 or 1 for successful function?

查看:16
本文介绍了我应该为成功的功能返回 0 还是 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我应该返回 1 吗?为什么main函数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_SUCCESSEXIT_FAILURE 表示成功的常规状态值,分别失败.它们在文件 stdlib.h 中声明.

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 或类似调用返回给操作系统的值)

(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆