473,563 Members | 2,897 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forcing a keyword to be treated as function

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

Jun 5 '06 #1
5 3229
sp****@gmail.co m 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)(somethin g)" does not match the pattern "foo(s)",
"foo(something) " does.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 5 '06 #2

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


How is "(foo)(somethin g)" tokenized ?

Cheers
Spiros Bousbouras

Jun 5 '06 #3


sp****@gmail.co m 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.

--
Er*********@sun .com

Jun 5 '06 #4
sp****@gmail.co m writes:
Michael Mair wrote:
The short of it: The C preprocessor performs text replacement;
"(foo)(somethin g)" does not match the pattern "foo(s)",
"foo(something) " does.


How is "(foo)(somethin g)" 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_Keit h) ks***@mib.org <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.
Jun 5 '06 #5

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

Jun 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
3127
by: Bryan Bullard | last post by:
hi, is there a way to force the user of an object to catch exceptions of specific type? for instance a compile error is issued if exception 'ExeceptionXXX' thrown by method 'ThrowsExceptionXXX' is not caught. thanks,
9
6350
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you...
20
7064
by: talin at acm dot org | last post by:
Although I realize the perils of even suggesting polluting the Python namespace with a new keyword, I often think that it would be useful to consider defining an operator for testing whether or not an item is a member of a category. Currently, we have the 'in' operator, which tests for membership within a container, and that works very well...
5
16596
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining extern storage class: /****************************************************************** SOURCE FILE ONE...
5
347
by: Sam | last post by:
Hi everyone Could anyone help me understand the usage of the "New" keyword I'm new to VB.Net. 1. Why do we use the "New" keyword on some object type variables such as the myPen of the example below and not with the bgColor. Both the Pen and Color are objects Dim myPen As Pen = New Pen(Color.AquaMarine)
3
1993
by: flat_ross | last post by:
For anyone who is just getting into VB.NET and/or is starting to work with inheritance I would like to point out a potential pitfall. We found this confusion recently when code-reviewing an application. If you have not used the keyword 'Overridable' then read on for sure... If you setup a child class and you want to override a method in your...
4
3084
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax) { T retVal = val; if ( retVal < vmin ) retVal = vmin;
9
5412
by: newcomsas | last post by:
Hello. I'm working on a problem related with CSS and javascript: I have got a link on a page and a stylesheet file that makes the background color change when users click on it. Is there anyone who knows if it is possible to 'force' via javascript a status A:active on a link ? I mean: is there a property of the style that can make the...
1
2772
by: alamodgal | last post by:
hiiiiiii I have a problem in highlighting searching keyword.Actually im using this function for searching Public Function HighLight(ByVal Keyword As String, ByVal ContentFor As String) Dim objHighLight As New highlight(Keyword, "<span class='searchKeyword'>{keyword}</span>") ContentFor = objHighLight.process(ContentFor, False,...
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.