...
When using printf(const char *format, ...)
or its variants, string literals should be used for the format
parameter as opposed to a char *
variable. The reason is because the compile can check the string literal’s %
symbols, and match them against the number of arguments passed into printf()
variants. The consequence of not matching the parameters with %
symbols is stack corruption, hence, the compiler checks are vital for this use-case.
Code Block | ||
---|---|---|
| ||
int value = 1; // Good printf("Value is %d", value); // Bad const char* const format = "Value is %d"; printf(format, value); |
...