Skip to content

Instantly share code, notes, and snippets.

@migf1
migf1 / s_new_from_stdin.c
Last active July 16, 2021 02:03
Read portably a string of arbitrary length from the console in C ( ANSI C89 / ISO C90 ).
#include <stdio.h> /* getchar() */
#include <stdlib.h> /* malloc(), realloc() */
#include <string.h> /* memcpy() */
/* -----------------------------------------------
* Create a new string (NUL-terminated array of chars) by reading the stdin.
* Return the newly created string, or NULL on error.
* Args:
* int beExact: If 0 (false), then the size of the created string will
* be an exact multiple of the internally used alloc-ahead