当前位置: 首页 > article >正文

【C语法学习】26 - strcat()函数

文章目录

  • 1 函数原型
  • 2 参数
  • 3 返回值
  • 4 使用说明
  • 5 示例
    • 5.1 示例1

1 函数原型

strcat():将src指向的字符串附加在dest指向的字符串末尾,将两个字符串拼接成一个字符串,函数原型如下:

char *strcat(char *dest, const char *src);

2 参数

strcat()函数有两个参数src和dest:

  1. 参数src是指向源字符串的指针,类型为char*型;
  2. 参数dest是指向目的字符串的指针,类型为char*型。

3 返回值

strcat()函数的返回值类型为char*型,返回值为dest。

4 使用说明

  1. strcat()函数将src指向的字符串(不包括末尾的空字符’\0’)附加在dest指向的字符串末尾,即dest指向的字符串末尾的空字符’\0’被src指向的字符串的第一个字符src[0]覆盖;然后在拼接后的新字符串的末尾添加空字符’\0’;
  2. strcat()函数不检查dest指向的内存空间的大小,必须保证dest所指向的内存空间足够大,能够容纳下src指向的字符串和dest指向的字符串,否则会导致溢出。

C语言标准描述如下:

1. Appends a copy of the null-terminated byte string pointed to by src to the end of the null-terminated byte string pointed to by dest. 
2. The character src[0] replaces the null terminator at the end of dest. 
3. The resulting byte string is null-terminated.
4. The behavior is undefined if the destination array is not large enough for the contents of both src and dest and the terminating null character. 
5. The behavior is undefined if the strings overlap. 
6. The behavior is undefined if either dest or src is not a pointer to a null-terminated byte string.

5 示例

5.1 示例1

代码如下所示:

int main()
{
   //
   char dest[27] = "a";
   char src[2] = { 0 };
   //
   int n = 0;

   for (n = 98; n < 108; n++)
   {
      src[0] = n;
      strcat(dest, src);
      puts(dest);
   }

   return 0;
}

代码运行结果如下图所示:
在这里插入图片描述


http://www.kler.cn/news/136096.html

相关文章:

  • 智能门禁刷脸照片格式gif、bmp,png转换,转换base64
  • 力扣labuladong——一刷day44
  • 网络参考模型与标准协议(一)
  • ORA-00600 【3948】,ORA-00600 【3949】
  • 何时使用Elasticsearch而不是MySql?
  • 虚拟化逻辑架构: 创建KVM中的VM与实现VNC远程登录
  • 键盘控制ROS车运动
  • Linux 串口应用编程
  • 阿里国际站(直通车)
  • 【开题报告】基于SpringBoot的膳食营养健康网站的设计与实现
  • WebGoat通关攻略之 SQL Injection (intro)
  • 持续集成交付CICD:Jenkins Sharedlibrary 共享库
  • Java语言基础第四天
  • centos oracle11g开启归档模式
  • 下一代搜索引擎会什么?
  • 力扣贪心——跳跃游戏I和II
  • 【整顿C盘】pycharm、chrome等软件,缓存移动
  • 记一次线上bug排查-----SpringCloud Gateway组件 请求头accept-encoding导致响应结果乱码
  • pytho你-opencv划痕检测
  • 浅谈无线测温产品在菲律宾某工厂配电项目的应用