Science, asked by pavithkalyan2088, 1 year ago

Out of fgets() and gets() which function is safe to use and why?

Answers

Answered by Sushank2003
3
he function fgets() function is safer to use. It checks the bounds, i.e., the size of the buffer and does not cause overflow on the stack to occur. gets() does not check the bounds. 
gets is an insecure function, its careless use can lead to errors. If you want to use gets, consider using fgets instead, supplying stdin as the file reference parameter.

The parameter given to gets must be an already allocated array of characters, not an
uninitialised char * pointer; gets will never allocate memory.

The array given to gets must be big enough to hold any line that could conceivably be
input. C++ and C are incapable of telling how long an array is. If it is not long enough
for the data that is read, other data (and perhaps program code) will be overwritten.
Thus gets is not a safe function for use in critical applications.

gets does NOT check the size of the buffer and overflow on the stack can occour. Because of this you should use fgets in preferance

Similar questions