How to convert an integer to a string in C language?

In C language, the sprintf function can be used to convert integers to strings. Here is a simple example code:

#include <stdio.h>

int main() {
    int num = 123;
    char str[10]; // 定义一个字符数组来存储转换后的字符串

    sprintf(str, "%d", num); // 使用sprintf函数将整数转换为字符串

    printf("整数转换为字符串: %s\n", str);

    return 0;
}

In the above code, an integer num is first defined, followed by a character array str to store the converted string. The sprintf function is used to convert the integer to a string and store the result in the character array str. Finally, the printf function is used to print the converted string.

bannerAds