Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is wcstof in C?

Abdul Monum

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.

wcstof in C converts the first valid floating-point representation in a wide string to the type float, and moves the pointer to the wide string ahead of the float number. Valid floating-point representations include:

  • Decimal floating-point value. Can include plus or minus sign and ee or EE to represent base 10 exponent.
  • Hexadecimal floating-point value. Can include plus or minus sign and pp or PP to represent base 2 exponent.
  • Infinity value. Can include plus or minus sign and ignores cases of INF or INFINITY.
  • NaN value. Can include plus or minus sign and ignores cases of NAN.

The function is defined in the wchar.h header file and the function declaration is in the standard C library, as follows:

float wcstof(const wchar_t * restrict str, wchar_t ** restrict str_end);
  • If wcstof does not find a valid floating-point conversion, it returns 0.0.
  • If str_end is pointing to NULL, then the function ignores str_end.

NOTE: A wide string uses characters from the Unicode character set, where each character is typically 2-bytes.

Parameters

str: Wide string that is to be analyzed.

str_end: Pointer to the wide string.

Return value

First valid floating-point representation as type float.

Example

#include <wchar.h>
#include <stdio.h>

int main ()
{
  wchar_t str[] = L"76.5 -0.82e+2 0x19 xyz";
  wchar_t * str_end = str;
  float num = 1;

  while(num)
  {
      num = wcstof(str_end, &str_end);
      printf("%f\n", num);
  }
  
  return 0;
}
str_end position after execution of each wcstof() statement

Explanation

First, we import the wchar.h header file and initialize our wide string. The L identifier before the string indicates that subsequent characters are to be used from the Unicode character set. str_end is initialized to point to the start of str, and num is set to 1 to start the while loop.

In line 12, we update the num value using the wcstof method, which returns the first valid floating-point representation in the wide string. str_end then moves to the whitespace ahead of the floating-point number. The loop continues, and when str_end moves to point to xyz, wcstof returns 0 and the loop is terminated.

RELATED TAGS

c

CONTRIBUTOR

Abdul Monum
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