does perror() set errno?

here the program gives a odd result:

#include <stdio.h>
int main(){
    perror("first");
    perror("next");
    return 0;
}

result:

first: Success
next: Illegal seek

why? any resonable explanation? i found no information about this in man pages.
thanks in advance

perror uses errno.

all it does is the equivalent of sprintf your phrase along with the result of strerror(errno).
You are experiencing undefined behavior - meaning do not call perror twice like that.

You idea may or may not work correctly - undefined means the run time library writer decides what if anything some aspect of C coding will do. In your case it probably trashed errno after the first call.

Write your own perror() if you don't want to use it conventionally.

What Jim means is that errno is undefined after a successful library call. You should only call perror() immediately after a failing call.

As a matter of fact, the glibc implementation of perror() calls some other functions such as dup(), fdopen() that modify errno, even if perror() itself always succeed.

I would personally expect that clean library code doesn't expose their mess to outside. But we cannot complain, as POSIX/SUS allows such behavior.

Cheers,
Lo�c
--
My Blog: Lo�c OnStage

�UNIX is simple. It just takes a genius to understand its simplicity.� - Dennis Ritchie