What is the difference between
perror("hello");
and
fprintf(stderr,"not working");
and between exit and return????
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????...
What is the difference between
perror("hello");
and
fprintf(stderr,"not working");
and between exit and return????
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]
perror("hello"); is equivalent to:
Where, 'errno' is an extern variable, which holds the last return value of most of the standard functions.Code:fprintf(stderr, "%s: %s\n", "hello", strerror(errno));
exit() also closes all open files before returning control to the operating system, whereas return just returns to the operating system.