August 19th, 2013, 03:53 PM
How to check library functions for return errors
Should I check the return value or errno or both?
August 19th, 2013, 11:37 PM
I think it depend on your requirements.
if you just want to know that your function is succeed or failed then there is no need to check errno.
but if you want to know exact reason of error then you can check errno.
return value only tell you that function was successful or failed but dont give you exact reason of failure.
August 20th, 2013, 09:31 AM
RTFM. Read The Manual.
In the documentation for each function, the return values are described. Part of that description is what is returned in case of an error. That is not the same for every single function, so you need to RTFM.
Once you have detected that error, that is when you use errno to find out what kind of error you got. Part of the documentation for a function is a list of the errors that it could encounter and what could have caused that error. You could also use a function like perror to print out a description for that error.
BTW, errno is set to the last error encountered. If your function call did not result in an error, then errno would still be set to the last error encountered before your successful function call. It is therefore necessary to first detect whether an error occurred before you consult errno.
Library function documentation can be found in the man pages, thousands of copies of which are on-line. Google on man page and the function you are looking up.
August 21st, 2013, 04:13 PM
Yes, I've read it a thousand times... "Set errno to 0 before calling a function."