Javatpoint Logo
Javatpoint Logo

towctrans() function in C++

As a general-purpose programming language, C++ accommodates several functions that are useful for handling and processing the information under consideration. In C++, a little lesser known but very useful function involves towctrans(). It belongs to the <cwctype> library, intended mainly for character class and/or kind.

Towctrans() function of C++ turns wide character by means of a chosen conversion table. It's most helpful in relation to broad characters and their respective translations, particularly in localities wherein character mappings vary significantly.


The syntax of towctrans() is as follows:

wint_t: It is the one broad character you need to retranslate.

wctrans_t desc: It is a translation descriptor that identifies a particular translation table in use. It translates a wide character into one and a half bytes. The special case is when the wide character is invalid, and thus it returns the same character.


Let's take an example to understand the use of towctrans() function in C++:


Original Character: é
Translated Character: É


Header Inclusions:

In this example, the program has important preprocessors such as <iostream>, iot <clocale>, and charmanip <cwctype>.

Locale Setting:

The program sets the locale using the std::setlocale of the script to fr_FR.UTF-8. It concerns adequate wide character management under French grammar conventions.

Wide Character Definition:

An initial wide character is specified as originalChar = L'é. In this context, the letter 'e' is diacritically marked and refers to the accented letter é.

Translation Descriptor Creation:

A translation descriptor (wctrans_t) named toUpperDesc is created using std::wctrans("toupper"). It is a converter of characters into capital letters.

Character Translation:

The towctrans() function is invoked to transform the original wide character ('originalChar') into uppercase based on the existing descriptive translator ('toUpperDesc').

Results Display:

The results of the translation are displayed using std::wcout. These include the wide character to be used as it is and the wide character in its translated form output into the console.

Program Termination:

At the end of the main() function, the program would return 0, meaning that it was successfully executed.

Use Cases and Considerations

The towctrans() function is very useful whenever you require character conversions based on a given locale. Here are a few use cases where this function can be useful:

Locale-Specific Text Processing:

If you are engaged in the internationalization and localization of your C++ app, towctrans() is quite useful in its ability to normalize wide characters based on locale criteria.

Character Case Conversion:

In the example, this function is illustrated and is used to change characters to uppercase or lowercase, respectively, according to the locale. It is important for effective caseload management in various languages.

Specialized Text Processing:

The special handling of certain characters may depend on some language or locale. Such cases can be handled with the help of the towctrans() function which enables definition of translation tables.


In conclusion, the C++ code demonstrates the application of towctrans() to translate wide characters in specific language settings or locales. The program strategically employs the std::setlocale() function, which must be used to specify a particular locale, i.e., for writing in French (fr_,FR_,UTF_-8) where the necessary handling of the accented characters and language-specific rules will work out properly. The towctrans() created a translation descriptor toUpperDesc that demonstrates flexibility in converting characters from lower to upper cases when translating an accented 'é' into its uppercase form.

This code provides an actual example of how developers could use the locale information to enable text-handling features in globalized applications. The emphasis on locale settings means that any handling of character transformation must be done based on an appropriate language. Such a move will lead to more flexible and stronger text processing capabilities in C++ programs that address diverse language requirements to achieve the ultimate objectives of software localization and internationalization.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA