Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Has Implicit Int been disabled in the new C11 standard? What aboutother previously depreciated constructions eg gets?

Reply
Thread Tools

Has Implicit Int been disabled in the new C11 standard? What aboutother previously depreciated constructions eg gets?

 
 
David Thompson
Guest
Posts: n/a
 
      03-01-2012
On Mon, 20 Feb 2012 13:25:52 +0000, Ben Bacarisse
<> wrote:

> Nick Keighley <> writes:

<snip>
> If you simply must get your gets-containing code to run today, you can use:
>
> #define gets(s) (scanf("%[^\n]%*c", (s)) == 1 ? (s) : NULL)
>
> but you should fix it tomorrow! (Not you personally, of course, but
> "one" is sometimes too formal for Usenet).


Not quite. That gets 'stuck' on an empty line (newline only).

You need to split the call like
( scanf ("%[^\n]", (s)) == 1 ? scanf ("%*c"), (s)
: (scanf ("%*c"), NULL ) )

or you can substitute getchar (or getc(stdin) etc)
( scanf ("%[^\n", (s)) == 1 ? getchar(), (s) : ( getchar(), NULL ) )

And of course it usually evaluates s twice and thus is wrong for an
argument that is not idempotent (often inexactly stated as having side
effects). One could argue that using gets() that way is a bad idea --
but then using gets() at all is a bad idea!

By this point it isn't really fun anymore. If it was necessary (which
as Keith says it's not) I'd just write something straightforward like

char * gets (char * s){
size_t i = 0; int c;
while ( (c = getchar()) != EOF && c != '\n' ) s[i++] = c;
if (c == EOF && i == 0) return NULL;
s[i] = 0; return s;
}

or the obvious pointer equivalent.

Or to be (extra) cautious about the external name, I might write it as
another name like mygets and #define gets to that.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm intrigued that Python has some functional constructions in the language. Casey Hawthorne Python 21 05-11-2009 07:56 AM
The printing has been stopped and this job has been add to the queu? dejola Computer Support 6 12-30-2005 03:26 AM
ASP.NET Forms Authentication - How to Know if User Has Previously Been Authenticated David Krussow ASP .Net 2 01-17-2005 05:19 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57