简体   繁体   English

C 标准是否定义了在 printf 中调用哪些函数?

[英]Does the C standard define which functions are called in printf?

I'm working on an embedded project.我正在做一个嵌入式项目。 I want printf to work with UART port.我希望printf与 UART 端口一起使用。

I did some google and some people suggest that printf calls fputc and I needs to supply function definition of fputc to work with UART port.我做了一些谷歌,有些人建议printf调用fputc ,我需要提供fputc函数定义才能使用 UART 端口。 Other people suggest _write instead.其他人建议改为_write

I am assuming that printf calls fputc , which then calls _write ?我假设printf调用fputc ,然后调用_write

What I want to ask is, does the C standard define anything about this?我想问的是,C 标准是否对此有任何定义? (Is it guaranteed that printf calls fputc ?) (保证printf调用fputc吗?)

C spec doesn't define anything about this aside from printf() functions as if it called fputc() multiple times.除了printf()函数之外,C 规范没有对此进行任何定义,就好像它多次调用fputc()

It is not guaranteed that printf calls fputc .不能保证printf调用fputc


I want printf to work with UART port.我希望 printf 与 UART 端口一起使用。

Consider instead writing your own UART_printf() that calls vsprintf() and then sends the string out to the UART.考虑改为编写自己的UART_printf()调用vsprintf()然后将字符串发送到 UART。

There is no guarantee that printf calls fputc .不能保证printf调用fputc

But you can write your own uprintf function using variadic parameters that internally calls sprintf and then sends the characters to the UART.但是您可以使用内部调用sprintf可变参数编写自己的uprintf函数,然后将字符发送到 UART。

Something like this (untested code):像这样(未经测试的代码):

int uprintf(const char *format, ...)
{
  va_list argList;
  va_start(argList, format);
  char buffer[1000];
  int retval = vsprintf(buffer, format, argList);
  va_end(argList);

  // write chars in buffer to uart here

  return retval;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM