Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is wcstod in C?

Talha Ashar

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 wcstod function in C interprets a given wide string as a floating-point number. All characters up to the first non-numeric character are converted to a double. The wcstod function ignores any leading whitespaces.

The process is illustrated below:

Note: A wide string is comprised of characters from the Unicode character set.

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

#include <wchar.h>

The prototype of the wcstod function is shown below:

double wcstod(const wchar_t *str, wchar_t **str_end);

Parameters

The wcstod function takes the following objects as parameters:

  • str: The wide string to be converted.
  • str_end: A pointer to a wide character. The value of this pointer is set to the next character in str after the last valid floating-point character. This parameter may also be a null pointer.

Return value

If the conversion is successful, the wcstod function returns a double value; otherwise, 00 is returned.

If the converted value is out of range, the wcstod function returns a positive or negative HUGE_VAL.

Example

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

#include <stdio.h>
#include <wchar.h>
int main() {
// initilaizing strings
wchar_t str[] = L"2021.2Educative22";
wchar_t* str_end;
double result;
// converting string to double
result = wcstod(str, &str_end);
// output results
printf("The converted double is %f.\n", result);
printf("The remaining string is \'%ls\'.", str_end);
return 0;
}

Explanation

First, the wide string we want to convert is initialized. 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 wcstod function in Line 12 proceeds to convert the given wide string, “2021.2Educative22”, to a double.

Since the first 66 characters (“2021.2”) represent a valid floating-point number, the conversion is successful, and the wcstod function returns the value 2021.22021.2. The remaining string, “Educative22”, is stored at the location pointed to by str_end.

RELATED TAGS

c

CONTRIBUTOR

Talha Ashar
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