Practice mode C Language

If strncpy(dst, src, n) is called and src has n or more characters, what is true about dst afterwards?

Correct answer D. dst is not null-terminated

Explanation

strncpy copies at most n characters. If src is shorter it pads the rest of dst with null bytes, but if src has n or more characters it copies exactly n and adds no terminator, leaving dst without a null. Passing such a dst to printf or strlen then reads past the array. A safe pattern is to copy n - 1 characters and set dst[n - 1] = '\0' yourself.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.