Any other way to print on the screen instead of printf() and fprintf() in C?

寵の児 提交于 2021-02-04 21:40:56

问题


I'm programming with gcc in CentOS 5.5 and the most of time I use printf() and fprintf() to print on terminal, but in some websites I've seen that some people use write(). I want to know if there's other ways to print on terminal.

Thanks.


回答1:


There are some major differences between these functions.

  1. The standard library provides some functions to output to stdout: printf, puts, putchar etc.
  2. And some functions to output to a stream, you can specify the stream to stdout: fprintf, fputs, fwrite, etc.
  3. But write is different, it's a low-level I/O function. The standard library doesn't provide any low-level I/O functions. For example, POSIX provides write that can output to a file descriptor.

Google for how to use each one of them.




回答2:


You could use puts() or putchar().

puts("Hello, world!\n");

There's a also fputs(), putc(), and fputc() if you want/need to specify a FILE* to write to.




回答3:


All the output functions in the C standard I/O library could be used:

  • fprintf()
  • fputc()
  • fputs()
  • fputwc()
  • fputws()
  • fwprintf()
  • fwrite()
  • printf()
  • putc()
  • putchar()
  • puts()
  • putwc()
  • putwchar()
  • vfprintf()
  • vfwprintf()
  • vprintf()
  • vwprintf()
  • wprintf()

Most of the other write-like functions in POSIX could be used (but a few are reserved for sockets and those probably can't be used).

  • aio_write()
  • dprintf()
  • putc_unlocked()
  • putchar_unlocked()
  • pwrite()
  • vdprintf()
  • write()
  • writev()

There are many functions in the curses library that could be used.



来源:https://stackoverflow.com/questions/19919764/any-other-way-to-print-on-the-screen-instead-of-printf-and-fprintf-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!