-3

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;
}
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 1
    Have you read the documentation for how `gets` works? – Mooing Duck Jan 23 '23 at 15:41
  • 1
    Welcome to Stack Overflow. Please read [ask] and try to explain the problem more clearly. When showing code, make sure that someone else can **copy and paste** the code **without adding or changing anything**, and see the **exact** problem **directly**. This means, if you want to explain the code inside the code block, use proper code comments. Also, please read the [formatting help](https://stackoverflow.com/help/formatting) in order to understand how to post code properly. I tried to fix it a little, but it is still not a proper example. – Karl Knechtel Jan 23 '23 at 15:42
  • 1
    Read the Q&A on [Why the `gets()` function is so dangerous it should not be used — ever!](https://stackoverflow.com/q/1694036/15168) Then repent of your evil ways. I'd love to be able to castigate whichever teacher suggested that `gets()` could be used — if it was my course, you'd automatically fail any assignment that used plain `gets()`. – Jonathan Leffler Jan 23 '23 at 16:23
  • 1
    Next time you ask a question, please don't use all UPPERCASE. It's not readable and some people may consider it as rude, because it basically mean you're shouting. – Jabberwocky Jan 23 '23 at 17:10

1 Answers1

1

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.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158