How to use a input to fixed size buffer without overflow in C

1 Answer

0 votes
#include <stdio.h>

int main(void)
{
    char str[16];

    printf("Enter a string: ");

    fgets(str, sizeof(str), stdin); // takes only 15 characters 

    puts(str);

    return 0;
}





/*
run

Enter a string: abcdefghijklmnopqrstuvwxyz
abcdefghijklmno

*/

 



answered Jun 1, 2023 by avibootz

Related questions

...