Forcing a keyword to be treated as function

S

spibou

I read in the "ctype.h - macros or functions?" thread that
if foo is both the name of a function and a macro , by typing
(foo) you instruct the compiler to use the function version.
Can someone explain to me why this works ?

Cheers
Spiros Bousbouras
 
M

Michael Mair

I read in the "ctype.h - macros or functions?" thread that
if foo is both the name of a function and a macro , by typing
(foo) you instruct the compiler to use the function version.

Consider
,-- macrofun.c -
#include <stdio.h>
#define bar baz
#define foo(s) bar(s)

void (foo) (const char *s);
void (bar) (const char *s);

int main (void)
{
foo("Hello");
(foo)("world");

return 0;
}

void (foo) (const char *s)
{
printf(" *%s*\n", s);
}

void baz (const char *s)
{
printf("%s", s);
}

`---
and play with the parentheses around foo for the three locations
Can someone explain to me why this works ?

The short of it: The C preprocessor performs text replacement;
"(foo)(something)" does not match the pattern "foo(s)",
"foo(something)" does.

Cheers
Michael
 
S

spibou

Michael said:
The short of it: The C preprocessor performs text replacement;
"(foo)(something)" does not match the pattern "foo(s)",
"foo(something)" does.

How is "(foo)(something)" tokenized ?

Cheers
Spiros Bousbouras
 
E

Eric Sosman

I read in the "ctype.h - macros or functions?" thread that
if foo is both the name of a function and a macro , by typing
(foo) you instruct the compiler to use the function version.
Can someone explain to me why this works ?

It's because the macro must be a "function-like" macro,
that is, one like

#define sqrt(x) __builtin_sqrt(x)

and not like

#define sqrt __builtin_sqrt

The preprocessor will not recognize and expand a function-
like macro unless the name is followed by a ( that introduces
the list of arguments. When you write (sqrt)(42.0) the `sqrt'
is followed by a ) and not by a (, so the preprocessor leaves
it alone and does not try to expand it as a macro.
 
K

Keith Thompson

How is "(foo)(something)" tokenized ?

It's the following sequence of tokens:
left-paren
identifier "foo"
right-paren
left-paren
something (whatever that happens to be)
right-paren

Since the answer to your question is so obvious and straightforward, I
suspect you were really trying to ask something else.
 
S

spibou

Keith Thompson wrote:

It's the following sequence of tokens:
left-paren
identifier "foo"
right-paren
left-paren
something (whatever that happens to be)
right-paren

Since the answer to your question is so obvious and straightforward, I
suspect you were really trying to ask something else.

Oh no , I asked what I wanted to ask and in fact the tokenization is
what I thought would happen. But I guess I had in the back of my head
the notion that if a token is the name of a macro , it will be
expanded. But
Eric Sosman explained why this would not happen in this case.

Cheers
Spiros Bousbouras
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top