什么是用C决定返回值一个很好的一般方法? [英] What is a good general approach for deciding return values in C?

查看:114
本文介绍了什么是用C决定返回值一个很好的一般方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序是用C语言编写的Linux,并有不同的图案很多函数的返回值:

1)一个或两个符 N 成功和 1 失败。结果
2)一些回报 0 成功和 1 失败。结果
3)对失败的成功和0一些回报1(我一般用唾弃一个boolean类型)。结果
4)指针返回 0 失败(我一般用唾弃 NULL )。

我的困惑出现在前三 - 函数指针返回总是返回 0 失败,这很容易

第一个选项通常涉及其返回的长度可能只有积极的作用。

第二个选项,通常涉及命令行处理功能,但我不能确定它的正确性,也许更好的价值将是EXIT_SUCCESS和EXIT_FAILURE?

第三个选项是为这是方便,自然要条件范围内调用的函数,而我平时在这里模仿布尔类型,使用 INT 值1和0

尽管这一切似乎相当明智的,我仍然觉得领域,这不是很清楚或明显,因为当我创建函数,或风格是在使用的时候我想用它来使用的风格。

所以,我怎么能在这里的返回类型决定何时增加清晰度我的做法?


解决方案

  

所以,我怎么能在这里的返回类型决定何时增加清晰度我的做法?


挑选的有一个的每个模式的返回类型,并坚持下去,否则你会自己开车疯了。模型中的一些长期建立了平台的约定你的格局:


  • 如果您正在大量的系统调用,比任何整数返回函数应该返回 1 失败。


  • 如果你没有做系统调用,您可以自由地按照为零,则该空调控制结构的公约表示成功,零表示失败。 (我不知道为什么你不喜欢布尔


  • 如果一个函数返回一个指针,功能不全者应通过返回指示 NULL


  • 如果一个函数返回一个浮点数,功能不全者应通过返回一个NaN的指示。


  • 如果一个函数返回一个全方位的符号和无符号整数,你可能不应该在编码返回值是成功还是失败。


返回值的测试是一个祸根C程序员。如果故障是罕见的,你可以写一个中央处理程序,考虑使用的例外宏包可以使用指示故障的longjmp

My program is written in C for Linux, and has many functions with different patterns for return values:

1) one or two return n on success and -1 on failure.
2) some return 0 on success and -1 on failure.
3) some return 1 on success and 0 on failure (I generally spurn using a boolean type).
4) pointers return 0 on failure (I generally spurn using NULL).

My confusion arises over the first three -- functions that return pointers always return 0 on failure, that's easy.

The first option usually involves functions which return a length which may only be positive.

The second option, is usually involved with command line processing functions, but I'm unsure it has correctness, perhaps better values would be EXIT_SUCCESS and EXIT_FAILURE?

The third option is intended for functions which are convenient and natural to be called within conditions, and I usually emulate a boolean type here, by using int values 1 and 0.

Despite this all seeming reasonably sensible, I still find areas where this is not so clear or obvious as to which style to use when I create the function, or which style is in use when I wish to use it.

So how can I add clarity to my approach when deciding upon return types here?

解决方案

So how can I add clarity to my approach when deciding upon return types here?

Pick one pattern per return type and stick with it, or you'll drive yourself crazy. Model your pattern on the conventions that have long been established for the platform:

  • If you are making lots of system calls, than any integer-returning function should return -1 on failure.

  • If you are not making system calls, you are free to follow the convention of the C control structures that nonzero means success and zero means failure. (I don't know why you dislike bool.)

  • If a function returns a pointer, failure should be indicated by returning NULL.

  • If a function returns a floating-point number, failure should be indicated by returning a NaN.

  • If a function returns a full range of signed and unsigned integers, you probably should not be coding success or failure in the return value.

Testing of return values is a bane to C programmers. If failure is rare and you can write a central handler, consider using an exception macro package that can indicate failures using longjmp.

这篇关于什么是用C决定返回值一个很好的一般方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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