Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's meaning of this code in C?

Tags:

c

bison

In some Bison code, what does the following line mean?

#define YY_DECL extern "C" int yylex();

I know #define command but I don't understand the whole command.

like image 995
user16948 Avatar asked Dec 20 '22 20:12

user16948


2 Answers

It means that YY_DECL will be expanded to

extern "C" int yylex();

This is actually C++, not C; when you compile this file with a C++ compiler, it declares that the function yylex must be compiled with "C linkage", so that C functions can call it without trouble.

If you don't program in C++, this is largely irrelevant to you, but you may encounter similar declarations in C header files for libraries that try to be compatible with C++. C and C++ can be mixed in a single program, but it requires such declarations for function to nicely work together.

There's probably an #ifdef __cplusplus around this #define; that's a special macro used to indicate compilation by a C++ compiler.

like image 200
Fred Foo Avatar answered Dec 23 '22 08:12

Fred Foo


#define YY_DECL extern "C" int yylex();

Define a macro YY_DECL standing for the declaration of a function yylex that has 'C' linkage inside a C++ program, taking no arguments and returning an int.

like image 44
log0 Avatar answered Dec 23 '22 08:12

log0