You should find that if the input ends after reading at least some characters, but before meeting a new line, that fgets returns non-null (a pointer to the provided buffer), and the supplied buffer will not contain a new line but will be completed with a zero value.
This is exactly what the documentation for fgets says.
eg.
#include <stdio.h> int main(void) { char buffer[200]; char* ret = fgets(buffer, sizeof buffer, stdin); printf("exit code = %p\n", (void*)ret); if (ret != 0) { printf("read code = %s<--END\n", buffer); } return 0; }
output:
$ printf "no newline here->" | ./a.out exit code = 0x7fff6ab096c0 read code = no newline here-><--END
or
$ printf "newline here->\nmore text\n" | ./a.out exit code = 0x7fff6f59e330 read code = newline here-> <--END
without input:
$ printf "" | ./a.out exit code = (nil)
source share