|
Vernon Hamberg wrote:
As you say, functions that set errno do not change it if they end successfully. That is why it is good practice in C to set it to 0 before any call to such a function. ...
I used to think this was the right way to use errno until a recent post by Scott Klement (maybe here, maybe on another forum) convinced me otherwise. I may even have been a (the?) source of the misleading information about errno in the Redbook; I don't recall. For the couple of cases like strtod() where errno is the only way to determine if an error occurred, and if so, those cases can be handled differently. (And from reading Simon's post, even strtod() has a non-errno way of checking for error.) But aside from right or wrong, I think Scott's way is just plain nicer. It doesn't interrupt the normal flow with the assignment to errno, and the checking for an error is directly related to the call that is being checked. parm = something; errno = 0; // this line gets in the way retval = somefunction(parm); if errno <> 0; // check seems unrelated to the call --- handle the error endif; vs parm = something; retval = somefunction(parm); if retval indicates failure; --- handle the error, possibly referring to errno endif; A programming rule of thumb: "If it looks nicer, it's better." (This rule of thumb, more than most, should be taken with a huge grain of salt. Still, I find it useful, along with a related rule "If it looks icky, it's got bugs.")
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.