473,699 Members | 3,180 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 3239
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
3137
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
6361
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 have idea why most programmers do this? Bryan Parkoff
20
7083
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 -- in particular, it allows such membership tests to be expressed in very natural way. So for...
5
16602
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 *******************************************************************/ extern int i; /* Reference to i, defined below */
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
2001
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 base class, you may see the squiggles under your child's method name. The pop-up/build error says...
4
3099
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
5445
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 css believe a link was clicked when in reality it was not ?
1
2779
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, False) Return ContentFor 'Dim RegExp As Regex = New Regex(Keyword.Replace(" ", "|").Trim(),...
0
8706
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8633
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8947
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8902
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7787
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5891
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3076
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
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.