954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

famous gets() function

Hi people,
when starting many of us were warn not to use gets() function. We know it's unsafe and it might easily crah program. On many boards there is FAQ in which there are advices not to use gets(). I wonder why such function exist in standard library and why is it written in the first place.
What is actual use of gets() exept that many of us use it in school for quick and dirty user input?
Why is it there? I assume because of historical reasons, but which reasons that is real question.
Thanks

Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 55
Solved Threads: 6
 

Rationale :7.19.7.7 The gets function
Because gets does not check for buffer overrun, it is generally unsafe to use when its input is not under the programmer’s control. This has caused some to question whether it should appear in the Standard at all. The Committee decided that gets was useful and convenient in those special circumstances when the programmer does have adequate control over the input, and as 35 longstanding existing practice, it needed a standard specification. In general, however, the preferred function is fgets (see §7.19.7.2).

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Okay, thanks Dave

Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 55
Solved Threads: 6
 

Its true that gets function is not as convenient as scanf, but its has its own merits... say, the ease of accepting strings... its not completely redundant.

Hi people, when starting many of us were warn not to use gets() function. We know it's unsafe and it might easily crah program. On many boards there is FAQ in which there are advices not to use gets(). I wonder why such function exist in standard library and why is it written in the first place. What is actual use of gets() exept that many of us use it in school for quick and dirty user input? Why is it there? I assume because of historical reasons, but which reasons that is real question. Thanks
Ginna Yadav
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

>but its has its own merits...
There are no merits to gets.

>say, the ease of accepting strings...
I've argued this point with authors before (Dan Gookin is one such example), and have yet to hear a sufficiently good explanation of how fgets(s, n, stdin) is so hard that it justifies the teaching of unsafe programming habits. Also, it's very common for programmers to prefer the first technique they learn for a task, and if the first thing they learn is gets...

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You