| Contents |

32 <wctype.h> Phân Loại và Biến Đổi Wide Character

Hàm Mô tả
iswalnum() Kiểm tra wide character có là chữ cái hoặc chữ số.
iswalpha() Kiểm tra wide character có là chữ cái
iswblank() Kiểm tra đây có phải wide blank character
iswcntrl() Kiểm tra đây có phải wide control character.
iswctype() Xác định phân loại wide character
iswdigit() Kiểm tra wide character có là chữ số
iswgraph() Kiểm tra wide character có in được mà không phải space
iswlower() Kiểm tra wide character có là chữ thường
iswprint() Kiểm tra wide character có in được
iswpunct() Kiểm tra wide character có là dấu câu
iswspace() Kiểm tra wide character có là whitespace
iswupper() Kiểm tra wide character có là chữ hoa
iswxdigit() Kiểm tra wide character có là chữ số hex
towctrans() Chuyển wide character sang chữ hoa hoặc chữ thường
towlower() Chuyển wide character hoa sang chữ thường
towupper() Chuyển wide character thường sang chữ hoa
wctrans() Hàm phụ trợ cho towctrans()
wctype() Hàm phụ trợ cho iswctype()

Cái này giống <ctype.h> nhưng dành cho wide character (ký tự rộng).

Với nó bạn có thể kiểm tra phân loại ký tự (kiểu “ký tự này có phải whitespace không?”) hoặc làm vài chuyển đổi ký tự cơ bản (kiểu “ép ký tự này thành chữ thường”).


32.1 iswalnum()

Kiểm tra wide character có là chữ cái hoặc chữ số.

Synopsis

#include <wctype.h>

int iswalnum(wint_t wc);

Description

Về cơ bản là kiểm tra xem một ký tự có là chữ cái (A-Z hoặc a-z) hoặc chữ số (0-9). Nhưng vài ký tự khác cũng có thể tính là hợp lệ tùy theo locale.

Cái này tương đương với việc kiểm tra iswalpha() hoặc iswdigit() có true không.

Return Value

Trả về true nếu ký tự là chữ cái hoặc chữ số.

Example

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                  testing this char
    //                           v
    wprintf(L"%ls\n", iswalnum(L'a')? L"yes": L"no");  // yes
    wprintf(L"%ls\n", iswalnum(L'B')? L"yes": L"no");  // yes
    wprintf(L"%ls\n", iswalnum(L'5')? L"yes": L"no");  // yes
    wprintf(L"%ls\n", iswalnum(L'?')? L"yes": L"no");  // no
}

See Also

iswalpha(), iswdigit(), isalnum()


32.2 iswalpha()

Kiểm tra wide character có là chữ cái

Synopsis

#include <wctype.h>

int iswalpha(wint_t wc);

Description

Về cơ bản là kiểm tra một ký tự có là chữ cái (A-Z hoặc a-z). Nhưng vài ký tự khác cũng có thể tính là hợp lệ tùy theo locale. (Nếu ký tự khác được tính, chúng sẽ không phải ký tự điều khiển, chữ số, dấu câu hay space.)

Cái này giống như kiểm tra iswupper() hoặc iswlower().

Return Value

Trả về true nếu ký tự là chữ cái.

Example

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                  testing this char
    //                           v
    wprintf(L"%ls\n", iswalpha(L'a')? L"yes": L"no");  // yes
    wprintf(L"%ls\n", iswalpha(L'B')? L"yes": L"no");  // yes
    wprintf(L"%ls\n", iswalpha(L'5')? L"yes": L"no");  // no
    wprintf(L"%ls\n", iswalpha(L'?')? L"yes": L"no");  // no
}

See Also

iswalnum(), isalpha()


32.3 iswblank()

Kiểm tra đây có phải wide blank character

Synopsis

#include <wctype.h>

int iswblank(wint_t wc);

Description

Blank character là whitespace đồng thời được dùng làm dấu ngăn từ trên cùng một dòng. Ở locale “C”, các blank character duy nhất là space và tab.

Các locale khác có thể định nghĩa blank character khác.

Return Value

Trả về true nếu đây là blank character.

Example

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                   testing this char
    //                           v
    wprintf(L"%ls\n", iswblank(L' ')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswblank(L'\t')? L"yes": L"no");  // yes
    wprintf(L"%ls\n", iswblank(L'\n')? L"yes": L"no");  // no
    wprintf(L"%ls\n", iswblank(L'a')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswblank(L'?')? L"yes": L"no");   // no
}

See Also

iswspace(), isblank()


32.4 iswcntrl()

Kiểm tra đây có phải wide control character.

Synopsis

#include <wctype.h>

int iswcntrl(wint_t wc);

Description

Spec khá trống trải ở chỗ này. Nhưng tôi cứ giả định là nó hoạt động giống bản không-wide. Vậy thì xem bản kia.

Control character (ký tự điều khiển) là ký tự không in được, phụ thuộc locale.

Với locale “C”, nghĩa là control character nằm trong khoảng 0x00 đến 0x1F (ngay trước ký tự SPACE) và 0x7F (ký tự DEL).

Về cơ bản nếu không phải ký tự ASCII in được (hoặc Unicode nhỏ hơn 128), thì đó là control character trong locale “C”.

Chắc là vậy.

Return Value

Trả về true nếu đây là control character.

Example

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                   testing this char
    //                           v
    wprintf(L"%ls\n", iswcntrl(L'\t')? L"yes": L"no");  // yes (tab)
    wprintf(L"%ls\n", iswcntrl(L'\n')? L"yes": L"no");  // yes (newline)
    wprintf(L"%ls\n", iswcntrl(L'\r')? L"yes": L"no");  // yes (return)
    wprintf(L"%ls\n", iswcntrl(L'\a')? L"yes": L"no");  // yes (bell)
    wprintf(L"%ls\n", iswcntrl(L' ')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswcntrl(L'a')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswcntrl(L'?')? L"yes": L"no");   // no
}

See Also

iscntrl()


32.5 iswdigit()

Kiểm tra wide character có là chữ số

Synopsis

#include <wctype.h>

int iswdigit(wint_t wc);

Description

Kiểm tra wide character có là chữ số (0-9).

Return Value

Trả về true nếu ký tự là chữ số.

Example

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                   testing this char
    //                           v
    wprintf(L"%ls\n", iswdigit(L'0')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswdigit(L'5')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswdigit(L'a')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswdigit(L'B')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswdigit(L'?')? L"yes": L"no");   // no
}

See Also

iswalnum(), isdigit()


32.6 iswgraph()

Kiểm tra wide character có in được mà không phải space

Synopsis

#include <wctype.h>

int iswgraph(wint_t wc);

Description

Trả về true nếu đây là ký tự in được (không phải control) và cũng không phải whitespace.

Về cơ bản nếu iswprint() true và iswspace() false.

Return Value

Trả về true nếu đây là ký tự in được mà không phải space.

Example

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                   testing this char
    //                           v
    wprintf(L"%ls\n", iswgraph(L'0')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswgraph(L'a')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswgraph(L'B')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswgraph(L'?')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswgraph(L' ')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswgraph(L'\n')? L"yes": L"no");  // no
}

See Also

iswprint(), iswspace(), isgraph()


32.7 iswlower()

Kiểm tra wide character có là chữ thường

Synopsis

#include <wctype.h>

int iswlower(wint_t wc);

Description

Kiểm tra một ký tự có là chữ thường, trong khoảng a-z.

Ở các locale khác, có thể có ký tự chữ thường khác. Trong mọi trường hợp, để là chữ thường thì những điều sau phải đúng:

!iswcntrl(c) && !iswdigit(c) && !iswpunct(c) && !iswspace(c)

Return Value

Trả về true nếu wide character là chữ thường.

Example

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                   testing this char
    //                           v
    wprintf(L"%ls\n", iswlower(L'c')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswlower(L'0')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswlower(L'B')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswlower(L'?')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswlower(L' ')? L"yes": L"no");   // no
}

See Also

islower(), iswupper(), iswalpha(), towupper(), towlower()


32.8 iswprint()

Kiểm tra wide character có in được

Synopsis

#include <wctype.h>

int iswprint(wint_t wc);

Description

Kiểm tra wide character có in được, bao gồm cả space (' '). Nên giống như isgraph(), chỉ khác là space không bị bỏ rơi ngoài trời lạnh.

Return Value

Trả về true nếu wide character in được, bao gồm cả space (' ').

Example

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                   testing this char
    //                           v
    wprintf(L"%ls\n", iswprint(L'c')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswprint(L'0')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswprint(L' ')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswprint(L'\r')? L"yes": L"no");  // no
}

See Also

isprint(), iswgraph(), iswcntrl()


32.9 iswpunct()

Kiểm tra wide character có là dấu câu

Synopsis

#include <wctype.h>

int iswpunct(wint_t wc);

Description

Kiểm tra wide character có là dấu câu.

Ở bất kỳ locale nào, điều này có nghĩa là:

!isspace(c) && !isalnum(c)

Return Value

True nếu wide character là dấu câu.

Example

Kết quả có thể khác nhau tùy locale.

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                   testing this char
    //                           v
    wprintf(L"%ls\n", iswpunct(L',')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswpunct(L'!')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswpunct(L'c')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswpunct(L'0')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswpunct(L' ')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswpunct(L'\n')? L"yes": L"no");  // no
}

See Also

ispunct(), iswspace(), iswalnum()


32.10 iswspace()

Kiểm tra wide character có là whitespace

Synopsis

#include <wctype.h>

int iswspace(wint_t wc);

Description

Kiểm tra c có là ký tự whitespace. Chắc là bao gồm:

Các locale khác có thể đặc tả ký tự whitespace khác. iswalnum(), iswgraph(), và iswpunct() đều false với mọi ký tự whitespace.

Return Value

True nếu ký tự là whitespace.

Example

Kết quả có thể khác nhau tùy locale.

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                   testing this char
    //                           v
    wprintf(L"%ls\n", iswspace(L' ')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswspace(L'\n')? L"yes": L"no");  // yes
    wprintf(L"%ls\n", iswspace(L'\t')? L"yes": L"no");  // yes
    wprintf(L"%ls\n", iswspace(L',')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswspace(L'!')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswspace(L'c')? L"yes": L"no");   // no
}

See Also

isspace(), iswblank()


32.11 iswupper()

Kiểm tra wide character có là chữ hoa

Synopsis

#include <wctype.h>

int iswupper(wint_t wc);

Description

Kiểm tra một ký tự có là chữ hoa trong locale hiện tại.

Để là chữ hoa, những điều sau phải đúng:

!iscntrl(c) && !isdigit(c) && !ispunct(c) && !isspace(c)

Return Value

Trả về true nếu wide character là chữ hoa.

Example

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                   testing this char
    //                           v
    wprintf(L"%ls\n", iswupper(L'B')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswupper(L'c')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswupper(L'0')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswupper(L'?')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswupper(L' ')? L"yes": L"no");   // no
}

See Also

isupper(), iswlower(), iswalpha(), towupper(), towlower()


32.12 iswxdigit()

Kiểm tra wide character có là chữ số hex

Synopsis

#include <wctype.h>

int iswxdigit(wint_t wc);

Description

Trả về true nếu wide character là chữ số hex. Cụ thể là nếu nó thuộc 0-9, a-f, hoặc A-F.

Return Value

True nếu ký tự là chữ số hex.

Example

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                    testing this char
    //                            v
    wprintf(L"%ls\n", iswxdigit(L'B')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswxdigit(L'c')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswxdigit(L'2')? L"yes": L"no");   // yes
    wprintf(L"%ls\n", iswxdigit(L'G')? L"yes": L"no");   // no
    wprintf(L"%ls\n", iswxdigit(L'?')? L"yes": L"no");   // no
}

See Also

isxdigit(), iswdigit()


32.13 iswctype()

Xác định phân loại wide character

Synopsis

#include <wctype.h>

int iswctype(wint_t wc, wctype_t desc);

Description

Đây là con dao Thụy Sĩ của các hàm phân loại; nó gom hết mấy hàm kia vào một.

Bạn gọi nó kiểu này:

if (iswctype(c, wctype("digit")))  // or "alpha" or "space" or...

và nó hành xử y như bạn đã gọi:

if (iswdigit(c))

Khác biệt là bạn có thể đặc tả kiểu matching muốn làm dưới dạng chuỗi lúc runtime, nghe thì tiện.

iswctype() dựa vào giá trị trả về của lời gọi wctype() để làm việc.

Chôm từ spec, đây là các lời gọi iswctype() và các hàm tương đương:

Lời gọi iswctype() Tương đương hard-code
iswctype(c, wctype("alnum")) iswalnum(c)
iswctype(c, wctype("alpha")) iswalpha(c)
iswctype(c, wctype("blank")) iswblank(c)
iswctype(c, wctype("cntrl")) iswcntrl(c)
iswctype(c, wctype("digit")) iswdigit(c)
iswctype(c, wctype("graph")) iswgraph(c)
iswctype(c, wctype("lower")) iswlower(c)
iswctype(c, wctype("print")) iswprint(c)
iswctype(c, wctype("punct")) iswpunct(c)
iswctype(c, wctype("space")) iswspace(c)
iswctype(c, wctype("upper")) iswupper(c)
iswctype(c, wctype("xdigit")) iswxdigit(c)

Xem tài liệu wctype() để biết hàm phụ trợ đó hoạt động ra sao.

Return Value

Trả về true nếu wide character wc khớp với lớp ký tự ở desc.

Example

Kiểm tra một phân loại ký tự nào đó khi không biết phân loại tại thời điểm compile:

#include <stdio.h>  // for fflush(stdout)
#include <wchar.h>
#include <wctype.h>

int main(void)
{
    wchar_t c;        // Holds a single wide character (to test)
    char desc[128];   // Holds the character class

    // Get the character and classification from the user
    wprintf(L"Enter a character and character class: ");
    fflush(stdout);
    wscanf(L"%lc %s", &c, desc);

    // Compute the type from the given class
    wctype_t t = wctype(desc);

    if (t == 0)
        // If the type is 0, it's an unknown class
        wprintf(L"Unknown character class: \"%s\"\n", desc);
    else {
        // Otherwise, let's test the character and see if its that
        // classification
        if (iswctype(c, t))
            wprintf(L"Yes! '%lc' is %s!\n", c, desc);
        else
            wprintf(L"Nope! '%lc' is not %s.\n", c, desc);
    }
}

Output:

Enter a character and character class: 5 digit
Yes! '5' is digit!

Enter a character and character class: b digit
Nope! 'b' is not digit.

Enter a character and character class: x alnum
Yes! 'x' is alnum!

See Also

wctype()


32.14 wctype()

Hàm phụ trợ cho iswctype()

Synopsis

#include <wctype.h>

wctype_t wctype(const char *property);

Description

Hàm này trả về một giá trị opaque cho property cho trước, dùng để truyền làm tham số thứ hai cho iswctype().

Giá trị trả về có kiểu wctype_t.

Các property hợp lệ trong mọi locale là:

"alnum"   "alpha"   "blank"   "cntrl"
"digit"   "graph"   "lower"   "print"
"punct"   "space"   "upper"   "xdigit"

Các property khác có thể được định nghĩa tùy category LC_CTYPE của locale hiện tại.

Xem trang tham khảo iswctype() để biết chi tiết sử dụng.

Return Value

Trả về giá trị wctype_t tương ứng với property cho trước.

Nếu truyền giá trị không hợp lệ cho property, trả về 0.

Example

Kiểm tra một phân loại ký tự nào đó khi không biết phân loại tại thời điểm compile:

#include <stdio.h>  // for fflush(stdout)
#include <wchar.h>
#include <wctype.h>

int main(void)
{
    wchar_t c;        // Holds a single wide character (to test)
    char desc[128];   // Holds the character class

    // Get the character and classification from the user
    wprintf(L"Enter a character and character class: ");
    fflush(stdout);
    wscanf(L"%lc %s", &c, desc);

    // Compute the type from the given class
    wctype_t t = wctype(desc);

    if (t == 0)
        // If the type is 0, it's an unknown class
        wprintf(L"Unknown character class: \"%s\"\n", desc);
    else {
        // Otherwise, let's test the character and see if its that
        // classification
        if (iswctype(c, t))
            wprintf(L"Yes! '%lc' is %s!\n", c, desc);
        else
            wprintf(L"Nope! '%lc' is not %s.\n", c, desc);
    }
}

Output:

Enter a character and character class: 5 digit
Yes! '5' is digit!

Enter a character and character class: b digit
Nope! 'b' is not digit.

Enter a character and character class: x alnum
Yes! 'x' is alnum!

See Also

iswctype()


32.15 towlower()

Chuyển wide character hoa sang chữ thường

Synopsis

#include <wctype.h>

wint_t towlower(wint_t wc);

Description

Nếu ký tự là chữ hoa (tức iswupper(c) true), hàm này trả về chữ thường tương ứng.

Các locale khác nhau có thể có chữ hoa và chữ thường khác nhau.

Return Value

Nếu chữ cái wc là chữ hoa, phiên bản chữ thường của chữ đó sẽ được trả về theo locale hiện tại.

Nếu chữ cái không phải chữ hoa, wc được trả về nguyên không đổi.

Example

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                  changing this char
    //                           v
    wprintf(L"%lc\n", towlower(L'B'));  // b (made lowercase!)
    wprintf(L"%lc\n", towlower(L'e'));  // e (unchanged)
    wprintf(L"%lc\n", towlower(L'!'));  // ! (unchanged)
}

See Also

tolower(), towupper(), iswlower(), iswupper()


32.16 towupper()

Chuyển wide character thường sang chữ hoa

Synopsis

#include <wctype.h>

wint_t towupper(wint_t wc);

Description

Nếu ký tự là chữ thường (tức iswlower(c) true), hàm này trả về chữ hoa tương ứng.

Các locale khác nhau có thể có chữ hoa và chữ thường khác nhau.

Return Value

Nếu chữ cái wc là chữ thường, phiên bản chữ hoa của chữ đó sẽ được trả về theo locale hiện tại.

Nếu chữ cái không phải chữ thường, wc được trả về nguyên không đổi.

Example

#include <wchar.h>
#include <wctype.h>

int main(void)
{
    //                  changing this char
    //                           v
    wprintf(L"%lc\n", towupper(L'B'));  // B (unchanged)
    wprintf(L"%lc\n", towupper(L'e'));  // E (made uppercase!)
    wprintf(L"%lc\n", towupper(L'!'));  // ! (unchanged)
}

See Also

toupper(), towlower(), iswlower(), iswupper()


32.17 towctrans()

Chuyển wide character sang chữ hoa hoặc chữ thường

Synopsis

#include <wctype.h>

wint_t towctrans(wint_t wc, wctrans_t desc);

Description

Đây là con dao Thụy Sĩ của các hàm chuyển đổi ký tự; nó gom hết mấy hàm kia vào một. Và “mấy hàm kia” ở đây nghĩa là towupper()towlower(), vì đó là tất cả những gì có.

Bạn gọi nó kiểu này:

if (towctrans(c, wctrans("toupper")))  // or "tolower"

và nó hành xử y như bạn đã gọi:

towupper(c);

Khác biệt là bạn có thể đặc tả kiểu chuyển đổi muốn làm dưới dạng chuỗi lúc runtime, nghe thì tiện.

towctrans() dựa vào giá trị trả về của lời gọi wctrans() để làm việc.

Lời gọi towctrans() Tương đương hard-code
towctrans(c, wctrans("toupper")) towupper(c)
towctrans(c, wctrans("tolower")) towlower(c)

Xem tài liệu wctrans() để biết hàm phụ trợ đó hoạt động ra sao.

Return Value

Trả về ký tự wc như thể đã chạy qua towupper() hoặc towlower(), tùy giá trị của desc.

Nếu ký tự đã khớp sẵn với phân loại, nó được trả về nguyên xi.

Example

#include <stdio.h>  // for fflush(stdout)
#include <wchar.h>
#include <wctype.h>

int main(void)
{
    wchar_t c;        // Holds a single wide character (to test)
    char desc[128];   // Holds the conversion type

    // Get the character and conversion type from the user
    wprintf(L"Enter a character and conversion type: ");
    fflush(stdout);
    wscanf(L"%lc %s", &c, desc);

    // Compute the type from the given conversion type
    wctrans_t t = wctrans(desc);

    if (t == 0)
        // If the type is 0, it's an unknown conversion type
        wprintf(L"Unknown conversion: \"%s\"\n", desc);
    else {
        // Otherwise, let's do the conversion
        wint_t result = towctrans(c, t);
        wprintf(L"'%lc' -> %s -> '%lc'\n", c, desc, result);
    }
}

Output trên máy tôi:

Enter a character and conversion type: b toupper
'b' -> toupper -> 'B'

Enter a character and conversion type: B toupper
'B' -> toupper -> 'B'

Enter a character and conversion type: B tolower
'B' -> tolower -> 'b'

Enter a character and conversion type: ! toupper
'!' -> toupper -> '!'

See Also

wctrans(), towupper(), towlower()


32.18 wctrans()

Hàm phụ trợ cho towctrans()

Synopsis

#include <wctype.h>

wctrans_t wctrans(const char *property);

Description

Đây là hàm phụ trợ để sinh tham số thứ hai cho towctrans().

Bạn có thể truyền vào một trong hai thứ cho property:

Return Value

Nếu thành công, trả về giá trị có thể dùng làm tham số desc cho towctrans().

Ngược lại, nếu property không nhận ra, trả về 0.

Example

#include <stdio.h>  // for fflush(stdout)
#include <wchar.h>
#include <wctype.h>

int main(void)
{
    wchar_t c;        // Holds a single wide character (to test)
    char desc[128];   // Holds the conversion type

    // Get the character and conversion type from the user
    wprintf(L"Enter a character and conversion type: ");
    fflush(stdout);
    wscanf(L"%lc %s", &c, desc);

    // Compute the type from the given conversion type
    wctrans_t t = wctrans(desc);

    if (t == 0)
        // If the type is 0, it's an unknown conversion type
        wprintf(L"Unknown conversion: \"%s\"\n", desc);
    else {
        // Otherwise, let's do the conversion
        wint_t result = towctrans(c, t);
        wprintf(L"'%lc' -> %s -> '%lc'\n", c, desc, result);
    }
}

Output trên máy tôi:

Enter a character and conversion type: b toupper
'b' -> toupper -> 'B'

Enter a character and conversion type: B toupper
'B' -> toupper -> 'B'

Enter a character and conversion type: B tolower
'B' -> tolower -> 'b'

Enter a character and conversion type: ! toupper
'!' -> toupper -> '!'

See Also

towctrans()


  1. https://beej.us/guide/bgc/↩︎

  2. https://en.wikipedia.org/wiki/ANSI_C↩︎

  3. https://en.wikipedia.org/wiki/POSIX↩︎

  4. https://visualstudio.microsoft.com/vs/community/↩︎

  5. https://docs.microsoft.com/en-us/windows/wsl/install-win10↩︎

  6. https://developer.apple.com/xcode/↩︎

  7. https://beej.us/guide/bgclr/↩︎

  8. https://en.cppreference.com/↩︎

  9. https://groups.google.com/g/comp.lang.c↩︎

  10. https://www.reddit.com/r/C_Programming/↩︎

  11. Việc này không thay đổi kiểu của x ở ngữ cảnh khác, nó chỉ có hiệu lực trong lần dùng cụ thể này trong biểu thức này.↩︎

  12. Lưu ý ngầm định này chỉ áp dụng cho main(), không áp dụng cho bất cứ hàm nào khác.↩︎

  13. https://en.wikipedia.org/wiki/Complex_conjugate↩︎

  14. https://en.wikipedia.org/wiki/Riemann_sphere↩︎

  15. Thật ra nó chỉ cần là modifiable lvalue, nên không nhất thiết là biến. Nhưng bạn cứ xem nó như biến cũng được.↩︎

  16. https://man.archlinux.org/man/errno.3.en↩︎

  17. https://docs.microsoft.com/en-us/cpp/c-runtime-library/errno-constants?view=msvc-160↩︎

  18. https://en.wikipedia.org/wiki/Subnormal_number↩︎

  19. Giá trị nhỏ nhất của unsigned char0. Với unsigned shortunsigned long cũng vậy. Hay bất kỳ kiểu unsigned nào, cũng thế.↩︎

  20. https://en.wikipedia.org/wiki/Two%27s_complement↩︎

  21. Nhớ là char chỉ là integer cỡ một byte.↩︎

  22. Dù hệ thống định nghĩa MATH_ERRNO1MATH_ERREXCEPT2, tốt nhất luôn dùng tên ký hiệu. Phòng hờ.↩︎

  23. https://en.wikipedia.org/wiki/Denormal_number↩︎

  24. Đây là trên máy tôi. Một số hệ thống sẽ có ngưỡng mà số trở thành subnormal khác, hoặc có thể không hỗ trợ giá trị subnormal.↩︎

  25. https://en.wikipedia.org/wiki/E_(mathematical_constant)↩︎

  26. https://en.wikipedia.org/wiki/Denormal_number↩︎

  27. https://en.wikipedia.org/wiki/Pythagorean_theorem↩︎

  28. https://en.wikipedia.org/wiki/Error_function↩︎

  29. https://en.wikipedia.org/wiki/Error_function↩︎

  30. https://en.wikipedia.org/wiki/Gamma_function↩︎

  31. https://en.wikipedia.org/wiki/Gamma_function↩︎

  32. https://en.cppreference.com/w/c/numeric/math/remquo↩︎

  33. https://en.cppreference.com/w/c/numeric/math/remquo↩︎

  34. Một quiet NaN là NaN không raise bất kỳ exception nào.↩︎

  35. https://man.archlinux.org/man/sigaction.2.en↩︎

  36. Như kiểu được gửi từ lệnh kill trên Unix.]↩︎

  37. https://en.wikipedia.org/wiki/Data_structure_alignment↩︎

  38. Có thể nó phụ thuộc vào môi trường run-time và không thể biết ở compile-time.↩︎

  39. https://en.cppreference.com/w/cpp/atomic/ATOMIC_VAR_INIT↩︎

  40. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1138r0.pdf↩︎

  41. Hiệu quả thì cái này làm cùng việc, nhưng rõ ràng nó không nguyên tử.↩︎

  42. Spec nói: “Cái thất bại tự phát này cho phép cài đặt compare-and-exchange trên một lớp máy rộng hơn, ví dụ như các máy load-locked store-conditional.” Và thêm: “Khi compare-and-exchange nằm trong vòng lặp, phiên bản weak sẽ cho hiệu năng tốt hơn trên một số nền tảng. Khi một compare-and-exchange weak sẽ cần vòng lặp còn strong thì không, thì strong được ưu tiên hơn.”↩︎

  43. Đừng dùng cái này trừ khi bạn biết mình đang làm gì—dùng chức năng mutex của thread thay vào đó. Nó sẽ cho phép thread bị chặn ngủ và ngừng gặm CPU.↩︎

  44. Đừng dùng cái này trừ khi bạn biết mình đang làm gì—dùng chức năng mutex của thread thay vào đó. Nó sẽ cho phép thread bị chặn ngủ và ngừng gặm CPU.↩︎

  45. https://en.wikipedia.org/wiki/Data_structure_alignment↩︎

  46. https://man.archlinux.org/man/unlinkat.2.en#DESCRIPTION↩︎

  47. https://man.archlinux.org/man/mkstemp.3.en↩︎

  48. https://nee.lv/2021/02/28/How-I-cut-GTA-Online-loading-times-by-70/↩︎

  49. https://stackoverflow.com/questions/17017331/c99-vscanf-for-dummies/17018046#17018046↩︎

  50. http://man.cat-v.org/unix-1st/3/atof↩︎

  51. http://man.cat-v.org/unix-1st/3/atoi↩︎

  52. https://en.wikipedia.org/wiki/Radix↩︎

  53. https://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator↩︎

  54. https://mumble.net/~campbell/2014/04/28/uniform-random-float↩︎

  55. https://www.gnu.org/software/gsl/doc/html/rng.html↩︎

  56. Fan của Minecraft có thể nhớ rằng khi tạo thế giới mới, họ được cho tuỳ chọn nhập vào một seed số ngẫu nhiên. Giá trị duy nhất đó được dùng để sinh ra toàn bộ thế giới ngẫu nhiên đó. Và nếu bạn của bạn bắt đầu thế giới với cùng seed bạn dùng, họ sẽ nhận được cùng thế giới bạn có.↩︎

  57. https://en.wikipedia.org/wiki/Unix_time↩︎

  58. Spec của C không quy định chính xác time(NULL) sẽ trả về gì, nhưng spec POSIX thì có! Và gần như ai cũng trả về đúng thứ đó: số giây kể từ epoch.↩︎

  59. https://en.wikipedia.org/wiki/Data_structure_alignment↩︎

  60. “Thử tưởng tượng mọi sự sống như bạn biết dừng ngay lập tức và mọi phân tử trong cơ thể bạn nổ tung ở tốc độ ánh sáng.” —Egon Spengler↩︎

  61. https://en.wikipedia.org/wiki/Core_dump↩︎

  62. quick_exit() khác exit() ở chỗ file đang mở có thể không được flush và file tạm có thể không bị xoá.↩︎

  63. “Tại chỗ” (in-place) nghĩa là mảng gốc sẽ chứa kết quả; không có mảng mới nào được cấp phát.↩︎

  64. Nó luôn trả về số byte mà chuỗi đã transform chiếm, nhưng trong trường hợp này vì s1NULL, nó không thực sự ghi ra một chuỗi đã transform.↩︎

  65. Đúng ra nên như vậy vì có spurious wakeup.↩︎

  66. Ừm, ít nhất là chừng nào bạn có đủ core rảnh. OS sẽ lên lịch cho chúng tuỳ khả năng.↩︎

  67. Ví dụ, nếu một destructor làm cho nhiều biến khác được đặt lại.↩︎

  68. Các hệ kiểu Unix có syscall sleep() ngủ theo số giây nguyên. Nhưng thrd_sleep() có lẽ portable hơn và thêm nữa còn cho độ phân giải dưới-giây!↩︎

  69. Khi bạn nói GMT, trừ khi bạn đang nói cụ thể về múi giờ chứ không phải về thời điểm, có lẽ thứ bạn muốn nói là “UTC”.↩︎

  70. Spec thực ra không nói “clock tick”, nhưng tôi… thì nói.↩︎

  71. Trừ khi bạn đang ở trên một hệ thống POSIX nơi time_t chắc chắn là số nguyên, lúc đó bạn có thể trừ. Nhưng bạn vẫn nên dùng difftime() để portable (khả chuyển) tối đa.↩︎

  72. https://man.archlinux.org/man/timegm.3.en↩︎

  73. https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/mkgmtime-mkgmtime32-mkgmtime64?view=msvc-160↩︎

  74. https://man.archlinux.org/man/timegm.3.en↩︎

  75. https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/mkgmtime-mkgmtime32-mkgmtime64?view=msvc-160↩︎

  76. https://en.wikipedia.org/wiki/Unix_time↩︎

  77. https://en.wikipedia.org/wiki/ISO_week_date↩︎

  78. https://en.wikipedia.org/wiki/Symbols_for_Legacy_Computing↩︎

  79. https://stackoverflow.com/questions/17017331/c99-vscanf-for-dummies/17018046#17018046↩︎

  80. Đây là một biến, không phải macro, nên nếu bạn dùng nó để định nghĩa mảng, đó sẽ là mảng có độ dài biến thiên (variable-length array).↩︎


| Contents |