Best Answer

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 gets function waits until a line of input is available (unless one is already

available), and consumes the whole line including the ENTER/newline at the end.

The characters on the line are stored in the string parameter, except for the

ENTER/newline, which is discarded.

returns NULL on end-of-file, otherwise the parameter s.

The parameter given to gets must be an already allocated array of characters, not an

uninitialised char * pointer; gets will never allocate memory.

{ char a[100]; gets(line); // This is correct

{ char a[100]; char *s; s=a; gets(s); // This is correct

{ char *s; s=new char[100]; gets(s); // This is correct

{ char *s; gets(s); // This is WRONG

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.

User Avatar

Wiki User

โˆ™ 2008-02-22 11:07:04
This answer is:
User Avatar
Study guides

What is a programming language

What does DOS stand for

What is a software that is distributed for free

What is application software

See all cards
33 Reviews

Add your answer:

Earn +20 pts
Q: Between fgets and gets which function is safe to use and why?
Write your answer...
Still have questions?
magnify glass
Related questions

Is puts safe to use?

There is nothing wrong with 'puts' but you should avoid the using of 'gets', and even 'fgets' has a disadvantage: if the input contains a binary zero, you will lose data.

What is gets() function in c programming language?

The gets() functions reads input from stdin and writes it to a user-defined buffer. The function has the following declaration in <stdio.h>: char* gets (char* str); // WARNING! Deprecated since 2011 The function is unsafe because the function won't stop writing to the buffer until a newline or end-of-file is encountered in stdin. If the buffer is too small, the function will overwrite memory beyond the bounds of the buffer. If available, use the gets_s() function instead. This allows you to specify the length of the buffer and thus prevent buffer overruns: char* gets_s (char* s, rsize_t n); Alternatively use the fgets() function passing stdin as the input stream: char* fgets (char* s, int n, FILE* stream); POSIX 2008 also provides the getline() function as a safe alternative to gets(). However, this function resizes the buffer dynamically and must be manually released (free'd) when no longer required.

How safe is a fixed annuity with pacific life?

That's about as safe as it gets.

Who gets locked in the safe in a retrieved reformation?

Agatha got traped in the safe

What is the safe mode function key?


Is tramadol a safe for someone with a kidney function disorder?


What is function of cell wall?

To keep your cells safe.

Why is the Volvo C70 safe?

They are very safe cars the metal they use for the cars is strong and when you crash it gets pushed in but its safe ALL volvos are safe

What is the function of the aqueous humour?

The Aqueous Humor is the fluid that is in between the Cornea and the Lens. It help keeps the front of the eye safe from dust, wind, etc.

What are those ways to keep tools and equipment safe and function?

Ways keep. Tools safe and functional

What do snails do when it gets cold?

they snug in their shell. safe and sound......!

Is iodine safe to take for low thyroid function?


People also asked