-10
void quit()
{
fprintf(stderr, "memory exhausted\n");
exit(1);
}

Why do they use fprintf? Also, what is stderr and why is it exit(1) if that part of the program is exiting successfully? Cheers

4 Answers4

3

To backtrack a bit, printf is a less generic version of fprintf that uses the standard output stream, also known as stdout. stderr is another standard stream, however it's typically used to output errors encountered by the program. You can redirect the two streams differently so you only see the errors or the output if you so desire.

http://en.wikipedia.org/wiki/Standard_streams

exit(1) is simply a convention to show that the program has exited unsuccessfully. In truth, any exit code other than 0 typically means a failure in the C standard.

GriffinG
  • 667
  • 4
  • 13
1

exit(1) tells the operating system (or whatever invoked your program) that the result of the program was 1.

The meaning ascribed to this by the operating system depends on the system, e.g. in Unix-style shells it means failure, and in VMS it means success.

There is a portable macro defined in stdlib.h called EXIT_FAILURE, which will indicate the appropriate failure code for the operating system in use.

There is also a macro EXIT_SUCCESS, however it is defined that 0 and EXIT_SUCCESS both return a success code, so you can just exit(0).

Doing exit is similar to returning from main. Obviously when you exit you bypass any code that would have run in the process of getting back to main from where you are now.

M.M
  • 130,300
  • 18
  • 171
  • 314
0

fprintf is a more generic version of printf where the output is sent to a FILE. stderr is the standard error output, so fprintf(stderr, "memory exhausted\n"); write "memory exhausted" to the standard error output.

fbafelipe
  • 4,712
  • 1
  • 22
  • 39
0

The printf() function generally outputs to the stdout stream. However, there are other streams that accept output, including stderr. To the casual observer, it might appear that these two streams are the same; which both appear to simply send their content to the terminal screen.

-

However, this separation of stdout an and stderr allow these two streams to be redirected in various ways. For example, stderr can be redirected to a file; while stdout can be redirected (or piped) to the stdin of another application, such as sort, less, etc.

-

Hence, normal program output is generally sent to the stdout stream, while error messages, warnings, etc., are generally sent to stderr.

In your example:

fprintf(stderr, "memory exhausted\n");

The program is attempting to inform the user of an error condition. This is only output when the error condition occurs. While one could just use printf() and send the message to stdout stream, the convention is to send such error messages to the stderr stream. Hence, the use of fprintf() where the output stream can be specified.

-

Incidentally, you can use fprintf() to send output to stdout as well. The following statements are equivalent:

printf("Hello world!\n");
fprintf(stdout, "Hello world!\n");
Mahonri Moriancumer
  • 5,803
  • 2
  • 14
  • 28