Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is wcstoull() in C?

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The wcstoull function in C interprets a given wide string as an unsigned long long integer of a specified base. All characters, up to the first non-numeric character, are converted to an unsigned long long int. The wcstoull function ignores any leading whitespace.

Note: The unsigned long long data type can take on any value between 0 to (264 - 1).

Library

To use the wcstoull function, you will need to include the <wchar.h> library in the program, as shown below:

#include <wchar.h>

Prototype

The prototype of the wcstoull function is shown below:

unsigned long long int wcstoull(const wchar_t *str, wchar_t **str_end, int base);

Parameters

The wcstoull function takes the following objects as parameters:

  • str: The wide string to be converted.
  • str_end: A pointer that references the characters after the last valid numeric character in str. This parameter may also be a null pointer.
  • base: The base of the integral value.

Return value

If the conversion is successful, the wcstoull function returns an unsigned long long integer value; otherwise, it returns 0.

Code

The code below shows how the wcstoull function works in C:

#include <stdio.h>
#include <wchar.h>
int main() {
// initilaizing strings
wchar_t str[] = L" 987ABC@12";
wchar_t* str_end;
unsigned long long int result;
// converting string to long long int
result = wcstoull(str, &str_end, 10);
// output results
printf("The converted unsigned integral number is %llu.\n", result);
printf("The remaining string is \'%ls\'.", str_end);
return 0;
}

Explanation

First, the code initializes the wide string we want to convert. The ‘L’ identifier in line 7 informs the compiler that the Unicode character set is being used. The str_end pointer will contain the string that remains after conversion.

The wcstoull function in line 12 proceeds to convert the given wide string, “ 987ABC@12”, to an unsigned long long integer. For this example, the decimal number system (base 10) is used.

Since the first 3 characters after the whitespaces (“987”) represent valid numeric characters, the conversion is successful and the wcstoull function returns the value 987. The remaining string, “ABC@12”, is stored at the location pointed to by str_end.

RELATED TAGS

c

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring