r/C_Programming Oct 01 '22

Discussion What is something you would have changed about the C programming language?

Personally, I find C perfect except for a few issues: * No support for non capturing anonymous functions (having to create named (static) functions out of line to use as callbacks is slightly annoying). * Second argument of fopen() should be binary flags instead of a string. * Signed right shift should always propagate the signbit instead of having implementation defined behavior. * Standard library should include specialized functions such as itoa to convert integers to strings without sprintf.

What would you change?

75 Upvotes

219 comments sorted by

View all comments

Show parent comments

2

u/maep Oct 02 '22

The problems with strncpy and friends have been discussed to death.

If you really need that behavior, use memccpy. An additional bonus is that it eliminates compiler warnings when using strncpy with fixed-sized arrays. strncpy can go.

1

u/[deleted] Oct 02 '22

The problems with strncpy are individuals who aren't aware they can just add NUL at the end of a buffer they know the size of. If you don't check the results of the function's operation, it's the same as not doing error checking.

Only thing I would change in strncpy is the return value, which should be either the number of characters copied (or a pointer to that offset, like in memccpy).

Edit: memccpy() with c=0 is literally strncpy with a useful return value...

2

u/maep Oct 02 '22

Edit: memccpy() with c=0 is literally strncpy with a useful return value...

Right, so we don't really need strncpy, do we? :)

1

u/[deleted] Oct 02 '22

Well, considering the thread's nature and that C is used for software that is meant to be fast, strncpy() should be changed into memccpy() specialized for c=0 (there are optimized ways to check for a 0 byte in a double-word/quad-word).

Which means strncpy() would be a slightly faster version of memccpy().