4 ms·
gets has no right to exist. But sprintf does. It's a workaround for some old embedded C compilers that lack the safe string copy functions (strcpy_s, strlcpy.
by finnw 13y ago
gets has no right to exist. But sprintf does. It's a workaround for some old embedded C compilers that lack the safe string copy functions (strcpy_s, strlcpy.)
char dst[5];
sprintf(dst, "%.*s", (int)sizeof(dst)-1, "Hello!");
Copies a null-terminated string to dst, truncating if necessary to avoid overflow.
Just don't forget the "." or the "-1".
- tedunangst 13y agoThat code is just as easily converted to use snprintf, and then you don't need to worry about the . Or -1.
- epsylon 13y agosnprintf is C99 IIRC. Which means legacy C89 compilers (or worse!) don't have it.
- panzi 13y agoWhich brings it back again to my initial comment: "I don't understand why functions like gets and sprintf weren't removed from the standard and headers a long time ago." And by a long time I mean before 1989 (certainly before 1999).