errno

Discussion in 'C Programming' started by Bill Cunningham, Mar 9, 2012.

  1. 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
     
    Bill Cunningham, Mar 9, 2012
    #1
    1. Advertising

  2. Bill Cunningham wrote:
    > 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
     
    Bill Cunningham, Mar 9, 2012
    #2
    1. Advertising

  3. Bill Cunningham

    Kiru Sengal Guest

    On Mar 8, 8:54 pm, "Bill Cunningham" <> wrote:
    >     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
     
    Kiru Sengal, Mar 9, 2012
    #3
  4. Bill Cunningham

    Don Y Guest

    Hi Bill,

    On 3/8/2012 6:54 PM, Bill Cunningham wrote:
    > 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!)
     
    Don Y, Mar 9, 2012
    #4
  5. Don Y <> writes:
    > On 3/8/2012 6:54 PM, Bill Cunningham wrote:
    >> 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).


    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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 9, 2012
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus Elfring

    conversion: errno => exception

    Markus Elfring, Nov 8, 2004, in forum: C++
    Replies:
    13
    Views:
    3,370
    Markus Elfring
    Jan 5, 2005
  2. Replies:
    3
    Views:
    376
    Ron Natalie
    Mar 4, 2005
  3. Marcia Hon

    errno 13

    Marcia Hon, Feb 9, 2004, in forum: C Programming
    Replies:
    3
    Views:
    481
    Mike Wahler
    Feb 9, 2004
  4. viza

    &errno, sizeof errno

    viza, Sep 12, 2008, in forum: C Programming
    Replies:
    20
    Views:
    1,000
    Tim Rentsch
    Sep 14, 2008
  5. Glenn Linderman

    errno 22 instead of errno 2

    Glenn Linderman, Jan 28, 2009, in forum: Python
    Replies:
    0
    Views:
    365
    Glenn Linderman
    Jan 28, 2009
Loading...

Share This Page