wcwidth() — Determine the display width of a wide character

Standards

Standards / Extensions C or C++ Dependencies
XPG4
XPG4.2
Single UNIX Specification, Version 3
both  

Format

Non-XPG4:
#include <wchar.h>

int wcwidth(const wint_t wc);
XPG4:
#define _XOPEN_SOURCE
#include <wchar.h>

int wcwidth(const wchar_t wc);

General description

Determines the number of printing positions that a graphic representation of wc occupies on a display device. Each of the printing wide characters occupies its own number of printing positions on a display device. The number is independent of its location on the device.

Special behavior for XPG4: If you define any feature test macro specifying XPG4 behavior before the statement in your program source file to include the wchar header, then the compiler assumes that your program is using the XPG4 variety of the wcwidth() function. Please see Table 1 for a list of XPG4 and other feature test macros.

The prototype for the XPG4 variety of the wcwidth() function is:
int wcwidth(const wchar_t wc);

The difference between this variety and the C/370, non-XPG4 variety of the wcwidth() function is that its parameter, wc, is a wchar_t rather than a wint_t type.

Returned value

If successful, wcwidth() returns the number of printing positions occupied by wc.

If wc is a NULL or non-spacing wide character, wcwidth() returns 0.

If wc is not a printing wide character, wcwidth() returns -1.

The behavior of wcwidth() is affected by the LC_CTYPE category.
Notes:
  1. Under z/OS® XL C/C++ applications, the width returned will be zero for a NULL or non-spacing character, 1 for a single-byte character, and 2 for a double-byte character.
  2. A non-spacing character is a character belonging to the charclass named _zlc (zero length character class) in the LC_CTYPE category.

Example

CELEBW31
/* CELEBW31 */                                   
#include <stdio.h>                                                              
#include <wchar.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   wint_t wc = L'A';                                                            
                                                                                
   printf("wc has a width of: %d\n", wcwidth(wc));                              
}                                                                               
Output:
wc has a width of: 1

Related information