206

In C, printing to standard output (stdout) is easy, with printf from stdio.h.

However, how can I print to standard error (stderr)? We can use fprintf to achieve it apparently, but its syntax seems strange. Maybe we can use printf to print to standard error?

4
  • 11
    What's so "strange" about it's syntax? It's print where, how and what.
    – Eugene Sh.
    Aug 17, 2016 at 16:42
  • 7
    I am focusing on it. The only problem which is arising from the question, that you find the solution "strange". Otherwise there is no question. Use fprintf.
    – Eugene Sh.
    Aug 17, 2016 at 16:46
  • 1
    @Eugene. I agree with you. I thought it was strange as I did not realize stderr is a FILE :)
    – wad
    Aug 17, 2016 at 16:49
  • @Schilive Thanks for letting me know - I deleted mine now as well. Apr 8 at 15:04

7 Answers 7

294

The syntax is almost the same as printf. With printf you give the string format and its contents ie:

printf("my %s has %d chars\n", "string format", 30);

With fprintf it is the same, except now you are also specifying the place to print to:

FILE *myFile;
...
fprintf(myFile, "my %s has %d chars\n", "string format", 30);

Or in your case:

fprintf(stderr, "my %s has %d chars\n", "string format", 30);
0
58

Some examples of formatted output to stdout and stderr:

printf("%s", "Hello world\n");              // "Hello world" on stdout (using printf)
fprintf(stdout, "%s", "Hello world\n");     // "Hello world" on stdout (using fprintf)
fprintf(stderr, "%s", "Stack overflow!\n"); // Error message on stderr (using fprintf)
21
#include<stdio.h>

int main ( ) {
    printf( "hello " );
    fprintf( stderr, "HELP!" );
    printf( " world\n" );
    return 0;
}

$ ./a.exe
HELP!hello  world
$ ./a.exe 2> tmp1
hello  world
$ ./a.exe 1> tmp1
HELP!$
  1. stderr is usually unbuffered and stdout usually is. This can lead to odd looking output like this, which suggests code is executing in the wrong order. It isn't, it's just that the stdout buffer has yet to be flushed. Redirected or piped streams would of course not see this interleave as they would normally only see the output of stdout only or stderr only.

  2. Although initially both stdout and stderr come to the console, both are separate and can be individually redirected.

8

If you don't want to modify current code and just for debug usage.

Add this macro:

#define printf(args...) fprintf(stderr, ##args)
// Under GCC

#define printf(args...) fprintf(stderr, __VA_ARGS__)
// Under MSVC

Change stderr to stdout if you want to roll back.

It's helpful for debug, but it's not a good practice.

2
2

You may know sprintf. It's basically the same thing with fprintf. The first argument is the destination (the file in the case of fprintf i.e. stderr), the second argument is the format string, and the rest are the arguments as usual.

I also recommend this printf (and family) reference.

1
  • 3
    This is unrelated to the OP and is worded confusingly. sprintf will "print" to a string and cannot print to stderr. Sep 22, 2020 at 0:27
-1

For some reason, no one has mentioned eprintf() yet, which is the same thing as fprintf(stderr, ...).

eprintf("ERROR: You have caused an error!!1!");

Quick tip: If you are using stderr for debugging, you can use the __LINE__ macro

#define printfError(str, ...) eprintf("ERROR at line " #__LINE__ ": " str, __VA_ARGS__)

printfError("Division by zero"); // "ERROR at line 2: Division by zero
2
  • 4
    Nobody mentioned eprintf because that's not a standard C library function. Sep 22, 2022 at 17:56
  • 2
    @AdrianMole Oh.
    – user13721385
    Sep 23, 2022 at 18:50
-7

To print your context, you can write code like this:

FILE *fp;
char *of;
sprintf(of, "%s%s", text1, text2);
fp = fopen(of, 'w');
fprintf(fp, "your print line");
2
  • This doesn't add anything to the previous answers and doesn't really answer the question. Apr 30, 2019 at 8:43
  • 4
    That sprintf() statement causes memory corruption! ...I highly advise you read up on how pointers, arrays, and strings work.
    – BlueChip
    Jun 13, 2020 at 17:15

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.