C program to convert integer to string

Following program converts interger 42 to string 42 using sprintf. sprintf() writes to the character string.

 $ vim integer_to_string.c 
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {
        char buffer[256];
        int i = 42;
        sprintf(buffer, "%d", i);
        printf("buffer = %s\n", buffer);
        return 0;
}
 $ gcc -o integer_to_string integer_to_string.c 
 $ ./integer_to_string
buffer = 42 
Follow Lynxbee

Leave a Reply / Ask Question