It's bad design to have overlapping data and error domains.
It is brittle to change, say, you want to add a new error code, now you have audit all calls to your function to ensure that other value is being used for something important. You need to guard against your magic values being inserted, otherwise you cannot tell an error from a pop()
of that value. As this implements an abstract data type (ADT) you may find you want versions for other types, say, long, or double, and here INT_MIN makes little sense, so you would have to adapt it for each type.
You need to document that INT_MIN
means error, and how you can't use it as a value to push()
. You would want to use a better constant name. It will not be obvious to someone reading your code that return INT_MAX
means there was an error, so you ought to write comments for your future self.
The size of int
is platform specific. For example, if you log error code INT_MAX, it might be -2^31 on system and -2^63 on another.
Ideally, you want a distinct error code for each error beneficial for client, which is probably more than 1 error code. A library, like this, is much more reusable if you leave the UI to the calling code (have caller printf()
error messages).
There are many valid design options, btw, and you may benefit for reviewing the framework I outlined in my answer to Error handling in C code.