Namespaces
Variants
Views
Actions

errno

From cppreference.com
< c‎ | error
Revision as of 23:02, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <errno.h>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td class="t-dcl-nopad">
#define errno /*implementation-defined*/
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

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 the error codes, listed in <errno.h> as macro constants that begin 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.

Example

#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 (errno)
	{
		perror ("Ouuupsss");
		exit (EXIT_FAILURE);
	}
 
	close (fd);
 
	return EXIT_SUCCESS;
}

Output:

Ouuupsss: No such file or directory

See also

Template:c/error/dcl list errno macrosTemplate:c/io/dcl list perrorTemplate:c/string/byte/dcl list strerror