您应该使用什么返回值在C函数调用失败? [英] What return value should you use for a failed function call in C?
问题描述
可能重复:结果
错误使用C code 处理
假设你有一个函数:
int MightWork(){
// if it works
return x;
// if it fails
return y;
}
应该是什么x和y是什么?
what should x and y be?
因为我有另一个功能:
if (MightWork){
// do stuff #1
}else{
// do stuff #2
}
我知道这个特殊的例子,使用一个返回值1将采取第二code块做的东西#1,用的返回值为0,将采取第二code块做的东西#2
I know for this particular example, using a return value of 1 will take the second code block to "do stuff # 1" and using a return value of 0 will take the second code block to "do stuff #2"
我的问题是什么是在C preferred风格做到这一点?不为0的函数的返回值表示成功,任何其他值表示失败?或相反亦然?或在0值?
My question is what is preferred style in C to do this? Does a return value of 0 for a function indicate success and any other value indicates failure? Or vice versa? Or values under 0?
我想确保我写我的C code。与现在的风格。谢谢!
I'd like to make sure I'm writing my C code with the current style. Thanks!
推荐答案
有关非指针成功/失败:
For non-pointer success/fail:
-
0
=>成功 -
1
=>失败
0
=> success-1
=> fail
有关指针功能:
-
NULL
=>失败 - 一切=>成功
这篇关于您应该使用什么返回值在C函数调用失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!