errno - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# errno

From cppreference.com
< c‎ | error

Defined in header `<errno.h>` |  |
#define errno /*implementation-defined*/ |  |

`errno` is a preprocessor macro that expands to a thread-local modifiable lvalue of type int. Several standard library functions indicate errors by writing positive integers to `errno`. Typically, the value of `errno` is set to one of the error codes listed in `<errno.h>` as macro constants beginning with the letter `E` followed by uppercase letters or digits.

The value of `errno` is ​0​ at program startup, and although library functions are allowed to write positive integers to `errno` whether or not an error occurred, library functions never store ​0​ in `errno`.

### [edit] Example

Run this code

    #include <stdio.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdlib.h>

    int main (int argc, char **argv)
    {
    	int fd = -1;
    	fd = open ("/dev/zer0", O_RDONLY);

    	if (fd == -1)
    	{
    		/* perror uses errno */
    		perror ("Ouuupsss");
    		exit (EXIT_FAILURE);
    	}

    	close (fd);

    	return EXIT_SUCCESS;
    }

Output:

    Ouuupsss: No such file or directory

### [edit] Example

Run this code

    #include <stdio.h>
    #include <math.h>
    #include <errno.h>

    void show_errno(void)
    {
        if(errno==EDOM)   printf("domain error");
        if(errno==EILSEQ) printf("illegal sequence");
        if(errno==ERANGE) printf("pole or range error");
        if(errno==0)      printf("no error");
        printf(" occurred\n");
    }

    int main()
    {
        printf("math_errhandling & MATH_ERRNO = %d\n", math_errhandling & MATH_ERRNO);

        errno = 0;
        acos(+1.1);
        show_errno();

        errno = 0;
        log(0.0);
        show_errno();

        errno = 0;
        sin(0.0);
        show_errno();

        return 0;
    }

Output:

    math_errhandling & MATH_ERRNO = 1
    domain error occurred
    pole or range error occurred
    no error occurred

### [edit] See also

E2BIG, EACCES, ..., EXDEV | macros for standard POSIX-compatible error conditions (macro constant) [edit]
perror | displays a character string corresponding of the current error to stderr (function) [edit]
strerror | returns a text version of a given error code (function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/error/errno&oldid=71759"