-2

I'm currently learning strings in C, and a question arose: why doesn't the C developers make strncpy null terminated automatically?

For example in the implementation of strncpy, why didn't people who make C language add this line:

// dest denotes the destination string

dest[n - 1] = '\0';

in order to make strncpy safer?

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
Falanke
  • 69
  • 5
  • it is a choice, you can also ask for why _puts_ adds a final newline but not _fputs_ etc – bruno Feb 15 '19 at 16:28
  • Not every design decision has an explanation which will satisfy everyone. There is `strlcpy` FWIW, but it is not standard... – Eugene Sh. Feb 15 '19 at 16:29
  • 3
    Because `strncpy` gets used in situations where you may *not* want to 0-terminate the result, such as overwriting only part of a string. – John Bode Feb 15 '19 at 16:30
  • How would you be able to identify that you weren't able to copy the entire length of the source buffer that you intended to? If the last character in your destination buffer is non-null, then you know that the copy was incomplete. – Christian Gibbons Feb 15 '19 at 16:35
  • The folklore reason I know -- but I can't guarantee its veracity -- has been told [here](https://stackoverflow.com/a/1454071/136208). In other words, it's a special purpose function dating back from when C and Unix where not clearly separated which shouldn't have been dragged in C at the separation time. – AProgrammer Feb 15 '19 at 17:01
  • 1
    Possible duplicate of [Why does strncpy not null terminate?](https://stackoverflow.com/questions/1453876/why-does-strncpy-not-null-terminate) – Dipstick Feb 15 '19 at 17:28

3 Answers3

3

strncpy was never intended to be a "safe alternative" to strcpy, though that is the impression a lot of new C programmers get since it does take a "size" parameter. The third argument is for the number of characters to be read from the source buffer, not the maximum capacity of the destination buffer.

There are few use cases today for which strncpy is the best choice for copying data from one buffer to another. If you do require a safe alternative to strcpy, you could use snprintf like so:

snprintf(dest, len_dest, "%s", src);
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
1

Its done this way, because you may want to overwrite just the first words of a sentence.

yar
  • 1,855
  • 13
  • 26
0

It is null terminated if you don't reach the count. See quote below (https://en.cppreference.com/w/c/string/byte/strncpy)

Copies at most count characters of the character array pointed to by src (including the terminating null character, but not any of the characters that follow the null character) to character array pointed to by dest.

It is only unterminated if you reach count before terminating null character. You can't add terminating null character at the capacity because that would be outside of array bounds.

You may want to look into strlcpy which will do what you want but is not part of the standard library.

Kon
  • 4,023
  • 4
  • 24
  • 38