複製鏈接
請複製以下鏈接發送給好友

strncat

鎖定
strncat,原型extern char *strncat,主要功能是在字符串的結尾追加n個字符。
外文名
strncat
原    型
extern char *strncat
接原型
(char *dest,char *src,int n);
庫    名
#include string.h>
功    能
把src所指字符串的前n個字符添加
自定義
到dest結尾處(覆蓋dest結尾處

strncat函數

原型
char * strncat(char *dest, const char *src, size_t n);
 【參數説明】:dest指向目標字符串,src為指向源字符串。
庫名
#include <string.h>
功能
把src所指字符串的前n個字符添加到dest所指字符串的結尾處,並覆蓋dest所指字符串結尾的'\0',從而實現字符串的連接。
説明
src和dest所指內存區域不可以重疊,並且dest必須有足夠的空間來容納src的字符串。
返回指向dest的指針。

strncat程序示例

#include<stdio.h>

#include<string.h>


int main(void)

{

        int n;
    char dest[20] = "Hello";

    char src[10] = "World";

    strncat(dest, src, 5);//n=5為src前n個字符

    printf("%s\n", dest);

    return  0;

}
【運行結果】:HelloWorld

strncat函數説明

strncat()將會從字符串src的開頭拷貝n 個字符到dest字符串尾部,dest要有足夠的空間來容納要拷貝的字符串。如果n大於字符串src的長度,那麼僅將src指向的字符串內容追加到dest的尾部。
strncat()會將dest字符串最後的'\0'覆蓋掉。

strncat相關函數