3 ms·
What if the number you want to return just happens to be the value of ERROR? You need an error flag that can't be represented as an int, but then C wouldn't le
by fhdkweig 5mo ago
What if the number you want to return just happens to be the value of ERROR? You need an error flag that can't be represented as an int, but then C wouldn't let you return it from a function that only returns "int". It is why some languages throw exceptions and why databases have the special "null" value.
- voidUpdate 5mo agoI don't use C enough to know what the convention is for throwing an error when the function can return a number anyway. You'd have to ask someone else
- zbentley 5mo agoIn C, errors are usually indicated by a negative return value constant, crashing the program with abort, or setting the errno global (thread-local, but whatever) and expecting callers to check it. Sometimes multiple of those.
- QuercusMax 5mo agoOne reasonably common pattern is to have the return value indicate success / error, and you pass in a pointer to the value which will be mutated if successful.
- jamesfinlayson 5mo agoYep, lots of the Windows API does it this way.
- jerf 5mo agoAnd why some very, very special languages have an effectively-global variable called "errno" that you have to check after the call manually, and worry about whether maybe it was populated from some previous error. Nothing says "production-quality language that an entire civilization's code base should be based on" like "sometimes (but only sometimes!) functions return additional information through global values".
- aleph_minus_one 5mo ago> And why some very, very special languages have an effectively-global variable called "errno" that you have to check after the call manually, and worry about whether maybe it was populated from some previous error. As you can read at https://en.wikipedia.org/wiki/Errno.h https://en.wikipedia.org/wiki/Errno.h errno is barely used by the C standard (though defined there). It is rather POSIX that uses errno very encompassingly. For example the WinAPI functions use a much more sensible way to report errors (and don't make use of errno).