What is the meaning of these C macros (protos, #x ?, __unused)?

#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__)) 
  • What is protos ? Where is this defined?
  • What is #x ?
  • Why is __unused needed when __unused__ already exists?
  • Where is __const__ , __noreturn__ , __unused__ defined?
+4
source share
2 answers
  • protos is a macro parameter. It is defined in __P(protos) , and its scope is to the end of the line. In this case, the macro int func(__P(int foo)) will be replaced by int func(int foo) , which is a prototype of the ANSI style function, unlike the pre-standard C, which does not necessarily declare functional parameters. On such a preliminary standard compiler, the macro will be defined without extension, so the compiler will only see int func() .

  • #x is the stringize operator. It turns the contents of its argument x into a string by adding quotes. If the argument passed to x contains macros, they are not expanded until the string is complete.

  • These macros are used to define different things for different platforms. __unused can expand to various objects on GCC or MSVC.

  • These are hooks for the inside of the compiler. The header file provides an interface between the internal components of the compiler 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__ .

+8
source
  • protos is the __P parameter that has just been passed.
  • #x means make a string from x . __STRING(abc) is replaced by "abc"
  • Perhaps service, cross-platform, or uniform reasons. Hard to know without context.
  • Compiler extension. See the documentation for the compiler.
+4
source

Source: https://habr.com/ru/post/1501592/


All Articles