C++ Standard Library C++ STL Library

C++ <cstdio> - stderr



The C++ <cstdio> stderr is a predefined stream. It is associated with the standard error stream, which is the default destination for error messages and other diagnostic warnings. In most systems, it is usually directed by default to the text console (generally, on the screen).

At program startup, the stream is not fully buffered. It is library-dependent whether the stream is line buffered or not buffered by default.

stderr is expanded to expressions of type FILE*. Therefore, it can be used with any function which expects an output stream (FILE*) as one of its parameters, like fputs() or fprintf().

In the <cstdio> header file, it is defined as follows:

#define stderr /* implementation-defined */

Example:

In the example below, stderr is used with fprintf() function where it prints the error message on the screen when tried to open a file which does not exist.

#include <cstdio>
#include <cmath>
#include <cerrno>
#include <cstring>

int main(){
  FILE * pFile;
  pFile = fopen ("wrongfile.txt", "r");
  
  if (pFile == NULL){
    fprintf(stderr, "Value of errno: %d\n", errno);
    fprintf(stderr, "Error opening file: %s\n", strerror(errno));
  } else {
    fclose (pFile);
  }
}

The output of the above code will be:

Value of errno: 2
Error opening file: No such file or directory

❮ C++ <cstdio> Library