Why is it usually a bad idea to use gets()? Suggest a workaround.

Answers were Sorted based on User's Feedback



Why is it usually a bad idea to use gets()? Suggest a workaround...

Answer / hrpynux@gmail.com

The function gets() reads characters from the stdin and stores them at the provided input buffer. However, gets() will keep reading until it encounters a newline character. Unless the buffer is large enough, or the length of the line being read is known ahead of time, gets() can potentially overflow the input buffer and start overwriting memory it is not supposed to, wreaking havoc or opening security vulnerabilities.

Is This Answer Correct ?    1 Yes 0 No

Why is it usually a bad idea to use gets()? Suggest a workaround...

Answer / Alok Gautam

Using `gets()` is dangerous because it doesn't check the buffer size, which can lead to buffer overflows. To safely read input into an array, you can use fgets() function instead, which allows you to specify the maximum number of characters to be read.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

Write a program that takes a 5 digit number and calculates 2 power that number and prints it.

1 Answers  


How do I use strcmp?

1 Answers  


What is meant by keywords in c?

1 Answers  


How can I determine whether a machines byte order is big-endian or little-endian?

1 Answers  


Explain how can I remove the trailing spaces from a string?

1 Answers  


#define d 10+10 main() { printf("%d",d*d); }

6 Answers  


What is the difference between the = symbol and == symbol?

1 Answers  


What is macro?

5 Answers   IBM,


How would you find a cycle in a linked list?

3 Answers   NSN,


List some of the static data structures in C?

1 Answers  


Function which gives a pointer to a binary trees const an integer value at each code, return function of all the nodes in binary tree.?

1 Answers   Infosys,


can any one provide me the notes of data structure for ignou cs-62 paper

1 Answers   Ignou,


Categories