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

1 answer 106 views
106 views asked Aug 8, 2024 by avibootz
2 answers 210 views
210 views asked Feb 9, 2021 by avibootz
1 answer 149 views
1 answer 168 views
1 answer 171 views
1 answer 111 views
111 views asked Jul 12, 2023 by avibootz
...