C program to convert integer number to string

Following program converts integer number 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;

We can compile this program is using gcc as,

 $ gcc -o integer_to_string integer_to_string.c 

We can compile run program on command line as,

$ ./integer_to_string
buffer = 42 

Leave a Comment