7

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?
Pod
  • 3,938
  • 2
  • 37
  • 45
  • 4
    [`perror()`](https://man7.org/linux/man-pages/man3/perror.3.html) prints message to standard error. – MikeCAT Jan 07 '21 at 15:37
  • 2
    `fflush(NULL)` flushes _all_ streams. – KamilCuk Jan 07 '21 at 16:26
  • @MikeCAT when writing the question the only one I knew of was `perror()`, but I wanted a "comprehensive" list and I couldn't find one online. I didn't want to put perror in the question as it feels like I'd be contaminating the answers :) – Pod Jan 07 '21 at 22:54

1 Answers1

10

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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 1
    `malloc` and `free` can write to `stderr` if they discover heap corruption. Any call to a lazy-resolved function from a shared library may result in failure due to missing symbol, which will cause the loader to write to `stderr`. – Employed Russian Jan 08 '21 at 17:20
  • @EmployedRussian: If there is heap corruption, then something else in the program has caused undefined behavior by overrunning allocated space or some such other error. So this is not particularly permission by the C standard for `malloc` and `free` to use the standard error stream. One must allow for the fact that any undefined behavior might somehow lead to any number of things being affected. Similarly, run-time linking and loading and errors in it are outside of the C standard. (I am also not sure they write to C’s standard error stream, rather than to, say, Unix file descriptor 2.) – Eric Postpischil Jan 08 '21 at 17:28
  • 1
    One of the questions was: "do any GLIBC functions _implicitly_ use `stderr`?". I think the answer "under certain conditions, calling _any_ GLIBC function may do so" is more complete than "the standard allows X,Y, and Z". – Employed Russian Jan 09 '21 at 00:12