namcao00
namcao00

Reputation: 292

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.

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. Other people suggest _write instead.

I am assuming that printf calls fputc, which then calls _write?

What I want to ask is, does the C standard define anything about this? (Is it guaranteed that printf calls fputc?)

Upvotes: 1

Views: 173

Answers (2)

Jabberwocky
Jabberwocky

Reputation: 50775

There is no guarantee that printf calls fputc.

But you can write your own uprintf function using variadic parameters that internally calls sprintf and then sends the characters to the 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;
}

Upvotes: 1

chux
chux

Reputation: 153458

C spec doesn't define anything about this aside from printf() functions as if it called fputc() multiple times.

It is not guaranteed that printf calls fputc.


I want printf to work with UART port.

Consider instead writing your own UART_printf() that calls vsprintf() and then sends the string out to the UART.

Upvotes: 3

Related Questions