What would you like to do?

Between fgets and gets which function is safe to use and why?

already exists.

Would you like to merge this question into it?

already exists as an alternate of this question.

Would you like to make it the primary and merge this question into it?

exists and is an alternate of .

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.
4 people found this useful
Thanks for the feedback!

Difference between fgets and gets?

Both fgets() and gets() read a line terminated with a newline terminator, while gets() reads from stdin and fgets() reads from a specified file. Also, gets() strips the traili

What are the differences between strdup and strcpy functions used in C?

  Answer     strcpy - copy a string to a location YOU created (you create the location, make sure that the source string will have enough room there and afterwar

What is the difference between actual and formal arguments used in functions in C language?

The arguments passed to the functions while the function is called is known as the actual arguments, whereas the arguments declared in the function header is called as formal

What is the difference between safe and save?

  "Safe" is being careful about something. "Save" is helping something or someone in need. Sepcific Definition "Safe"-Free from harm, injury, or risk; untouched or u

Both fats and carbohydrates are used for energy. what is the difference between the functions of these two compounds?

  Carbohydrates are the sugars used to create energy as ATP. ATP is the primary source of energy in most organisms. If you pick up a ball and throw it you will be burning

Is getting your ears pierced at Claire's safe?

No it's not, see claire's does something called silver piercing, and silver piercing isn't as good for your ears, and then after you get your ears pierced there they get very

What exactly is the difference in function between the US Army and US Marine Corps?

There is a world of difference as far as training and how things are run. The Marines are more specialized for combat. All Marines regardless of occupation receive infantry tr

Is it safe to exercise when getting over a cold?

  You shouldn't excercise vigerously when under the symptoms of any viral infection, the reason being many viruses (cold viruses, echo viruses, enteroviruses) infect the h
In Health

When are you safe from getting a dry socket?

after the blood clot that has formed in the hole is gone and tissue has been replaced. Any time you dislodge this clot, you get a dry socket, which is exposed bone to air. Don