1. 程式人生 > >C 標準庫 - string.h之strcat使用

C 標準庫 - string.h之strcat使用

www. href 產生 rmi put for turned med main

strcat

  • Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
  • 後附 src 所指向的空終止字節字符串的副本到 dest 所指向的空終止字節字符串的結尾。字符 src[0] 替換 dest 末尾的空終止符。產生的字節字符串是空終止的。
  • 若目標數組對於 src 和 dest 的內容以及空終止符不夠大,則行為未定義。
  • 若字符串重疊,則行為未定義。
  • 若 dest 或 src 不是指向空終止字節字符串的指針,則行為未定義。
char * strcat ( char * destination, const char * source );

Parameters

destination

  • Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
  • 指向要後附到的空終止字節字符串的指針

source

  • C string to be appended. This should not overlap destination.
  • 指向作為復制來源的空終止字節字符串的指針

Return Value

  • destination is returned.
  • 該函數返回一個指向最終的目標字符串 dest 的指針。

Example

//
// Created by zhangrxiang on 2018/2/3.
//

#include <stdio.h>
#include <string.h>

int main() {
  char str[] = "Hello C"
; char str2[50] = "Hello World"; printf("%s\n", str);//Hello C printf("%s\n", str2);//Hello World char *str3 = strcat(str2, str); printf("%s\n", str2);//Hello WorldHello C printf("%s\n", str3);//Hello WorldHello C str3 = "hi everyone"; printf("%s\n", str3);//hi everyone printf("%s\n", str2);//Hello WorldHello C str2[2] = 'L'; str[2] = 'L'; str[0] = 'h'; printf("%s\n", str2);//HeLlo WorldHello C printf("%s\n", str);//heLlo C char *str4 = "hi world"; // str[0] = 'H'; //行為為定義 h printf("%c\n",str4[0]);//h printf("%s\n",str4); char str5[80]; strcpy (str5,"these "); strcat (str5,"strings "); strcat (str5,"are "); strcat (str5,"concatenated."); puts (str5); //these strings are concatenated. return 0; }

文章參考

  • http://www.cplusplus.com/reference/cstring/strcat/
  • http://zh.cppreference.com/w/c/string/byte/strcat
  • https://linux.die.net/man/3/strcat

C 標準庫 - string.h之strcat使用