VOOZH about

URL: https://www.geeksforgeeks.org/cpp/cpp-17-charconv_header/

⇱ C++17 - <charconv> Header - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C++17 - <charconv> Header

Last Updated : 28 Apr, 2025

The C++ <charconv> header provides many functions for converting the character sequences to numerical values and vice-versa. It is considered better than the <cstdlib> header file functions for the same purpose. The functions provided by the <charconv> header file are generally faster than the functions provided by the <cstdlib> header file. 

 It was introduced in C++17 and the main point of designing this header was to improve the complexity and performance of the code.

Functions in <charconv> Header

There are two functions available in charconv library which are:

1. to_chars()

The to_chars() function is used to convert a number to its corresponding character representation and stores the result in a buffer.

Syntax

to_chars_result to_chars( char* first, char* last, T value );

Parameters

  • First and last: Pointers to the beginning and end of the buffer where the output will be written.
  • value: The numeric value to be converted.

Return Value

  • It returns the object of type to_chars_result which contains two members:
    • ptr: A pointer to the first character after the end pointer of the characters written.
    • ec: It is an error code that determines if the conversion is successful or not. If the conversion is successful, the error code will be set to errc else it will be set to an error code if an error occurred.

Example

This code demonstrates how to use the to_chars() function from the <charconv> header to convert an integer value to a character sequence.


Output

Converted value: 42

2. from_chars()

The from_chars() function is used to convert the character representation of a number to its corresponding numeric representation.

Syntax

from_chars_result from_chars( const char* first, const char* last, T& value );

Parameters

  • first and last: Pointers to the beginning and end of the character to be converted.
  • value: Reference to the numeric value to be converted.

Return Value

  • It returns the object of type from_chars_result which contains two members:
    • ptr: A pointer to the first character after the end pointer of the characters written.
    • ec: It is an error code that determines if the conversion is successful or not. If the conversion is successful, the error code will be set to errc else it will be set to an error code if an error occurred.

Example 1:

This code demonstrates how to use the from_chars() function from the <charconv> header to convert a string to an integer value.


Output

42

Example 2:

This code demonstrates how to use the from_chars() function from the <charconv> header to convert a string to a double value.


Output

Converted value: 1234.56

Advantages of Using <charconv>

Following are the few advantages of using <charconv> library functions in C++:

  • Performance: This header has a lot of standard functions that minimize memory allocation and improve the performance of the code.
  • Error handling: It produces the from_chars_result that will tell you whether the conversation was a success or it has some error.
  • Flexibility: It can handle a wide range of input formats and ensures the scalability of the code.
Comment
Article Tags:
Article Tags: