What are the possible values for file descriptors? What are the possible values for file descriptors? c c

What are the possible values for file descriptors?


From the man page:

open() returns a file descriptor, a small, nonnegative integer.

and then:

open() and creat() return the new file descriptor, or -1 if an error occurred


When open fail, it returns -1, or 0xffffffff. It has no meaning but open failed:

Upon successful completion, the function shall open the file and return a non-negative integer representing the lowest numbered unused file descriptor. Otherwise, -1 shall be returned and errno set to indicate the error. No files shall be created or modified if the function returns -1.

The failure reason is stored in errno, you can read its value and check if it's one of the possible failures reasons EACCES, EEXIST, EINTR.. etc, or just use perror to print the error message.


Here's what a Linux manual page says:

open() and creat() return the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately).

Other systems may return other negative values in case of error.