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 
[bash] #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; } [/bash]
 $ gcc -o integer_to_string integer_to_string.c 
 $ ./integer_to_string
buffer = 42 

Leave a Comment