'AVR-libc `uart_putchar` implementation

In AVR-libc page 143 the implementation of the function uart_putchar() is typical as the following:

#include <stdio.h>

static int uart_putchar(char c, FILE *stream);

static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);

static int
uart_putchar(char c, FILE *stream)
{
if (c == '\n')
  uart_putchar('\r', stream);
loop_until_bit_is_set(UCSRA, UDRE);
UDR = c;
return 0;
}
int
main(void)
{
  init_uart();
  stdout = &mystdout;
  printf("Hello, world!\n");
  return 0;
}

I know that we can redirect a specific stream to a stdout or stdin using fdevopen().

But is not the variable stream is not used in this function? Why is there such a variable that not used by the function? Is that because the fdevopen() pointers to functions parameters?



Solution 1:[1]

This is just a simple example where there is only one thing to print to, so there is no need to check the stream argument.

If you were going to implement something more complicated where you could print to multiple serial streams using functions like fprintf that take a FILE * argument, then you would use the stream argument to figure out where the caller wanted to send characters to, and then make sure the characters go to the right place. I think most embedded AVR programs do not need that, but avr-libc gives you the ability to do that anyway.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1