strncpy
char * strncpy ( char * destination, const char * source, size_t num );
Copy characters from string
最多只复制source字符串中前num个字符. 如果在这之前遇到'\0',则在destination字符串剩下字节全部补'\0',直到num个字节.
当source字符串的长度大于或等于num时,不会对destination补'\0'。只有小于的时候才补'\0'。
Parameters
destinationPointer
to the destination array where the content is to be copied.
source
C string to be copied.
numMaximum
number of characters to be copied from
source.
Return Value
destination is returned.
- /* strncpy example */
- #include <stdio.h>
- #include <string.h>
- int main ()
- {
- char str1[]= "To be or not to be";
- char str2[6];
- strncpy (str2,str1,5);
- str2[5]='\0';
- puts (str2);
- return 0;
- }
Output:
阅读(1721) | 评论(0) | 转发(0) |