Latest web development tutorials

C 庫函數– strncat()

C 標準庫 - <string.h> C標準庫- <string.h>

描述

C庫函數char *strncat(char *dest, const char *src, size_t n)把src所指向的字符串追加到dest所指向的字符串的結尾,直到n字符長度為止。

聲明

下面是strncat() 函數的聲明。

char *strncat(char *dest, const char *src, size_t n)

參數

  • dest --指向目標數組,該數組包含了一個C字符串,且足夠容納追加後的字符串,包括額外的空字符。
  • src --要追加的字符串。
  • n --要追加的最大字符數。

返回值

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

實例

下面的實例演示了strncat() 函數的用法。

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

int main ()
{
   char src[50], dest[50];

   strcpy(src,  "This is source");
   strcpy(dest, "This is destination");

   strncat(dest, src, 15);

   printf("最终的目标字符串: |%s|", dest);
   
   return(0);
}

讓我們編譯並運行上面的程序,這將產生以下結果:

最终的目标字符串: |This is destinationThis is source|

C 標準庫 - <string.h> C標準庫- <string.h>