Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding C errno

Tags:

c

libc

On my system errno defined as:

int *    __error(void);
#define errno    (* __error())

I understand errno is a macro and expands to * __error() function:

  1. I searched everywhere (source on my system) but I can't find the definition of the __error() function, can someone show/explain what would/should be the definition of it?

  2. How the expression errno = 0 works with the above definition (Assigning 0 to a function?)? Does errno = 0 expands to * __error() = 0?

Thanks

like image 727
Orion Avatar asked Oct 18 '25 15:10

Orion


1 Answers

The __error function returns a pointer to the errno variable for the calling thread. The errno macro dereferences that pointer, resulting in an lvalue that can appear on either side of an equals sign.

To answer your questions:

  1. The function determines the correct address for the errno variable for that specific thread. Each thread gets its own.

  2. Yes, it becomes (* __error()) = 0; which assigns 0 to that thread's errno variable.

like image 164
David Schwartz Avatar answered Oct 21 '25 03:10

David Schwartz