C Programming/wchar.h/wcscat

From Wikibooks, open books for an open world
Jump to navigation Jump to search

In C, the function wcscat() is included in header file wchar.h. This function is quite similar to strcat.
This function is used to concatenate two wide character strings.

Syntax[edit | edit source]

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

Description[edit | edit source]

The job done by the functions strcat and Wcscat is same as mentioned i.e concatenating two strings.. The difference is that the function strcat takes (normal) character strings and wcscat takes wide character strings as arguments.
The function wcscat ,copy the wide character string say b including '\0'(at the end of string b) , at the end of wide character string say a. The '\0' character of the string a is replaced by the first character of the string 'b'. I.e function append the sring 'b' to the end of string 'a'. If wide character string 'a' is "hello" and wide character string 'b' is "world" . After calling the function wcscat(a , b) , string 'a' becomes "helloworld" and string 'b' remains same. The wide string 'a' must have memory of at least (strlen(a) + strlen(b) +1) bytes so that it can store wcs(Wide character string) 'a' , wcs 'b' and '\0'.

Sample Program[edit | edit source]

#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;

}

Output of program will be ' Hello World '.
Wide character string 'source'(World) is appended at the end of wide character string 'destination'(Hello).

Return Value[edit | edit source]

The function wcscat() returns pointer to wide character string 'a' i.e pointer to the string which is concatenated.