A format attribute tells the compiler which function parameter contains a printf-like format specification and where the variable arguments begin. For snprintf, positions 3 and 4 identify those parameters, enabling checks for nonliteral formats and mismatched argument types. section 10.2.3
Annotating snprintf
#if __has_c_attribute(__gnu__::__format__) [[__gnu__::__format__(__printf__, 3, 4)]] #endif int snprintf(char *buf, size_t size, const char *frmt, ...); section 10.2.3
Keep these portability and preprocessing rules in mind:
- Use compiler-specific prefixes such as gnu or clang; implementations may accept attributes from another compiler family. section 10.2.3
- Guard optional attributes with __has_c_attribute and provide a fallback that evaluates the test to zero when unavailable. section 10.2.3
- Remember that attribute identifiers are subject to macro replacement and are not keywords. section 10.2.3
