snprintf vs. strcpy (etc.) in C

后端 未结 7 1776
长发绾君心
长发绾君心 2021-01-02 07:35

For doing string concatenation, I\'ve been doing basic strcpy, strncpy of char* buffers. Then I learned about the snprintf and friends

相关标签:
7条回答
  • 2021-01-02 08:33

    strcpy, strncpy, etc. only copies strings from one memory location to another. But, with snprint, you can do more stuff like formatting the string. Copying integers into buffer, etc.

    It purely depends on your requirement which one to use. If as per your logic, strcpy & strncpy is already working for you, there is no need to jump to snprintf.

    Also, remember to use strncpy for better safety as suggested by others.

    0 讨论(0)
提交回复
热议问题