What happens to the extra characters in the fgets() buffer that are over the bounds check?

438 Views Asked by At
#include<stdio.h>

int main() {
    char arr[10];
    printf("Enter your name: \n");
    fgets(arr, sizeof(arr), stdin);
    puts(arr);
    return 0;
}

I have a char arr[10] and use fgets(arr, sizeof(arr), stdin) to receive input. If I input say 20 characters, 9 chars will be written to arr[10] along with the null terminator added. But what happens to the remaining chars in the buffer? Do they get automatically flushed/cleared or are they left in the buffer forever? i.e. Can extra chars that are outside the bounds check cause a problem?

1

There are 1 best solutions below

0
chux - Reinstate Monica On BEST ANSWER

But what happens to the remaining chars in the buffer?

They remain in stdin for the next read function.

Do they get automatically flushed/cleared or are they left in the buffer forever?

Left in stdin until read or program ends.

Can extra chars that are outside the bounds check cause a problem?

Not directly. It depends on how your program copes will failing to read the excess input.