How I can print to stderr in C?

后端 未结 6 1383
既然无缘
既然无缘 2020-12-23 00:11

In C, Printing to stdout is easy, with printf from stdio.h.

However, how can print to stderr? We can use fprintf to achieve it

6条回答
  •  半阙折子戏
    2020-12-23 00:53

    If you don't want to modify current codes 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.

提交回复
热议问题