Benutzer-Werkzeuge

Webseiten-Werkzeuge


edv:prg:c:tipp:errno

errno

Bei errno gibt es keinen einheitlichen Standard. In einigen Implementierungen ist es eine int-Variable, in den anderen ist es ein Makro.

Folgendes konnte ich beim Debuggen von Programmen feststellen:

Unter Sun Solaris hatte ich mit der Variable

int errno;

zu tun. Die Funktionen, wie z.B. atoi(), legen dort den ErrorCode ab oder 0 bei Erfolg. D.h., durch die Auswertung der Variable errno kann man feststellen, ob die zuletzt aufgerufene atoi() (oder andere Funktion, die errno nutzt) erfolgreich war.

#include <errno.h>
 
int main()
{
	char szEineZahlAlsString[] = "12345";
	int iEineZahlAlsInteger = 0;
 
	iEineZahlAlsInteger = atoi(szEineZahlAlsString);
	if (errno == EINVAL || errno == ERANGE) printf("Fehler beim atoi(), errno = [%d]\n", errno);
 
	printf("szEineZahlAlsString = [%s], iEineZahlAlsInteger = [%d]\n", szEineZahlAlsString, iEineZahlAlsInteger);
 
	return 0;
} // main()

Unter MS VisualStudio 2010 hatte ich mit dem Makro

#define errno (*_errno())

zu tun. Falls eine "fehlgeschlagene" Funktion dort einen Fehlerwert abspeichert, setzt die nächste "erfolgreiche" Funktion diesen Fehlerwert u.U. nicht zurück! Als work around funktioniert, wann man vor den Funktionen wie atoi() manuell errno auf 0 zurücksetzt.

Auszug aus dem Kapitel ERRNO(3) (Linux Programmer's Manual): http://man7.org/linux/man-pages/man3/errno.3.html

  • errno is defined by the ISO C standard to be a modifiable lvalue of type int, and must not be explicitly declared; errno may be a macro. errno is thread-local; setting it in one thread does not affect its value in any other thread.
  • It was common in traditional C to declare errno manually (i.e., extern int errno) instead of including <errno.h>. Do not do this. It will not work with modern versions of the C library. However, on (very) old UNIX systems, there may be no <errno.h> and the declaration is needed.

Weitere Infos:
IBM: Errors: errno in UNIX programs
MS: errno, _doserrno, _sys_errlist, and _sys_nerr (MSDN, Visual Studio 2010)


Stand: 07.06.2016
: Jürgen Kreick

EOF

edv/prg/c/tipp/errno.txt · Zuletzt geändert: 2020/01/11 01:23 von 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki