I was reading "Compiler Design in C" book. In the basics section I found a c snippet for lexer something like this -
static int Lookahead = -1;
int match(token)
int token;
{
if(Lookahead == -1)
Lookahead = lex();
return token == Lookahead;
}
void advance(){
Lookahead = lex();
}
I got confuse about how this match function get compiled on gnu gcc. So I wrote a function that looks like
int a(token)
int token;
{
printf("Value of token is %d", token);
}
int main()
{
printf("Hello world!\n");
a(1);
return 0;
}
And I am getting following output-
Hello world! Value of token is 1
But I dont getting the reason behind that function declaration. What the benefit of declaring function this way? And how the value of token being 1? Why its not a compile error? Is it some kind of function declaration in C?
Thanks for checking my question. Any kind of help will be great.
Its an old and deprecated K&R style of function declaration.
int match(token)
int token;
{
// Function body
}
is equivalent to
int match(int token)
{
// Function body
}
except that in former, compiler would not check that the function is called with the right number of arguments nor will it check the types of arguments. It rely on default argument promotions.
C89 and C99 support this style too.