Read characters from stdin into an resizeable array As first steps in C, I'm trying to write a program that reads from stdin into an array allocated on the free store, until an exclamation mark ! is entered. The array should be extended to prevent overflow.

Here is what I've written:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

char* increase_buffer_size(char *buff_ptr, size_t multiplier);

//==================================================================
int main () {
    // initial buffer size
    size_t buffer_size = 32;

    // allocate 32 bytes of heap memory
    char *buffer_pointer = (char*) malloc(buffer_size);

    // input loop variables
    char input_var;
    size_t counter = 0;
    char sentinel = '!';

    // input loop
    printf("Type input\n&gt;&gt;");
    while (input_var = getchar()) {

        // check termination condition
        if (input_var == sentinel) break;

        // check capacity and double it if maximum is reached
        if (counter == buffer_size - 1) buffer_pointer = increase_buffer_size(buffer_pointer, buffer_size *= 2);

        // populate buffer
        buffer_pointer[counter++] = input_var;
    }

    getchar();

    // print input values
    printf("Buffer content: \n");
    for (auto i = 0; i &lt; counter; ++i) printf("%c %c", buffer_pointer[i], ' ');

    getchar();
}

//==================================================================

char* increase_buffer_size(char *buff_ptr, size_t new_size) {

    char* new_ptr = (char*) realloc(buff_ptr, new_size);

    return new_ptr;
} 


As I already mentioned these are my first steps in C, so I was wondering:

Is the above code written in accordance with C conventions?

Is there anything that can be skipped or written better?
Unnecessary function

The only difference between increase_buffer_size() and realloc() is that the former returns a char* and the latter returns a void*. But you can just manually do the cast anyway, which will still be shorter to type.

buffer_pointer = (char*)realloc(buffer_pointer, buffer_size *= 2);


Avoid putting lots of things on on one line

Your reallocation line does three things: checks if you need to reallocate, doubles the buffer size, and reallocates. It'd be much better to just split that up. Don't write the bodies of your if statements on the same line either, unless the body is trivial (e.g. return;). Using {}s is a good habit to get into:

// check capacity and double it if maximum is reached
if (counter == buffer_size - 1) {
    buffer_size *= 2;
    buffer_pointer = (char*)realloc(buffer_pointer, buffer_size);
}


What if realloc fails?

It's possible for it to return a null pointer after all.