wcswidth() — Determine the Display Width of a Wide Character String

Format

#include <wchar.h>
int wcswidth (const wchar_t *wcs, size_t n);

Language Level

XPG4

Threadsafe

Yes

Locale Sensitive

The behavior of this function might be affected by the LC_CTYPE category of the current locale if LOCALETYPE(*LOCALE) is specified on the compilation command. The behavior of this function might also be affected by the LC_UNI_CTYPE category of the current locale if LOCALETYPE(*LOCALEUCS2) or LOCALETYPE(*LOCALEUTF) is specified on the compilation command. This function is not available when LOCALETYPE(*CLD) is specified on the compilation command. For more information, see Understanding CCSIDs and Locales.

Wide Character Function

See Wide Characters for more information.

Description

The wcswidth() function determines the number of printing positions that a graphic representation of n wide characters (or fewer than n wide characters if a null wide character is encountered before n wide characters have been exhausted) in the wide string pointed to by wcs occupies on a display device. The number is independent of its location on the device.

The value of errno may be set to EINVAL (non-printing wide character).

Return Value

The wcswidth() function either returns:
  • 0, if wcs points to a null wide character; or
  • the number of printing positions occupied by the wide string pointed to by wcs; or
  • -1, if any wide character in the wide string pointed to by wcs is not a printing wide character.

Example

#include <stdio.h>
#include <wchar.h>
 
int main(void)
{
   wchar_t *wcs = L"ABC";
 
   printf("wcs has a width of: %d\n", wcswidth(wcs,3));
}
 
/************The output is as follows**************/
/*                                                */
/*                wcs has a width of: 3           */
/*                                                */
/**************************************************/

Related Information