int to str c++
时间: 2023-09-07 17:17:42 浏览: 43
There is no built-in function to convert an integer to a string in C. However, you can use the sprintf function to achieve this. Here is an example:
```
int num = 123;
char str[10];
sprintf(str, "%d", num);
```
In this example, we first declare an integer variable `num` with the value of 123. We also declare a character array `str` with a length of 10 to store the result.
The `sprintf` function takes three arguments: the first argument is the target string where the result will be stored, the second argument is a format string that specifies how the integer should be converted to a string, and the third argument is the integer value that we want to convert.
In this case, the format string is simply "%d", which tells `sprintf` to convert the integer to a decimal number. The resulting string will be stored in `str`.
Note that the size of the `str` array should be large enough to hold the resulting string, including the null terminator. In this example, we used a size of 10, which is more than enough to hold a 3-digit integer plus the null terminator.