errno

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I have been trying to figure out how to use errno instead of having to
turn to perror. Do you just include errno.h in your source or use code
something like this...and this is what I am trying to understand.

if (errno==-1)
fprintf(stderr,"%d\n",errno);

Bill
 
B

Bill Cunningham

Bill said:
I have been trying to figure out how to use errno instead of
having to turn to perror. Do you just include errno.h in your source
or use code something like this...and this is what I am trying to
understand.
if (errno==-1)
fprintf(stderr,"%d\n",errno);

Bill

Ok I think I might have caught on now.
Thanks anyway.

Bill
 
K

Kiru Sengal

    I have been trying to figure out how to use errno instead of having to
turn to perror. Do you just include errno.h in your source or use code
something like this...and this is what I am trying to understand.

if (errno==-1)
fprintf(stderr,"%d\n",errno);

Bill

errno is never negative
Yes you can test it in if statements like that

Including errno.h gives you access to symbolic constants mapped to the
positive integers returned by errno.

So your if statements are better off being like: if (errno ==
ERANGE ) ...

look up strerror too
 
D

Don Y

Hi Bill,

I have been trying to figure out how to use errno instead of having to
turn to perror. Do you just include errno.h in your source or use code
something like this...and this is what I am trying to understand.

if (errno==-1)
fprintf(stderr,"%d\n",errno);

Include <errno.h> in your source file(s).

Before EVERY library call that *can* (potentially)
modify errno, explicitly SET IT TO ZERO.

After *each* such library call, test errno for
a non-zero value (indicative of an error).

The actual error values that are reported will
vary with your execution environment, etc.

If you are looking for a specific type of error
(e.g., ERANGE), make sure you refer to it symbolically
in your code since the actual values for these errors
will vary from system to system.

(N.B. errno *itself* might be a macro in some environments!)
 
K

Keith Thompson

Don Y said:
Include <errno.h> in your source file(s).

Before EVERY library call that *can* (potentially)
modify errno, explicitly SET IT TO ZERO.

After *each* such library call, test errno for
a non-zero value (indicative of an error).

Do so only if the function has returned a value indicating that it
failed. A function may set errno to a non-zero value even if it
succeeded (typically because some function that it called did so).

A non-zero errno value tells you why a call failed; it doesn't tell
you *whether* it failed.
The actual error values that are reported will
vary with your execution environment, etc.

If you are looking for a specific type of error
(e.g., ERANGE), make sure you refer to it symbolically
in your code since the actual values for these errors
will vary from system to system.

(N.B. errno *itself* might be a macro in some environments!)

Note that surprisingly few functions in the standard library are
required to set errno on failure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top