Why are there no types of "unsigned wchar_t" and "signed wchar_t"?

The char signature is not standardized. Therefore, there are types signed charand unsigned char. Therefore, functions that work with a single character should use an argument type, which can contain both signed char and unsigned char (this type was chosen as int), because if the argument type was char, we would receive type conversion warnings from the compiler (if -Wconversion is used) in the code:

char c = 'ΓΏ';
if (islower((unsigned char) c)) ...

warning: conversion to β€˜char’ from β€˜unsigned char’ may change the sign of the result

(here we look at what happens if the argument type islower () was char)

And what does the job without explicit type casting is automatic progress from charto int.

In addition, the ISO C90 standard, where it wchar_twas introduced, says nothing specific presentation wchar_t.

Some quotes from the glibc link:

it would be legal to define wchar_thowchar

if wchar_tdefined as char, the type wint_tmust be defined as intdue to the advancement of the parameter.

So, wchar_tit is quite possible to define how char, which means that similar rules for wide types of characters that should be applied, that is, there may be implementations where it wchar_tis positive, and there may be implementations where it is wchar_tnegative. It follows that there must be types unsigned wchar_tand signed wchar_t(for the same reason as types unsigned charand signed char).

, >= 0 ( wchar_t). - , ? , wchar_t 16- (), 15 ? , , wchar_t ? . .

, , , wchar_t wint_t. ?

:

#include <locale.h>
#include <ctype.h>
int main (void)
{
  setlocale(LC_CTYPE, "fr_FR.ISO-8859-1");

  /* 11111111 */
  char c = 'ΓΏ';

  if (islower(c)) return 0;
  return 1;
}

, , '( char)'. , char signed char, , , int, , unsigned char.

, ?

#include <locale.h>
#include <wchar.h>
#include <wctype.h>
int main(void)
{
  setlocale(LC_CTYPE, "");
  wchar_t wc = L'ΓΏ';

  if (iswlower(wc)) return 0;
  return 1;
}

iswlower((unsigned wchar_t)wc) , unsigned wchar_t.

unsigned wchar_t signed wchar_t?

UPDATE

, , unsigned int int ? ( wint_t wchar_t glibc)

#include <locale.h>
#include <wchar.h>
int main(void)
{
  setlocale(LC_CTYPE, "en_US.UTF-8");
  unsigned int wc;
  wc = getwchar();
  putwchar((int) wc);
}

-

#include <locale.h>
#include <wchar.h>
#include <wctype.h>
int main(void)
{
  setlocale(LC_CTYPE, "en_US.UTF-8");
  int wc;
  wc = L'ΓΏ';
  if (iswlower((unsigned int) wc)) return 0;
  return 1;
}
+4
1

TL; DR:

wchar_t wchar_t?

C , .


char .

, " char , , char unsigned char." (C2011, 6.2.5/15)

, signed char unsigned char.

", , , , , signed char unsigned char , , .

, , , char, unsigned char

, . , , char, , , . , - char .

getchar() . int, , , - . , : int unsigned char, char unsigned char.

int int getchar(), stdio . char - int, ( ) . , .

, ISO C90, wchar_t, wchar_t.

C90 , , , - C2011 (7.19/2), wchar_t

, , [...].

glibc , , , glibc. , , , . , , , . , , , , char, wchar_t char. , .

:

, >= 0 ( wchar_t). - , ?

, , , , , , , , , , , , C. , , , C.

, , wchar_t 16- (), 15 ?

C . wchar_t. , , , 32767, wchar_t.

, , wchar_t ?

C . , wchar_t ( , ). , , - , ( ) , set . 1 wchar_t.

, , , wchar_t wint_t. ?

, "". , wint_t

, , , , .

(C2011, 7.29.1/2)

wchar_t , , . wint_t . , wchar_t , . , . wint_t .

, , 32767, wchar_t 16- , wint_t 16- . , wchar_t, , wint_t ( wint_t , - ).

, . char , getchar() - -1, , , unsigned char. , wint_t, , .

,

iswlower((unsigned wchar_t)wc) , unsigned wchar_t.

, . wchar_t iswlower() , , , . , . unsigned wchar_t, C , , , .


, :

, , unsigned int int ? ( wint_t wchar_t glibc)

. , , , wchar_t int wint_t unsigned int.

, , getwchar() WEOF. WEOF wchar_t, , , . , putwchar() . , WEOF , UINT_MAX ( <<27 > ), int , , putwchar().

, , , , , , getwchar() , WEOF, , wchar_t. , , , int ( wchar_t) .

, , , . wchar_t wint_t, , . ( , .)

, , , :

#include <locale.h>
#include <wchar.h>
int main(void)
{
  setlocale(LC_CTYPE, "en_US.UTF-8");
  wint_t wc = getwchar();
  if (wc != WEOF) {
    // No cast is necessary or desirable
    putwchar(wc);
  }
}

:

#include <locale.h>
#include <wchar.h>
#include <wctype.h>
int main(void)
{
  setlocale(LC_CTYPE, "en_US.UTF-8");
  wchar_t wc = L'ΓΏ';
  // No cast is necessary or desirable
  if (iswlower(wc)) return 0;
  return 1;
}
+7

Source: https://habr.com/ru/post/1661659/


All Articles