Has Implicit Int been disabled in the new C11 standard? What aboutother previously depreciated const

D

David Thompson

Nick Keighley <[email protected]> writes:
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 = 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,082
Messages
2,570,589
Members
47,212
Latest member
JaydenBail

Latest Threads

Top