sprintf() with automatic memory allocation?

Here is the original answer from Stack Overflow. As others have mentioned, you need snprintf not sprintf. Make sure the second argument to snprintf is zero. That will prevent snprintf from writing to the NULL string that is the first argument. The second argument is needed because it tells snprintf that enough space is not … Read more

C Programming: Forward variable argument list

Don’t pass the results to printf. pass them to vprintf. vprintf specifically exists to handle passing in va_list arguments. From the Linux man page: #include <stdio.h> int printf(const char *format, …); int fprintf(FILE *stream, const char *format, …); int sprintf(char *str, const char *format, …); int snprintf(char *str, size_t size, const char *format, …); #include … Read more

printf string, variable length item

There is no need to construct a special format string. printf allows you to specify the precision using a parameter (that precedes the value) if you use a .* as the precision in the format tag. For example: printf (“%d %.*s”, number, SIZE, letters); Note: there is a distinction between width (which is a minimum … Read more

Alternatives to type casting when formatting NS(U)Integer on 32 and 64 bit architectures?

From http://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html: z Specifies that a following […] conversion specifier applies to a size_t or the corresponding signed integer type argument; t Specifies that a following […] conversion specifier applies to a ptrdiff_t or the corresponding unsigned type argument; And from http://en.wikipedia.org/wiki/Size_t#Size_and_pointer_difference_types: size_t is used to represent the size of any object (including arrays) in … Read more