#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    48
    Rep Power
    2

    Printf not working


    Yeah, I know, printf is sooo easy to use how could I have a brain and not know how to use it?

    Here's the code:

    (printf("dhlaf: %s: Out of range error.\nValid range is 1-65536 "
    "inclusive.", argv[*i])) ? raise(SIGHUP): raise(SIGABRT);

    Why does printf fail?
    i.e. I get a SIGHUP not a SIGABRT.
    i is a pointer to a signed int.
    I was writing a bunch of checks into my program so that the "impossible" was detected and reported to the user (me.)
    This message was supossed to be printed if the range of numbers was not met, but was small or too large.
    Unfortuatly only the first half of the printf successeds (up to the \n.)
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    printf returns the number of characters printed, or a negative value if an output error is encountered. Since that value is non-zero, you always get a SIGHUP. Why would you expect a SIGABRT?

    The "missing" part of the output is probably still in the output buffer when the SIGHUP hits. Try appending another '\n' at the end (ie "inclusive.\n") to try to flush it out. Doing (printf(), fflush(stdout)) would also work, but that would change the evaluation of the conditional.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    48
    Rep Power
    2
    Thanks!
    I must have gotten printf mixed up with so many other of the functions in c which return 0 for success and something else for failure.

IMN logo majestic logo threadwatch logo seochat tools logo