Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1148212
  • 博文数量: 173
  • 博客积分: 4048
  • 博客等级:
  • 技术积分: 2679
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-12 18:53
文章分类

全部博文(173)

文章存档

2018年(1)

2016年(1)

2013年(1)

2012年(118)

2011年(52)

分类: C/C++

2012-05-03 20:19:37

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.


点击(此处)折叠或打开

  1. /* strncpy example */
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main ()
  5. {
  6.   char str1[]= "To be or not to be";
  7.   char str2[6];
  8.   strncpy (str2,str1,5);
  9.   str2[5]='\0';
  10.   puts (str2);
  11.   return 0;
  12. }




Output:
To be


阅读(1657) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~