Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do any functions in the C standard library implicitly use `stderr`?

The C spec mandates all C programs have 3 streams open and available to them: stdout, stdin, stderr.

The users can use these streams as they see fit, e.g.:

fprintf(stdout, "lol");
fputs("oops", stderr);
fgets(buffer, 20, stdin);

Some functions in the C standard library implicitly use these, e.g.:

printf("lol");           /* implicitly uses stdout */
puts("rofl");            /* implicitly uses stdout */
int c = getchar(buffer); /* implicitly uses stdin  */
  1. Do any functions in the C standard library implicitly use stderr?
  2. Do any functions in common implementations of the C standard library (eg. GNU's glibc on Linux) implicitly use stderr?
like image 714
Pod Avatar asked Jan 07 '21 15:01

Pod


People also ask

What is library function with example?

library functions are those functions which reduce our time to write a lengthy code. for example: 1. you want to find the square root of a number...instead of writing the code you can use the function sqrt(); which use the file math.h.


1 Answers

The assert macro and the perror function write to the standard error stream. So does the abort_handler_s function (in optional Annex K).

exit closes files and flushes streams, so it implicitly acts on the standard error stream. _Exit and abort may do so; the C standard permits but does not require it. fflush(NULL) flushes all streams.

C 2018 7.21.3 3 describes some interaction between input and output streams: Requesting input on an unbuffered stream or on a line buffered stream and that requires characters from the host environment, then line buffered streams are flushed. This may affect the standard error stream.

Per C 2018 Annex J, which is optional, the C implementation may write some floating-point diagnostics to the standard error stream as part of normal program termination.

Searching for “standard error stream” and “stderr” in the C 2018 standard does not reveal any other implicit uses of the standard error stream in the standard library.

like image 178
Eric Postpischil Avatar answered Oct 11 '22 22:10

Eric Postpischil