Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Forcing a keyword to be treated as function

Reply
Thread Tools

Forcing a keyword to be treated as function

 
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-05-2006
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

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      06-05-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) schrieb:
> 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
and baz<->foo


> 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-05-2006

Michael Mair wrote:

> 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

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-05-2006


(E-Mail Removed) wrote On 06/05/06 16:05,:
> 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.

--
(E-Mail Removed)

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-05-2006
(E-Mail Removed) writes:
> Michael Mair wrote:
>> 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 ?


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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-05-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 PM
How to get child controls not be treated as properties ? Dmitriy Zakharov ASP .Net 2 07-30-2004 08:41 PM
Commented Code Not Treated As Commented Code Jim Heavey ASP .Net 1 12-02-2003 02:25 PM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments