473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

why macro not expanded ?

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 ?

Nov 15 '05 #1
3 1360
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

Nov 15 '05 #2
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**********@yahoo.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 ?


Nov 15 '05 #3
becase of the bracket indicate the *func* as a function.

<ju**********@yahoo.co.in>
??????:11********************@g47g2000cwa.googlegr oups.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 ?

Nov 15 '05 #4

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

Similar topics

25
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...
699
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...
19
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. ...
7
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....
3
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...
6
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...
7
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. ...
2
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...
12
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.