gcc attribute format - MarekBykowski/readme GitHub Wiki

HOME » Linux Kernel facilities » gcc attribute format

format (archetype, string-index, first-to-check)
where string-index is the number of the argument to check against. Then the arguments starting from first-to-check will be checked against. If mismatch GCC will complain. For example for myFormatText2 (const char *, ...) __attribute__((format(printf, 1, 2))); const char * is the 1st argument of myFormatText2 function to check against, and a number of aruguments to type check are 2. See below:

#include <stdio.h>

myFormatText1 (const 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...
}
⚠️ **GitHub.com Fallback** ⚠️