...
The variable arguments to a variadic function are not checked for type by the compiler. SoAs a result, the programmer is responsible for ensuring that they are compatible with the corresponding parameter after the default argument promotions:
...
The C99 printf()
function is implemented as a variadic function. This noncompliant code example swaps its null-terminated byte string and integer parameters with respect to how they were are specified in the format string. Consequently, the integer is interpreted as a pointer to a null-terminated byte string and dereferenced. This will likely cause the program to abnormally terminate. Note that the error_message
pointer is likewise interpreted as an integer.
...
Compliant Solution (Type Interpretation Error)
This compliant solution is formatted modifies the format string so that the specifiers are consistent with their parametersconversion specifiers correspond to the arguments.
Code Block | ||
---|---|---|
| ||
const char *error_msg = "Error occurred"; /* ... */ printf("%d:%s", 15, error_msg); |
...