C++ int to string
时间: 2023-08-30 09:09:28 浏览: 117
To convert an integer to a string in C, you can use the sprintf() function. The sprintf() function formats and stores a series of characters and values in a string.
Here's an example:
```c
#include <stdio.h>
int main() {
int num = 123;
char str[20];
sprintf(str, "%d", num);
printf("The integer is: %d\n", num);
printf("The string is: %s\n", str);
return 0;
}
```
Output:
```
The integer is: 123
The string is: 123
```
In the above example, we declared an integer variable "num" and set it to 123. We also declared a character array "str" to store the string version of the integer.
We then used the sprintf() function to convert the integer to a string and store it in the "str" array. The first argument of sprintf() is the buffer to store the string, the second argument is the format string, and the third argument is the integer to be converted.
Finally, we printed both the integer and string versions of the variable to the console.
阅读全文