开发者

What's the use of defining ARGS in a different header file?

开发者 https://www.devze.com 2023-02-23 13:29 出处:网络
So I\'ve been going through some code, and there\'s some things I can\'t understand. I have two header files. One is called \'args.h\' and in that there are these statements, amongst others:

So I've been going through some code, and there's some things I can't understand. I have two header files. One is called 'args.h' and in that there are these statements, amongst others:

#if (defined(__cplusplus) || defined(__STDC__) || defined(c_plusplus))
#define NEW_STYLE 1
#define VOID    void
#define ARGS(parenthesized_list) parenthesized_list
#else
#define NEW_STYLE 0
#define VOID
#define ARGS(parenthesized_list) ()
#define const
#endif

#if !defined(EXIT_SUCCESS)
#define EXIT_SUCCESS    0
#define EXIT_FAILURE    1
#endif

In the other header file, function prototypes are declared like this:

#if defined(__cplusplus)
extern "C" {
#endif

extern void     yyerror ARGS((const char *s_));
extern int      yylex ARGS((void));
extern int      yyparse ARGS((void));
extern int      yywrap ARGS((void));

#if defined(__cplusplus)
}
#endif

and a bunch of other stuff.

So my questions are:

1> What exactly does #开发者_StackOverflowdefine const do?

2> Why is arg declared in the other header file? Couldn't we simply declare the functions like a normal extern void a(const char *s__)? Or is this simply a preference of style?

Thanks.


This is to allow the code to compile with a pre-standard C compiler. It turns a function prototype into a function declaration, and simply removes const completely.

If you need to use a compiler so ancient that it doesn't understand prototypes or const, you have little choice but to use something like this. Otherwise, you're generally best off eliminating these horrible kludges.

20 years ago, code like this was common and necessary. It seems harder to excuse today, but I suppose there may still be a few platforms for which a reasonably modern compiler isn't available.


That are tweaks to make the code portable among compilers lacking this or that feature

  1. is removing const everywhere (for sure not a good idea if you have a modern compiler)
  2. this has to do with the ANSI C Syntax
0

精彩评论

暂无评论...
验证码 换一张
取 消