An error occurs in this part of the code:

if (acc > cutoff || (acc == cutoff && c > cutlim)) { any = -1; acc = ULONG_MAX; errno = ERANGE; } Ошибка: line 53: error: expected an expression errno = ERANGE; 

The source code of the function code is taken here: strtoul.c

The value of the variable is declared in errno.h. The variable itself is declared in the local file errno_local.h, which is connected to the .c file:

 #include "errno.h" #ifndef errno #define errno #endif 

As I read, the variable errno is declared in errno.h and is connected to files as

 extern int errno; 

But there it was not announced. So I created a local file. As far as I know, with this declaration, the variable automatically becomes an integer type. Right?

  • In general, errno is no longer just a variable, but a function or a set of define (because it would work normally with multithreading). Honestly, I don't understand why you added a variable declaration, which exists only in very ancient compilers. Add include errno and use the variable. - KoVadim

1 answer 1

Once before, in fact, errno could always be declared as

 extern int errno; 

Since now programs can potentially be multi-threaded, and errno for each thread (execution thread or thread) should be. own , the situation has changed.

Therefore, always just write in your programs.

 #include <errno.h> 

which will ensure the correct declaration of this variable (which logically does indeed correspond to extern int).

For example, in Linux, the file /usr/include/i386-linux-gnu/bits/errno.h (which connects from /usr/include/errno.h) errno is defined like this:

 # ifndef __ASSEMBLER__ /* Function to get address of global `errno' variable. */ extern int *__errno_location (void) __THROW __attribute__ ((__const__)); # if !defined _LIBC || defined _LIBC_REENTRANT /* When using threads, errno is a per-thread value. */ # define errno (*__errno_location ()) # endif # endif /* !__ASSEMBLER__ */ #endif /* _ERRNO_H */ 

Those. in fact, the memory allocated for the variable is hidden, and its current (actual) address is __errno_location() function.

By the way, you can remember this useful (in some situations) technique.

  • one
    Clarification: on Linux, the file /usr/include/i386-linux-gnu/bits/errno.h (which is connected from /usr/include/errno.h) - both of these files are not included in the linux program, but in the glibc library. - aleksandr barakin