What is a good general approach for deciding return values in C?

后端 未结 8 1786
庸人自扰
庸人自扰 2021-01-17 20:40

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

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-17 21:18

    Much of the C standard library uses the strategy to only return true (or 1) on success and false (or 0) on failure, and store the result in a passed in location. More specific error codes than "it failed" is stored in the special variable errno.

    Something like this int add(int* result, int a, int b) which stores a+b in *result and returns 1 (or returns 0 and sets errno to a suitable value if e.g. a+b happens to be larger than maxint).

提交回复
热议问题