1. __attribute__
One of the major features of GNU C (which is not known to beginners) is the __attribute__ mechanism.
__ATTRIBUTE__ can set function attributes, variable attributes (Variable attribute), and type attributes
There are two underscores before and after the __attribute__, followed by a pair of original brackets, with the corresponding __attribute__ parameters inside the brackets.
The __ATTRIBUTE__ syntax format is:
__attribute__ ((attribute-list))
function attributes, which enable developers to add features to function declarations, which can make the compiler more powerful in error checking.
The __attribute__ mechanism is also easily compatible with non-GNU applications.
GNU CC requires the use of –wall, which is a good way to control warning messages. Here are a few common property parameters.
2. Format
This property enables the compiler to check whether the formatted string is matched between the function declaration and the actual invocation parameters of the function. It can add features like printf or scanf to the declared function, which is useful, especially when dealing with bugs that are hard to find.
The syntax for format is:
Format (archetype, String-index, First-to-check)
The Format property tells the compiler to check the parameters of the function according to the printf,scanf,strftime or Strfmon parameter table-style rules. Archetype: Specifies the style of the;
String-index: Specifies that the first parameter of the Passed-in function is a formatted string;
First-to-check: Specifies that the first few arguments of a function are checked against the above rules.
The specific use of the format is as follows:
__ATTRIBUTE__ ((Format (printf,m,n))
__ATTRIBUTE__ ((Format (scanf,m,n))
Where the argument m and N means:
M: The first parameters are formatted strings (format string);
N: The first parameter in the parameter collection, the argument "...", is ranked in the number of function arguments
Note that sometimes there are "stealth" in the function parameters, which will be mentioned later;
In use, __attribute__ (format (printf,m,n)) is commonly used, while the other is rarely seen.
Here's an example of a function that has a variable parameter defined for itself, and functions like printf: myprint
m=1;n=2
extern void Myprint (const char *format, ...) __attribute__ ((Format (printf,1,2));
m=2; n=3.
extern void Myprint (int l,const char *format, ...) __attribute__ ((Format (printf,2,3));
The special note is that if Myprint is a member function of a function, then the value of M and n can be somewhat "poised", for example:
M=3;n=4
extern void Myprint (int l,const char *format, ...) __attribute__ ((Format (printf,3,4));
The reason for this is that the first argument of a class member function is actually a "this" pointer to "stealth". (A little C + + base all know point this pointer, do not know you here also know?) )
Here is a test case: ATTRIBUTE.C, the code is as follows:
extern void Myprint (const char *format,...) __attribute__ ((Format (printf,1,2));
void Test ()
{
Myprint ("i=% d/n", 6);
Myprint ("i=% s/n", 6);
Myprint ("i=% s/n", "abc");
Myprint ("%s,%d,%d/n", 1,2);
}