The C code example is a terrible strawman. The task can be solved in C without any memory management, just by using getchar().
Not only that, it's permissible for getchar() to be implemented as a macro, so you might even avoid an actual function call too.