跳轉到內容

C 程式設計/wchar.h/wcscat

來自華夏公益教科書,自由的教科書

在 C 語言中,函式 **wcscat()** 包含在標頭檔案 wchar.h 中。此函式與 strcat 非常相似。
此函式用於連線兩個寬字元字串。

#include <wchar.h>
wchar_t *wcscat(wchar_t *a, const wchar_t *b);

strcat 和 Wcscat 函式的作用相同,即連線兩個字串。區別在於 strcat 函式接受(普通)**字元字串**,而 wcscat 函式接受**寬字元字串**作為引數。
wcscat 函式將寬字元字串,例如 **b**(包括字串 b 末尾的 '\0')複製到寬字元字串 **a** 的末尾。字串 a 的 '\0' 字元被字串 'b' 的第一個字元替換。即函式將字串 'b' 附加到字串 'a' 的末尾。如果寬字元字串 'a' 是 "hello",而寬字元字串 'b' 是 "world"。呼叫 wcscat(a , b) 函式後,字串 'a' 變成 "helloworld",字串 'b' 保持不變。寬字串 'a' 必須至少有 (strlen(a) + strlen(b) +1) 個位元組的記憶體,以便它可以儲存 wcs(寬字元字串)'a',wcs 'b' 和 '\0'。

示例程式

[編輯 | 編輯原始碼]
#include <stdio.h>
#include <wchar.h>
#define SIZE 32

int main() {

      wchar_t destination[SIZE] = L"Hello"; /*SIZE should be sufficient enough to store both strings and NULL termination '\0' */
      wchar_t * source  = L" World";
      wchar_t * ptr;

      ptr = wcscat( destination, source );
      printf(" %ls\n", ptr ); /* prints "hello World"*/
     /*OR printf("%ls\n" , destination);*/

      return 0;

}

程式的輸出將是 ' Hello World '。
寬字元字串 'source'(World) 附加到寬字元字串 'destination'(Hello) 的末尾。

返回值

[編輯 | 編輯原始碼]

wcscat() 函式返回指向寬字元字串 'a' 的指標,即指向連線的字串的指標。

華夏公益教科書