How do I fill an 80-character buffer with characters as they are being entered or until the carriage return key is pressed, or the buffer is full, whichever occurs first.
#include ... char buf[80]; int i; for (i = 0; i < sizeof(buf) - 1; i++) { int c = getchar(); if ( (c == '\n') || (c == EOF) ) { buf[i] = '\0'; break; } buf[i] = c; } buf[sizeof(buf] - 1] = '\0';