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

后端 未结 8 1700
庸人自扰
庸人自扰 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条回答
  •  -上瘾入骨i
    2021-01-17 21:20

    One condition I can think of where your above methodology can fail is a function that can return any value including -1, say a function to add two signed numbers.

    In that case testing for -1 will surely be a bad idea.

    In case something fails, I would better set a global error condition flag provided by the C standard in form of errno and use that to handle error.
    Although, C++ standard library provides exceptions which takes off much hardwork for error handling.

提交回复
热议问题