如果我的 C 函数出现错误,我应该返回什么?
目前,如果发生错误,我会在 C 语言的自定义函数中返回 -1,如果成功则返回 0。例如,使用链表和某些函数需要非空列表才能正常工作。如果作为参数传递的列表为空,则返回 -1(错误);如果不为空,则返回 0,并且函数正常运行。
也许我应该返回 1 而不是 -1 吗?
这是 C 中执行操作的标准方法还是您推荐不同的方法?
Currently I'm returning -1 in my custom functions in C if something wrong happens and 0 for success. For instance, working with a linked list and some function needs a non-empty list to work properly. If the list passed as argument is empty, I return -1 (error) and 0 if it's not empty and the function worked without a problem.
Should I, maybe, return 1 instead of -1?
Is this the standard way of doing things in C or do you recommend a different approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
返回非零值以指示失败。通过这种方式,您可以像这样编写函数调用:
此约定将允许您使用任何
!0
值来指示特定错误,并且这将允许您以统一的方式使用一个变量。因此,上例中所示的if
主体可以有一个 switch 语句来处理特定的错误。你可以用不同的方式来做——但如果你选择这样做,就坚持一个约定——不幸的是,win32 API(以及我使用过的其他 API)混合和匹配约定。
Return a non-zero value to indicate failure. This way you can write you functions calls as so:
This convention will allow you to use any
!0
value to indicate a specific error, and this will allow you to use one variable in a uniform fashion. So the body of theif
shown in the example above can then have a switch statement to process the specific errors.You can do it differently -- but if you choose to do so stick to a convention -- the win32 API (and other API's I have used) mix and match conventions unfortunately.
听起来不错。 -1 用于 I/O 函数,因为正返回值通常意味着成功,并且是已处理的字节数。如果函数有多种可能出错的方式,那么您可以返回不同的整数或设置一个全局错误变量(标准库使用 errno)来包含错误代码。
就风格而言,我不喜欢返回状态代码,因为这意味着我的函数不能(干净地)返回任何其他内容。相反,我会在调用函数之前检查输入。但这是主观的,取决于具体情况。
That sounds fine. -1 is used in I/O function because a positive return value usually means success and is the number of bytes that were processed. If you have multiple ways a function can go wrong, then you can either return different integers or set a global error variable (errno is used by the standard library) to contain the error code.
In terms of style, I prefer not to return status codes as it means my functions can't (cleanly) return anything else. Instead I would check the input before calling the function. But this is subjective and depends on the context.
我建议您找到一种方法在错误发生时格式化一个包含完整信息的人类可读字符串,其中所有信息仍然可用,并设计一种方法将其通过其余部分记录下来。该程序通过用户(他的手机)发送给您的开发团队进行分析。
在我看来,如果你想更快地生产更好的软件,这似乎是任何设计的一个重要特征。途中杀死错误信息是重大犯罪,C语言不是借口。
I suggest that you find a way to format a fully-informative human-readable string at the point of the error, where all information is still available, and design a way to get it down thru the rest of the program, thru the user, his mobile phone, to your development team for the analysis.
This seems to me like an important feature of any design, if you want to produce better software faster. Killing error information on the way is a major crime, and C language is no excuse.
计划有很多,但无论你做什么,都要坚持不懈地去做!
如果失败条件不多,则只需使用 0 表示错误。这是因为错误测试是以简单的方式编写的:
否则,低于零的答案可以与正常答案 >= 0 一起使用。您可以将其用于列表长度等函数,在正常情况下,该函数不会为负数。或者,如果您想要许多错误代码(-1 - 不存在,-2 - 未找到,-3 ...,...)
There are many schemes - but whatever you do, do it consistently!
If you don't have many failing conditions, just use 0 for error. That's because error tests are written in a simple way:
Otherwise below-zero answers are good to use along with normal answers >= 0. You can use this for functions like list length, which in normal conditions will not be negative. Or if you want many error codes (-1 - nonexistant, -2 - not found, -3 ..., ...)