Home >Backend Development >C++ >How to Convert Numbers and Strings in C ?

How to Convert Numbers and Strings in C ?

Barbara Streisand
Barbara StreisandOriginal
2024-12-27 02:00:08352browse

How to Convert Numbers and Strings in C  ?

Conversion Between Numbers and Strings in C

Converting Integers to Strings

To convert an integer to a string in C , use the std::to_string function:

std::string intToString = std::to_string(123);

Converting Strings to Integers

To convert a string to an integer, use the std::stoi function:

int stringToInt = std::stoi("456");

Converting Floating-Point Numbers to Strings

For floating-point numbers, use the std::to_string function:

std::string floatToString = std::to_string(12.34);

Converting Strings to Floating-Point Numbers

To convert a string to a floating-point number, use the std::stof function:

float stringToFloat = std::stof("56.78");

The above is the detailed content of How to Convert Numbers and Strings in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn