using my own getchar() and got an error

Hello all,
I am writing my own getchar & putchar routines for
use with printf from the standard C library (stdio.h).

It seems like the compiler does not like me naming a function
the same as whats in the C Std library.

I am getting an error "error: expected identifier before '(' token
void putchar(char character)" , if I change the function
name to putchar1 then it compiles (no error msg).

On other tools that I have used, if I write a function
and name it the same as a library function, that will be
used instead of the one in the library.

Anyone have some experience with this?


Depending upon what you’re trying to accomplish on the whole, you may need to be aware that this is C++ and you may need to place your routines in your own namespace.

It's also possible that getchar is actually a macro.

In any case - this really, really sounds like an XY problem. If you want to printf to something other than stdout, you use fprintf(). That's what it's for.

Yes, I think I need to tell the compiler what std out & std in is. Normally on a PC it's the console but not on a microcontroller. Found an example on the avr-libc website that sets up std out and uses user getchar , putchar functions in order to use printf from the library.
What do you mean by an x,y problem?

What do you mean by an x,y problem?

In this case, X is “I want to use standard C++ file operators to print to Serial” and Y is “how can I override the library definition of putchar and getchar”.

(Hmm - maybe I was wrong about this being an XY problem. I suppose it depends on what X is.)

But basically - a microcontroller is not a unix machine, and some of the idioms of C are not all that applicable. stdout is “where you can put stuff so that the shell can pipe and redirect it”. In that sense, There simply is no stdout in the context of a microcontroller.

I have attached some of the text from stdio.h in the arduino library folder. The answer seems to be:

  1. create a Stream object
  2. use fdevopen to associate that object with your user-supplied code
  3. use fprintf (etc) to printf to the stream.

Outline of the chosen API

The standard streams \c stdin, \c stdout, and \c stderr are
provided, but contrary to the C standard, since avr-libc has no
knowledge about applicable devices, these streams are not already
pre-initialized at application startup. Also, since there is no
notion of “file” whatsoever to avr-libc, there is no function
\c fopen() that could be used to associate a stream to some device.
(See \ref stdio_note1 “note 1”.) Instead, the function \c fdevopen()
is provided to associate a stream to a device, where the device
needs to provide a function to send a character, to receive a
character, or both. There is no differentiation between “text” and
“binary” streams inside avr-libc. Character \c \n is sent
literally down to the device’s \c put() function. If the device
requires a carriage return (\c \r) character to be sent before
the linefeed, its \c put() routine must implement this (see
\ref stdio_note2 “note 2”).

As an alternative method to fdevopen(), the macro
fdev_setup_stream() might be used to setup a user-supplied FILE