int fontSize = 14;
const char *fontFamily = "Arial";
char style1[200];
snprintf(style1, sizeof style1,
"font-size: %d;font-family: \"%s\"", fontSize, fontFamily);
//style1 is "font-size: 14;font-family: "Arial""
//with a null buffer snprintf only counts the characters
int length = snprintf(nullptr, 0,
"font-size: %.1f;font-family: \"%s\"", fontSize / 2.0,
fontFamily);
char *style2 = malloc(length + 1);
snprintf(style2, length + 1,
"font-size: %.1f;font-family: \"%s\"", fontSize / 2.0,
fontFamily);
//style2 is "font-size: 7.0;font-family: "Arial""
printf("style1 is %s\n", style1);
printf("style2 is %s\n", style2);
free(style2);