How to print a line to STDERR and STDOUT in C?

In C, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely?

And similarly, how to print the line to STDERR?

asked Jun 5 by dtivl (2,500 points)

1 Answer

In C, to print to STDOUT, you may do this:

printf("%s\n", your_str);

For example,

$ cat t.c
#include <stdio.h>

void main()
  printf("%s\n", "hello world!");

$ gcc t.c -o t && ./t
hello world!

In C, you can print to the file handle stderr which is the STDERR:

#include <stdio.h>

fprintf(stderr, "fmt", ...);

One example:


#include <stdio.h>

int main()
  fprintf(stderr, "hello world!\n");

Built and run it:

$ gcc stderr.c -o s && ./s
hello world!
answered Jun 5 by dtivl (2,500 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost