473,503 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why this MACRO does not work?

I defined a MACRO 'removebrace' as following

#define removebrace(x) x

main() {

...

MyFunc(1, 2, removebrace("hello", 5, 6));
....

}

I expect this statement to expand in

MyFunc(1, 2, "hello", 5, 6);

I use MSVC. I can see in disassembly that only three values are pushed
into the stack before calling function 'MyFunc', that is pointer to
string "hello", 2 and 1, instead of all five arguments.

However, if I do not use MACRO 'removebrace', all five values are
pushed into the stack and it works as expected.

I can't understand why using MACRO 'removebrace' creates problem.

Thanks for any help.

Karim
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #1
7 2341
Karim Thapa wrote:
I defined a MACRO 'removebrace' as following

#define removebrace(x) x

main() {

..

MyFunc(1, 2, removebrace("hello", 5, 6));
According to the Unicode Consortium code charts, '(' and ')' are
called, respectively, left (or opening) and right (or closing)
parenthesis.

{' and '}' are called braces or curly brackets.
I expect this statement to expand in

MyFunc(1, 2, "hello", 5, 6);

I use MSVC.


My compiler says:

test.c:5: macro `removebrace' used with too many (3) args

Nov 14 '05 #2

"Karim Thapa" <ka********@yahoo.com> wrote in message
news:cl****************@plethora.net...
I defined a MACRO 'removebrace' as following

#define removebrace(x) x

main() {

..

MyFunc(1, 2, removebrace("hello", 5, 6));


As was previously mentioned, your macro expects only one argument,
where as you passed three! You can think of this in the lines of
a function's signature. You pass only that much of arguments as
declared by the prototype.

Last time I wrongly gave a solution to your problem. This time,
again, I propose a solution. Define your macro as:

#include <stdio.h>
#include <stdlib.h>
/*
* "info gcc" pages mention that C99 allows variable
* length macros
*/

#ifdef __STDC__
#define removebrace( x, ... ) ( x ), __VA_ARGS__
#else
#define removebrace( x, y, z ) ( x ), ( y ), ( z ) /* useless */
#endif

int
main ( void )
{
MyFunc(1, 2, removebrace("hello", 5, 6));
return EXIT_SUCCESS;
}

Compile as

$ gcc a.c -std=c99

[..]
--
Vijay Kumar R Zanvar

Nov 14 '05 #3
"Karim Thapa" <ka********@yahoo.com> wrote in message
news:cl****************@plethora.net...
I defined a MACRO 'removebrace' as following

#define removebrace(x) x

main() {

..

MyFunc(1, 2, removebrace("hello", 5, 6));


As was previously mentioned, your macro expects only one argument,
where as you passed three! You can think of this in the lines of
a function's signature. You pass only that much of arguments as
declared by the prototype.

Last time I wrongly gave a solution to your problem. This time,
again, I propose a solution. Define your macro as:

#include <stdio.h>
#include <stdlib.h>
/*
* "info gcc" pages mention that C99 allows variable
* length macros
*/

#ifdef __STDC__
#define removebrace( x, ... ) ( x ), __VA_ARGS__
#else
#define removebrace( x, y, z ) ( x ), ( y ), ( z ) /* useless */
#endif

int
main ( void )
{
MyFunc(1, 2, removebrace("hello", 5, 6));
return EXIT_SUCCESS;
}

Compile as

$ gcc a.c -std=c99

[..]
--
Vijay Kumar R Zanvar
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #4
ka********@yahoo.com (Karim Thapa) wrote:
I defined a MACRO 'removebrace' as following

#define removebrace(x) x


You've already had about half a gross of answers to this question, all
of them indicating that this macro definition doesn't do anything much.
If you didn't like those answer, tough; re-posting the question won't
make the macro suddenly work.

Richard
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #5
In comp.lang.c.moderated Karim Thapa <ka********@yahoo.com> wrote:
I defined a MACRO 'removebrace' as following #define removebrace(x) x main() { .. MyFunc(1, 2, removebrace("hello", 5, 6));


Your compiler should've complained about this --- you defined a macro
with one argument, but you're calling it with *three*. This is a bug
in your code. Since parentheses are an integral part of the syntax
of a macro call, you cannot just remove them.

To see what exactly happens, get your compiler to show you a
preprocessed version of your source.

--
Hans-Bernhard Broeker (br*****@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #6
On 20 Jan 2004 07:33:23 GMT, ka********@yahoo.com (Karim Thapa) wrote:
I defined a MACRO 'removebrace' as following

#define removebrace(x) x

main() {

..

MyFunc(1, 2, removebrace("hello", 5, 6));
...

}

I expect this statement to expand in

MyFunc(1, 2, "hello", 5, 6);


Why? You've defined 'removebrace' to take one argument, yet you give
it three. I'm not sure what the preprocessor is supposed to do with
it, though I suspect it's undefined. It appears your compiler ignores
the second and third parameters.

Perhaps you meant

MyFunc(1, 2, removebrace(("hello", 5, 6)));

which at least has just one argument. But it expands to

MyFunc(1, 2, ("hello", 5, 6));

which is probably not what you want either (still just three
parameters, the third being 6 instead of "hello").

What will work is

#define removebrace(x,y,z) x,y,z

along with your original function call. But I'm guessing that's not
what you want, either.

Regards,

-=Dave
--
Change is inevitable, progress is not.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #7
ka********@yahoo.com (Karim Thapa) writes:
I defined a MACRO 'removebrace' as following

#define removebrace(x) x

main() { MyFunc(1, 2, removebrace("hello", 5, 6));
}

I expect this statement to expand in

MyFunc(1, 2, "hello", 5, 6);


I'd expect it to fail to compiler. Your removebrace() macro takes one
argument; you've given it three.

You can call it like this:

MyFunc(1, 2, removebrace(("hello", 5, 6)));

This invokes removebrace() with a single argument: ("hello", 5, 6).

(BTW, "main()" is better writen as "int main(void)").

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #8

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

Similar topics

25
3210
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...
5
1860
by: MLH | last post by:
I have problems getting the CTRL-d, CTRL-w and other 'hotkey combo' assignments to work in Access 97. I just imported the autoexec macro directly from Access 2.0, so its no surprise. Some are...
10
2395
by: Karim Thapa | last post by:
Why following macro does not work? #define removebrace(x) x void Foo(int a, int b, char *txt, int d, int e); main() {
44
3682
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
4
11662
by: ApexData | last post by:
Hello 1- What is the AutoExec Macro? Is it the same thing as AutoKeys Macro? 2- I'm looking to Control Keys equally on startup for my entire app. I understand that the AutoKeys Macro is the...
6
6424
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
20
21705
by: rkk | last post by:
Hi, Is there an equivalent typeof macro/method to determine the type of a variable in runtime & most importantly that works well with most known C compilers? gcc compiler supports typeof()...
9
321
by: Francois Grieu | last post by:
Consider this macro // check if x, assumed of type unsigned char, is in range #define ISVALID(x) ((x)>=0x20 && (x)<=0x7E) Of course, this can't be safely used as in if (ISVALID(*p++)) foo();...
6
2289
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in...
4
2100
by: Gestorm | last post by:
Hi all, I found a macro "USE_VAR" in the code of bash-3.2 as follows: /*file: bash-3.2/shell.c*/ 344 USE_VAR(argc); 345 USE_VAR(argv); 346 USE_VAR(env); 347 USE_VAR(code); 348 ...
0
7287
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,...
0
7348
jinu1996
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...
0
5592
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,...
1
5021
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...
0
4685
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...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
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 ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
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...

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.