Katianie - 1 year ago 45

C Question

Lets say for example we have a function that does some math (for the sake of argument, multiply two numbers). It returns the result but if there is an error, the function returns -1.

However, -1 can actually a valid result (-1 * 1 for example). What is a better constant to return? I was thinking of returning INT_MAX on error but some of my math functions return double and float so I wanted to see if there was a better alternative.

This is for a library so I want to use the same constant for errors to minimize confusion.

Answer Source

If you're looking for the least likely integer constant to hit by accident, use `INT_MIN`

. It's one larger in magnitude than `INT_MAX`

. It also has the advantage of being copyable to `float`

without losing its value, `INT_MAX`

will get rounded to a different value.

Of course this is only if you need compatibility with C. In C++ you really should be using exceptions.