How can integers be converted to strings in the C language?

In C language, you can use the sprintf function to convert integers to strings. The prototype of the sprintf function is as follows:

int sprintf(char *str, const char *format, ...);

In this case, str is a pointer to the target string, format is the formatting string, and the following parameters are the integers to be converted.

Here is a sample code:

#include <stdio.h>

int main() {
    int num = 12345;
    char str[10];

    sprintf(str, "%d", num);

    printf("The integer is: %d\n", num);
    printf("The string is: %s\n", str);

    return 0;
}

The output result is:

The integer is: 12345
The string is: 12345

In the example above, we converted the integer num to a string using the sprintf function and stored the string in the array str. Finally, we printed out the integer and the string separately using the printf function.

bannerAds