You copied the Doc URL to your clipboard.

__attribute__((format)) function attribute

This attribute causes the compiler to check that the supplied arguments are in the correct format for the specified function.

Syntax

__attribute__((format(function, string-index, first-to-check)))

Where function is a printf-style function, such as printf(), scanf(), strftime(), gnu_printf(), gnu_scanf(), gnu_strftime(), or strfmon().

string-index specifies the index of the string argument in your function (starting from one).

first-to-check is the index of the first argument to check against the format string.

Example

#include <stdio.h>

extern char *myFormatText1 (const char *, ...);
extern char *myFormatText2 (const char *, ...) __attribute__((format(printf, 1, 2)));


int main(void) {
  int a, b;
  float c;

  a = 5;
  b = 6;
  c = 9.099999;

myFormatText1("Here are some integers: %d , %d\n", a, b); // No type checking. Types match.
myFormatText1("Here are some integers: %d , %d\n", a, c); // No type checking. Type mismatch, but no warning.


myFormatText2("Here are some integers: %d , %d\n", a, b); // Type checking. Types match.
myFormatText2("Here are some integers: %d , %d\n", a, c); // Type checking. Warning: 181-D: argument is incompatible...
}				

myFormatText1() is a function that is given a string and two arguments to print. It has no format checking, so when it is passed a float argument and the function is expecting an integer, there is a silent type-mismatch.

myFormatText2() is identical to myFormatText1(), except it has __attribute__((format())). When it receives an argument of an unexpected type, it raises a warning message.

Was this page helpful? Yes No