What is wcstoumax in C ?

The wcstoumax function converts a wide string to an unsigned integer. wchar is the pointer that points to the string that is to be converted.

Wide string is the one that contains characters larger than 8 bits.

The wchar.h header needs to be included to use this function as shown below:

#include <wchar.h>

Prototype

The function prototype is as follows:

uintmax_t wcstoumax(const wchar* widestr, wchar** endPtr, int base);

Parameters

The function takes the following parameters:

widestr - a pointer type that points to wide string.

endPtr - a pointer type that provides reference to object of type wchar. This can be null if it is not used.

base - an integer that specifies the base of the integer value to be converted.

Return value

Figure 1: Return values of valid and invalid conversions

The conversion is invalid when the return value falls out of range. For example, the range for base is {0, 1, 2, …, 36}.

Example

The following code shows the use of the wcstoumax function for different values of widestr and base:

#include <inttypes.h>
#include <stdio.h>
#include <string.h> // for string
#include <wchar.h> // for wchar_t
int main(void)
{
wchar_t* endptr;
wprintf(L"Value 1 is %ld\n", wcstoumax(L"11111111", &endptr, 2)); // base 2
wprintf(L"Value 2 is %ld\n", wcstoumax(L"1wer", &endptr, 2)); // base 2
wprintf(L"Value 3 is %ld\n", wcstoumax(L" -123junk", &endptr, 10)); // base 10
wprintf(L"Value 4 is %ld\n", wcstoumax(L"XyZ",&endptr, 36)); // base 36
}

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved