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

How to Convert Numbers and Strings in C ?

Barbara Streisand
Release: 2024-12-27 02:00:08
Original
309 people have browsed it

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);
Copy after login

Converting Strings to Integers

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

int stringToInt = std::stoi("456");
Copy after login

Converting Floating-Point Numbers to Strings

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

std::string floatToString = std::to_string(12.34);
Copy after login

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");
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template