perrror

This is a discussion on perrror within the C Programming forums, part of the General Programming Boards category; What is the difference between perror("hello"); and fprintf(stderr,"not working"); and between exit and return????...

  1. #1
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    perrror

    What is the difference between

    perror("hello");

    and

    fprintf(stderr,"not working");

    and between exit and return????

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    perror() uses errno to print out a meaningful error message, whereas your fprintf() example prints out a static message. For example, if you use perror() after failing to fopen() a file, you'll get a message telling you *why* the open failed.

    return/exit - in short, return takes you back to the calling function, exit terminates the program immediately.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    perror("hello"); is equivalent to:
    Code:
    fprintf(stderr, "%s: %s\n", "hello", strerror(errno));
    Where, 'errno' is an extern variable, which holds the last return value of most of the standard functions.

  4. #4
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    exit() also closes all open files before returning control to the operating system, whereas return just returns to the operating system.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21