Reputation: 3941
I was learning about Linux Kernel in one of the course. I got to know that the C library returns -1
on error and sets the errno
variable to actual error. Below is the lecture slide which says the same thing.
My question is, why can't the C library return the return code directly instead of returning -1
?
Note: This question may seem duplicate of this one. But referenced question answers what, my question is more related to the why.
Upvotes: 0
Views: 746
Reputation: 76
Summary: C in itself doesn't know what exactly has gone wrong, it just knows something is wrong. So to maintain generic standard it just returns -1 as a signal to notify the caller that error has occurred, now it is up to the caller to check the errno for what exactly.
Brief Explanation: The reason is that all the errno returned from the kernel are defined in the kernel itself.
C is just a generic programing language that receives the errno from the kernel and sets the variable according to it and returns -1 as a signal to indicate an error or failure. So C language in itself doesn't know what exactly has gone wrong, it just receives a signal from the registered error handler of the kernel that some error has occurred, and this error handler then supplies the actual error code.
So for maintaining generic nature C just returns -1. This is useful in other scenarios, lets say you were using C for some other purpose like embedded systems development. Now here several error codes are different from the Linux kernel error codes. So how does C know what error code to return if something goes wrong in the embedded system?
The answer is C doesn't actually know what is wrong. The registered error handler of the system will signal your C program that something has gone wrong and give you the appropriate error code for it and C will return -1 and set the errno.
Now, why can't C return the Errno: Answer is to maintain generic design. This allowed C developers to not care much about the return type of the error signal. If you interpret it correctly -1 returned by the C program is just a generic signal to userland API or shell script invoking the C program that something has gone wrong and if the invoker of C program needs to know what exactly has gone wrong they can check up the errno variable.
Upvotes: 1
Reputation: 1494
I think there are some possible reasons.
Firstly, this design is normal in C/C++, we get a return noted successful/fail, and if you want to get detail error message you can get it. This makes the return more uniform, and it becomes a normal use way. What's more, there maybe some time you just need to check whether a function works or not, you don't need to get the answer.
Secondly, this may be a historical design remained. There are many old desgin of C/C++, the reason we still remain them is because the cost of change it is too heavy and we don't get anything wrong if we don't change it.
I'm not sure my answer is totaly right, they're just some possible reasons and may help you.
Upvotes: 0