When should we use asserts in C?

前端 未结 9 1741
孤街浪徒
孤街浪徒 2020-12-02 11:21

I am writing a function in C. As a matter of style, when is it good to use assert compared to returning an error code. Lets say the function is dividing two numbers. Should

9条回答
  •  Happy的楠姐
    2020-12-02 12:02

    Since C does not support exceptions you don't have any real option other than to return an error code. A failing C assert() results in abort() being called which bombs the process. That's not really comparable with standard error handling.

    For floating point operations you can use NaN to signal error conditions. For integer operations an error code is simply your only option.

提交回复
热议问题