Updated April 3, 2023
Introduction to Stderr in C
In C programming language, there are different file descriptors which are also known as standard output. There are 3 standards I/O devices that are stdin for standard input, stdout for standard output, stderr for error message output. In this article, we are discussing stderr which used to map on the terminal output. It generates the error message that displays on the output devices and not anywhere else. These three standards I/O devices are the streams that are declared in the header file stdio.h file. Stderr is directly linked by OS to either window terminal or Unix terminal.
Functions of Stderr in C with Examples
Stderr is the standard error message that is used to print the output on the screen or windows terminal. Stderr is used to print the error on the output screen or window terminal. Stderr is also one of the command output as stdout, which is logged anywhere by default. So the output message goes to the console or terminal and if once the terminal is closed the output message is gone forever, where if we want to store such output messages or error messages then we have to redirect it to the files. Stdout and stderr are standards in which stdout is fully buffered whereas stderr is not fully buffered because stdout will completely remove the messages or flushes whenever the program is asked to do explicitly and stderr writes the output message or error message immediately to the console or the window terminal. Let us see how the stderr is used to print.
Example #1
Code:
#include <stdio.h>
int main()
{
fprintf(stderr, "Educba Training");
}
Output:
In the above program, we are printing the message on the output screen. This can be done by using both Stdout and stderr.
In an earlier version of v6 both output and error was sent to the file also which required the manual cleanup by the user as there was no stderr in that version. So stderr is used to do the opposite of the above version where stderr is used to send the message to the file and stdout is used to print the output on the console.
The stderr cannot be redirected to any file instead they are used to print on the same console, whereas stdout can be used for the redirection. The printf() statements used in the programs are used stdout devices by default. So if we use fprintf() statement then these are used to send the output message to the file stdout. If we use stderr in the fprintf() statement then this will not redirect the output message to the file instead it is printed on the same console. The above situation can be explained by the below programs.
Example #2
Code:
#include <stdio.h>
int main()
{
printf("This is message 1\n");
printf("This is message 2\n");
printf("This is message 3\n");
return(0);
}
Ouput:
This above program uses printf() statement where stdout uses these statements to print the output message on the console. Whereas the above program can also be written using frprintf() statements to do a similar job as the above program. This can be written as below:
#include <stdio.h>
int main()
{
fprintf(stdout,"This is message 1\n");
fprintf(stdout,"This is message 2\n");
fprintf(stdout,"This is message 3\n");
return(0);
}
Output:
In the above program, we use fprintf() statement where it redirects the output message and send it to the file using stdout. Now let us see if we use stderr it will not redirect the output message to the file instead it works the same as the above program it will print the output on the console. This can be done using the below program.
Example #3
Code:
#include <stdio.h>
int main()
{
fprintf(stdout,"This is message 1\n");
fprintf(stderr,"This is message 2\n");
fprintf(stdout,"This is message 3\n");
return(0);
}
Output:
In the above program, the second fprintf() statement uses stderr and when we try to redirect the output message only the output message of first and third fprintf() statements are redirected or send to the file whereas the second fprintf() statement which uses stderr cannot be redirected so it prints the output message on the console.
The fprintf(stderr, “”) is the statement is used by both stdout and stderr to print the output message or error message within the double quotes to the window terminal or console.
In C programming language, as standard I/O is buffered therefore the error message is sent to the stderr which appears on the console as out of sequence where another text is sent to the standard output such as stdout. Stderr is used to print the error message to the output console and hence this is used in the program where we want the output to be fetched directly into the other program where it prints the error message directly on the console. Stderr prints the output message on the windows terminal even if the stdout is redirected. There are two different functions that stderr include are fprintf(), fputs(). If we use it for writing the out message to the file then we have to use: FILE *stderr. This stderr is a standard error stream is the default destination for printing the output message as an error message.
Conclusion
This article is based on the standard I/O in C programming language. 3 different standards in C are stdin, stdout, and stderr and in this article, we are discussing stderr. Stderr is the standard error message which prints the output message or error message to the console or windows terminal. The stderr is also unlike stdout where stdout prints the output message to the terminal and also can redirect the output message to the file whereas the stderr also prints the output message or error message immediately to the output terminal or console, but it cannot redirect it to the other file.
Recommended Articles
This is a guide to Stderr in C. Here we also discuss the Introduction and working of stderr in c along with different examples and its code implementation. You may also have a look at the following articles to learn more –