C Standard Library

C <inttypes.h> - wcstoimax() Function



The C <inttypes.h> wcstoimax() function is used to interpret an integer value of the specified base in a wide string pointed to by str.

The function first discards any whitespace characters (as determined by iswspace()) until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many characters as possible to form a valid integer number (of specified base) representation and converts them to an integer value.

The valid integer value consists of the following parts:

  • (optional) plus or minus sign
  • (optional) prefix indicating octal or hexadecimal base ("0" or "0x"/"0X" respectively)
  • a sequence of digits.

The set of valid values for base is {0,2,3,...,36}. The set of valid digits for base-2 integers is {0,1}, for base-3 integers is {0,1,2}, and so on. For bases larger than 10, valid digits include alphabetic characters, starting from Aa for base-11 integer, to Zz for base-36 integer. The case of the characters is ignored.

For locales other than the "C" locale, additional numeric formats may be accepted.

If the value of base is 0, the numeric base is auto-detected: if the prefix is 0, the base is octal, if the prefix is 0x or 0X, the base is hexadecimal, otherwise the base is decimal.

If str_end is not a null pointer, the function also sets the value of str_end to point to the first character after the number. If str_end is a null pointer, it is ignored.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists (because either empty or contains only whitespaces), no conversion is performed and zero is returned.

Note: This function is equivalent to wcstol() function for intmax_t.

Syntax

intmax_t wcstoimax (const wchar_t* str, wchar_t** str_end, int base);

Parameters

str Specify pointer to the null-terminated wide string to be interpreted.
str_end Specify pointer to a pointer to character.
base Specify base of the interpreted integer value.

Return Value

On success, the function returns the converted integral number as a value of type intmax_t. If no conversion can be performed, zero is returned. If the converted value falls out of the range, INTMAX_MAX or INTMAX_MIN is returned, and errno is set to ERANGE.

Example 1:

The example below shows the usage of <inttypes.h> wcstoimax() function.

#include <wchar.h>
#include <inttypes.h>

int main (){
  wchar_t str1[] = L"123";
  wchar_t str2[] = L"10.55";
  wchar_t str3[] = L"100 some words";
  wchar_t str4[] = L"some words 555";

  wchar_t *end;

  intmax_t num1 = wcstoimax(str1, &end, 10);
  intmax_t num2 = wcstoimax(str2, &end, 10);
  intmax_t num3 = wcstoimax(str3, &end, 10);
  intmax_t num4 = wcstoimax(str4, NULL, 10);

  //displaying the result
  wprintf(L"wcstoimax(\"%ls\") = %" PRIdMAX "\n", str1, num1);
  wprintf(L"wcstoimax(\"%ls\") = %" PRIdMAX "\n", str2, num2);
  wprintf(L"wcstoimax(\"%ls\") = %" PRIdMAX "\n", str3, num3);
  wprintf(L"wcstoimax(\"%ls\") = %" PRIdMAX "\n", str4, num4);

  return 0;
}

The output of the above code will be:

wcstoimax("123") = 123
wcstoimax("10.55") = 10
wcstoimax("100 some words") = 100
wcstoimax("some words 555") = 0

Example 2:

Consider the example below where the wide string contains multiple integral values. The second parameter if not a null pointer then this function sets this parameter to value which points to the first character after the interpreted number. This feature can be used to interpret multiple integral values from the wide string.

#include <wchar.h>
#include <inttypes.h>

int main (){
  wchar_t str[] = L"123 10 -555";
  wchar_t *pEnd;

  intmax_t val1 = wcstoimax(str, &pEnd, 10);
  intmax_t val2 = wcstoimax(pEnd, &pEnd, 10);
  intmax_t val3 = wcstoimax(pEnd, &pEnd, 10);

  //displaying the result
  wprintf(L"val1 = %" PRIdMAX "\n", val1);
  wprintf(L"val2 = %" PRIdMAX "\n", val2);
  wprintf(L"val3 = %" PRIdMAX "\n", val3);

  return 0;
}

The output of the above code will be:

val1 = 123
val2 = 10
val3 = -555

Example 3:

Consider one more example where the third parameter of this function is used to interpret integral values in different bases.

#include <wchar.h>
#include <inttypes.h>

int main (){
  wchar_t str[] = L"11011 55 -123 ff 0xff";
  wchar_t *pEnd;

  intmax_t val1 = wcstoimax(str, &pEnd, 2);
  intmax_t val2 = wcstoimax(pEnd, &pEnd, 8);
  intmax_t val3 = wcstoimax(pEnd, &pEnd, 10);
  intmax_t val4 = wcstoimax(pEnd, &pEnd, 16);
  intmax_t val5 = wcstoimax(pEnd, &pEnd, 0);

  //displaying the result
  wprintf(L"val1 = %" PRIdMAX "\n", val1);
  wprintf(L"val2 = %" PRIdMAX "\n", val2);
  wprintf(L"val3 = %" PRIdMAX "\n", val3);
  wprintf(L"val4 = %" PRIdMAX "\n", val4);
  wprintf(L"val5 = %" PRIdMAX "\n", val5);

  return 0;
}

The output of the above code will be:

val1 = 27
val2 = 45
val3 = -123
val4 = 255
val5 = 255

❮ C <inttypes.h> Library