Why does it give six 'g' s when the char array is only for 5. Also why does the compiler say 'gets' is dangerous ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does it give six 'g' s when the char array is only for 5. Also why does the compiler say 'gets' is dangerous ?

I created a char array as char v[5], but I could print 6 and more letters. Also the compiler throwed a warning saying it is dangerous to use 'gets' https://code.sololearn.com/cTmzpf3LeIk0/?ref=app

18th Apr 2020, 2:32 PM
villa then
villa then - avatar
3 Answers
+ 2
gets() is dangerous because it provides a way of buffer overflow attack or an error. Synatx of gets() : char * gets(char * buff) The function gets() work in a way that it reads data form standard input stream until a new line is found. Consider buffer "buff " has a length 10. If you read a stream of inputs that has a length 15 and the newline is present in the 16 th position. The gets() will read and store into "buff" until a newline encountered. Because newline is at 16th position, gets() will read 15 characters even the buffer has size of 10. It creates a big error in your program. There are lot of viruses uses these method to get into the software.
18th Apr 2020, 2:38 PM
sarada lakshmi
sarada lakshmi - avatar
+ 1
I not sure for correct function but why you use get, as i remember that in c there were function scanf, get fget use when you work with binary data.may be i wrong. But i know that fro imput from keyboard used scanf ,or sscanf or something like this
18th Apr 2020, 3:17 PM
george
george - avatar
0
Thanks for your explanations sarada lakshmi and george
19th Apr 2020, 7:22 PM
villa then
villa then - avatar