I really thought about it on my own, looking at the question. You can add the parameter number, and then $ to the format, after % . So it would be like this:
const char *fmtNoQuantity = "The price of %2$s is $%3$.2f each.";
That is, the string will use the second parameter, and the float will use the third parameter. Note, however, that this is a POSIX extension, not a standard C function.
The best way would probably be to define a custom print function. Something like that:
typedef enum {fmtDefault, fmtMultiLine, fmtCSV, fmtNoPrices, fmtNoQuantity} fmt_id; void print_record(fmt_id fmt, unsigned int qty, const char *item, float price) { switch (fmt) { case fmtMultiLine: printf("Qty: %3u\n", qty); printf("Item: %s\n", item); printf("Price per item: $%.2f\n\n", price); break; case fmtCSV: printf("%u,%s,%.2f\n", qty, item, price); break; case fmtNoPrices: printf("%ux %s\n", qty, item); break; case fmtNoQuantity: printf("The price of %s is $%.2f each.\n", item, price); break; default: printf("%ux %s ($%.2f each)\n", qty, item, price); break; } }
source share