Is there any function to get an unlimited input string from standard input

The C standard doesn't define such a function, but POSIX does.

The getline function, documented here (or by typing man getline if you're on a UNIX-like system) does what you're asking for.

It may not be available on non-POSIX systems (such as MS Windows).

A small program that demonstrates its usage:

#include <stdio.h>
#include <stdlib.h>
int main(void) {
    char *line = NULL;
    size_t n = 0;
    ssize_t result = getline(&line, &n, stdin);
    printf("result = %zd, n = %zu, line = \"%s\"\n", result, n, line);
    free(line);
}

As with fgets, the '\n' newline character is left in the array.


One way is to run a loop with getchar and keep placing the characters into an array. Once the array is full, reallocate it to a larger size.