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

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

问题描述

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

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

我总是使用0,但在ifwhile等中却无法真正读取.

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_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.

(我说的是从 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屋!

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