Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of these C Macros (protos, #x?, __unused)? [closed]

Tags:

c

macros

#if defined(__STDC__) || defined(__cplusplus)
#define __P(protos) protos      /* full-blown ANSI C */
#define __CONCAT(x,y)   x ## y
#define __STRING(x) #x


 #define    __unused    __attribute__((__unused__))
 #define    __dead2     __attribute__((__noreturn__))
 #define    __pure2     __attribute__((__const__))
  1. What's protos? Where is it defined?
  2. What's #x?
  3. Why need __unused when __unused__ already existed?
  4. Where are __const__, __noreturn__, __unused__ defined?
like image 569
lilzz Avatar asked Dec 11 '22 11:12

lilzz


1 Answers

  1. protos is the macro parameter. It's defined in __P(protos) and its scope is until the end of the line. In this case, the macro invocation int func(__P(int foo)) would be replaced by int func(int foo), which is an "ANSI style" function prototype, as opposed to pre-standard C which did not necessarily declare function parameters. On such a pre-standard compiler, the macro would be defined with no expansion, so the compiler would see only int func().

  2. #x is the stringize operator. It turns the content of its argument x into a string by adding quotes. If the argument passed to x contains macros, they are not expanded before the string conversion is done.

  3. These macros are used to define different things for different platforms. __unused might expand to different things on GCC or MSVC.

  4. They are hooks to the inside of the compiler. The header file is providing an interface between the compiler internals and the standard language. The compiler could work directly with __unused as an extension keyword, but its authors preferred to define a uniform interface around __attribute__.

like image 187
Potatoswatter Avatar answered Dec 28 '22 11:12

Potatoswatter