/* ** $Id: format.c 777 2008-02-16 01:47:01Z phf $ ** ** Example of using snprintf() to format into a string, ** could be useful in the future... :-) */ #include #include #define SIZE 1024 int main() { char buffer[1024]; // You know what this does... :-) printf("Bla %d, %p, %f..\n", 10, NULL, 10.2); // And you've seen that fprintf() writes to a file... fprintf(stderr, "Bla %d, %p, %f..\n", 10, NULL, 10.2); // But wow, we can write to a string as well, cool if // you want to format something before deciding where // to write it to... :-) int len = snprintf(buffer, SIZE, "Bla %d, %p, %f..", 10, NULL, 10.2); // Note that snprintf() is the "safe" version of sprintf() // which could overwrite stuff by accident; use snprintf() // to be able to check if something went wrong... if (len >= SIZE) { puts("error, something got truncated in snprintf!"); } // Then let's print the buffer. :-) puts(buffer); return EXIT_SUCCESS; }