Why string[10] containing more 10 character in c?

76 Views Asked by At

I'm taking array of character size of 10 , but in return it gives me out-range array(10+) string, YOU CAN REFER TO MY CODE

#include<stdio.h>
int main(){
    char name[10]; `array of 10 Character`
    gets(name); `INPUT:  THIS IS BEAUTIFUL WORLD!` 

    printf("Given string %s", name); `it should print only 10 string in c` 
    ` OUTPUT : Given string THIS IS BEAUTIFUL WORLD! `
    return 0;
}
1

There are 1 best solutions below

0
Mooing Duck On

https://en.cppreference.com/w/c/io/gets

char *gets( char *str );

The gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on stdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. fgets() and gets_s() are the recommended replacements.