Consider the following piece of code:
$ cat a.c
extern int func(int);
#define func(i) (i+i)
int main(void)
{
(func)(10);
}
On preprocessing the above code,
$ cc -E a.c
# 1 "a.c"
extern int func(int);
main()
{
(func)(10);
}
-------------------------------------------------------------
func being a macro,(func)(10 ) should be expanded to (10+10)(10), but it
doesn't seem to. It is treated as a function.
Why is it so ? 3 1378
On Mon, 08 Aug 2005 02:21:21 -0700, junky_fellow wrote: Consider the following piece of code: $ cat a.c extern int func(int); #define func(i) (i+i)
For this to be safe (well safer anyway) you should enclose the arguments
in parentheses as in
#define func(i) ((i)+(i))
int main(void) { (func)(10); } } On preprocessing the above code, $ cc -E a.c # 1 "a.c" extern int func(int);
main() { (func)(10); } } ------------------------------------------------------------- func being a macro,(func)(10 ) should be expanded to (10+10)(10), but it doesn't seem to. It is treated as a function. Why is it so ?
A function like macro is only expanded when the ( directly follows the
function name (intervening white-space is allowed). In your example
(func)(10) there is a ) between the name and the ( so it is not expanded.
Using parentheses around the name like this is a recognised method of
forcing a function call rather than a macro expansion. Of course it only
works with function-like macros.
Lawrence
Here there is a problem in code. First of all we need to understand
what a macro is.
#define name replacment_text
The name can have argumants also.
In this code. func(x) will be replaced by (x+x). But, you have used
(func)and it will be treated as a function call. You have provided a
declaration (extern int func(int);) for that function. So, it's
assuming that (func) is a function.
If you are expecting a macro to be used then
(func(10)) (10); to be used in your code. This will result (10+10) (10)
after preprocessing and which is syntactically incorrect.
(func(10))*(10) ; will give you the expected result.
Regards,
Raju ju**********@ya hoo.co.in wrote: Consider the following piece of code: $ cat a.c extern int func(int); #define func(i) (i+i) int main(void) { (func)(10); }
On preprocessing the above code, $ cc -E a.c # 1 "a.c" extern int func(int);
main() { (func)(10); }
------------------------------------------------------------- func being a macro,(func)(10 ) should be expanded to (10+10)(10), but it doesn't seem to. It is treated as a function. Why is it so ?
becase of the bracket indicate the *func* as a function.
<ju**********@y ahoo.co.in>
??????:11****** **************@ g47g2000cwa.goo glegroups.com.. . Consider the following piece of code: $ cat a.c extern int func(int); #define func(i) (i+i) int main(void) { (func)(10); }
On preprocessing the above code, $ cc -E a.c # 1 "a.c" extern int func(int);
main() { (func)(10);
^^^^^^^
you can replace *(func)* with *func*, then u will get what you want. }
------------------------------------------------------------- func being a macro,(func)(10 ) should be expanded to (10+10)(10), but it doesn't seem to. It is treated as a function. Why is it so ? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Andrew Dalke |
last post by:
Here's a proposed Q&A for the FAQ based on a couple recent
threads. Appropriate comments appreciated
X.Y: Why doesn't Python have macros like in Lisp or Scheme?
Before answering that, a clarification on what 'macro' means.
A Lisp macro is a way of modifying code when that code is first
defined. It can rearrange the structure of the code, and add and
remove parts of it. Unlike C's #define macro language, Lisp
macros understand the...
|
by: mike420 |
last post by:
I think everyone who used Python will agree that its syntax is
the best thing going for it. It is very readable and easy
for everyone to learn. But, Python does not a have very good
macro capabilities, unfortunately. I'd like to know if it may
be possible to add a powerful macro system to Python, while
keeping its amazing syntax, and if it could be possible to
add Pythonistic syntax to Lisp or Scheme, while keeping all
of the...
|
by: qazmlp |
last post by:
I hope comp.lang.c will not find the following question as a
complete off-topic.
I would like to remove ie.comment out the 'cout' statements during
compilation(actually preprocessing) time.
The statements like this:
cout<<"something\n" ;
should be made as
|
by: Newbie_sw2003 |
last post by:
Where should I use them?
I am giving you my understandings. Please correct me if I am wrong:
MACRO:
e.g.:#define ref-name 99
The code is substituted by the MACRO ref-name. So no overhead.
Execution is faster. Where will it be stotred?(Is it in bss/stack/?)
FUNCTION:
|
by: J Krugman |
last post by:
I'm trying to read some source code that makes very heavy use of
macros and other pre-processor directives. To make matters worse,
macro definitions are stacked several layers deep (i.e. macros are
defined in terms of other macros, which are in turn defined in
terms of other macros, seemingly ad infinitum).
I tried reading the *.i files generated by the preprocessor (i.e.
by giving gcc the -E flag). To my surprise, the macros were not...
| |
by: Marcus Kwok |
last post by:
I am designing a GUI (my question is not about GUIs) and I have named my
variables using a regular naming scheme. However, in order to simplify
the code using these variables, I have created an array of non-owning
pointers to these variables. I am trying to write a macro to generate
these variable names for me, but I am not sure if what I want to do is
possible.
The code below demonstrates what I want to do (except it is generating...
|
by: reppisch |
last post by:
Hi Ng,
i am looking for a method of expanding a macro while the rest of the
code remains untouched.
I have some code which does macro voodo / ifdef's which i would like to
strip and simplify.
The Faq pointed me to scpp but i could not compile it. the lex.c
generated by flex 2.5.4 is broken.
|
by: leo.hou |
last post by:
Hi experts,
I am new to linux and all the type definitions are driving me mad. What
is the best way to check a type definition in linux? When I use man
page to check some function definition, I may come across new types.
How do you guys look up their definitions?
I tried to lookup ssize_t, what I do is trace (grep in glibc source
code) to 'typedef __ssize_t ssize_t;', then '__STD_TYPE __SSIZE_T_TYPE
__ssize_t;' then '#define...
|
by: swellfr |
last post by:
Hi
if have simple macro defined this way:
#define M_OPplus( classname ) typedef classname operator+(const
classname& rhs);
and a template class :
template<typename X, typename Y>
class Test1
{
|
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,...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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...
|
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...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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: 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...
| |