Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MACRO help

4 views
Skip to first unread message

new

unread,
Mar 23, 2010, 10:36:36 PM3/23/10
to
Hi Experts,
Need your help in writing a macro.

1.c
-----------------------------
#include<stdio.h>

#define PRINT(x) printf("x = %d\n",x)

int main()
{
int i =9, j=2;
PRINT(i);
PRINT(j);
return 0;
}
-------------------------------
The output will be :
x = 9
x = 2

But what if I want to get the output as below:
i = 9
j = 2

How should be the macro declaration?? Is it possible to get the
desired output using macro?

Thanks much for your help.

spinoza1111

unread,
Mar 23, 2010, 10:46:05 PM3/23/10
to

The name of the variable cannot be accessed. You need:

#define PRINT(x,name) printf("%s = %d\n",(n),(x))

(You should as a stylistic point place the "formal parameters" in
parentheses when they are used in the "macro body" to avoid unexpected
parsing)

PRINT(i, "i")

Ai yi, I know. Life sucks.

A full macro processor does know these names and could plug them into
you, but C does not have one. The existing macro processor is hard
enough for most programmers to use.

Keith Thompson

unread,
Mar 23, 2010, 10:51:07 PM3/23/10
to

We get a lot of people here asking for us to do their homework for
them. (One of the standard answers is to ask for the instructor's
e-mail address, so we can submit our solutions directly.)

Your question looks like it could be a homework assignment, so I'm
hesitant to give you the solution, but I will give you a strong hint:
use the "#" operator. It's not an ordinary operator; it's specific
to the preprocessor. Your textbook should tell you about it.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Richard Harter

unread,
Mar 24, 2010, 12:29:45 AM3/24/10
to

Then again, it might not be homework, and Ghu knows what text if
any OP is using. Either answer the question or say nothing;
don't be cute.

To OP. What Keith is getting at is that you want

#define PRINT(x) printf("#x = %d\n",x)


Richard Harter, c...@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
It's not much to ask of the universe that it be fair;
it's not much to ask but it just doesn't happen.

Squeamizh

unread,
Mar 24, 2010, 12:46:24 AM3/24/10
to
On Mar 23, 9:29 pm, c...@tiac.net (Richard Harter) wrote:
> On Tue, 23 Mar 2010 19:51:07 -0700, Keith Thompson
>
>
>
>
>
> <ks...@mib.org> wrote:

That should be:

#define PRINT(x) printf(#x" = %d\n",x)

I'm not sure if you made an honest mistake, or if you were being
intentionally misleading because you don't want to do homework for
somebody. In this case, I don't see how being shown an example of the
stringizing operator could short-circuit his education.

If it was an honest mistake then ignore the above :).

spinoza1111

unread,
Mar 24, 2010, 8:23:22 AM3/24/10
to
On Mar 24, 12:29 pm, c...@tiac.net (Richard Harter) wrote:
> On Tue, 23 Mar 2010 19:51:07 -0700, Keith Thompson
>
>
>
>
>
> <ks...@mib.org> wrote:
> Richard Harter, c...@tiac.nethttp://home.tiac.net/~cri,http://www.varinoma.com

> It's not much to ask of the universe that it be fair;
> it's not much to ask but it just doesn't happen.

Forgot about that feature, but it may be unavailable in Microsoft C++
in C mode: this code:

#include <stdio.h>

#define PRINT(x) { printf("#x = %d\n",x); }

int main()
{
int zz = 5;
PRINT(zz)
}

prints

x = 5

and not

zz = 5

Am I missing something? This would certainly be a useful feature, and
it seems to be in the Standard.

spinoza1111

unread,
Mar 24, 2010, 8:44:11 AM3/24/10
to
WARNING TO ORIGINAL POSTER, DON'T LOOK AT THE ANSWER BELOW UNTIL
YOU'VE TRIED # AND ## (THEY ARE BOTH NEEDED)

#include <stdio.h>

#define PRINT(x) { printf(#x ## " = %d\n", x); }

int main()
{
int zz = 5;
PRINT(zz)
}

Prints xx = 5.

spinoza1111

unread,
Mar 24, 2010, 8:46:57 AM3/24/10
to

This was incorrect.

Ben Bacarisse

unread,
Mar 24, 2010, 8:50:47 AM3/24/10
to
c...@tiac.net (Richard Harter) writes:
<snip>

> To OP. What Keith is getting at is that you want
>
> #define PRINT(x) printf("#x = %d\n",x)

... and what Richard is getting at is that you want:

#define PRINT(x) printf(#x" = %d\n",x)

<snip>
--
Ben.

pete

unread,
Mar 24, 2010, 9:09:25 AM3/24/10
to
spinoza1111 wrote:
> WARNING TO ORIGINAL POSTER, DON'T LOOK AT THE ANSWER BELOW UNTIL
> YOU'VE TRIED # AND ## (THEY ARE BOTH NEEDED)

> #define PRINT(x) { printf(#x ## " = %d\n", x); }

The ## is not needed.

("A" "B") means the exact same thing as ("AB").

--
pete

spinoza1111

unread,
Mar 24, 2010, 10:04:27 AM3/24/10
to

No, there must be at least one space between the x and the pound sign,
at least on my compiler.
>
> <snip>
> --
> Ben.

spinoza1111

unread,
Mar 24, 2010, 10:08:02 AM3/24/10
to

Oh, yes, so they do. Is that behavior restricted to the first operand
of printf and related functions?
>
> --
> pete

spinoza1111

unread,
Mar 24, 2010, 10:13:05 AM3/24/10
to

No, I was wrong and you were right: this code:

#include <stdio.h>

#define PRINT(x) printf(#x" = %d\n",x)
int main()
{
int zz = 5;
PRINT(zz);
printf("a""b");
}

correctly prints zz = 5 then ab

Richard Harter

unread,
Mar 24, 2010, 10:25:38 AM3/24/10
to
On Tue, 23 Mar 2010 21:46:24 -0700 (PDT), Squeamizh
<squ...@hotmail.com> wrote:

>On Mar 23, 9:29=A0pm, c...@tiac.net (Richard Harter) wrote:
>> On Tue, 23 Mar 2010 19:51:07 -0700, Keith Thompson

[snip]

>> To OP. =A0What Keith is getting at is that you want
>>
>> #define PRINT(x) printf("#x =3D %d\n",x)
>
>That should be:
>
>#define PRINT(x) printf(#x" =3D %d\n",x)


>
>I'm not sure if you made an honest mistake, or if you were being
>intentionally misleading because you don't want to do homework for
>somebody. In this case, I don't see how being shown an example of the
>stringizing operator could short-circuit his education.
>
>If it was an honest mistake then ignore the above :).

It was slop, sir, sheer slop.

Keith Thompson

unread,
Mar 24, 2010, 11:26:36 AM3/24/10
to
c...@tiac.net (Richard Harter) writes:
> On Tue, 23 Mar 2010 19:51:07 -0700, Keith Thompson
> <ks...@mib.org> wrote:
[...]

>>We get a lot of people here asking for us to do their homework for
>>them. (One of the standard answers is to ask for the instructor's
>>e-mail address, so we can submit our solutions directly.)
>>
>>Your question looks like it could be a homework assignment, so I'm
>>hesitant to give you the solution, but I will give you a strong hint:
>>use the "#" operator. It's not an ordinary operator; it's specific
>>to the preprocessor. Your textbook should tell you about it.
>
> Then again, it might not be homework, and Ghu knows what text if
> any OP is using. Either answer the question or say nothing;
> don't be cute.
>
> To OP. What Keith is getting at is that you want
>
> #define PRINT(x) printf("#x = %d\n",x)

So you just assume that it *isn't* homework and give the OP the
answer. (And you got it wrong, but I'm not criticizing you for that;
it happens to all of us.)

I wasn't being cute, I was trying to be as helpful as possible.
If the OP had come back and said that it wasn't homework, I probably
would have posted the answer if someone else didn't beat me to it.

You can explain why there was something wrong with the way I
answered, or I'll continue to respond the same way to similar
questions. I might well do so anyway, but "don't be cute" is almost
guaranteed not to persuade me.

Nick Keighley

unread,
Mar 24, 2010, 11:53:32 AM3/24/10
to

seemed a pretty reasonable answer to me as well. If he thinks that's
cute then I'm cute too.

Dr Malcolm McLean

unread,
Mar 24, 2010, 12:03:41 PM3/24/10
to
On 24 Mar, 04:46, Squeamizh <sque...@hotmail.com> wrote:
> On Mar 23, 9:29 pm, c...@tiac.net (Richard Harter) wrote:
>
> > To OP.  What Keith is getting at is that you want
>
> > #define PRINT(x) printf("#x = %d\n",x)
>
> That should be:
>
> #define PRINT(x) printf(#x" = %d\n",x)
>
PRINT(i % 2) ?

A better macro is

#define PRINT(x) printf("%s = %g\n", #x, (double) (x));

This has also got the nice feature that it will give a compile time
error if you try to use it on a pointer.

Eric Sosman

unread,
Mar 24, 2010, 2:06:44 PM3/24/10
to
On 3/24/2010 9:09 AM, pete wrote:
> spinoza1111 wrote:
>> WARNING TO ORIGINAL POSTER, DON'T LOOK AT THE ANSWER BELOW UNTIL
>> YOU'VE TRIED # AND ## (THEY ARE BOTH NEEDED)
>
>> #define PRINT(x) { printf(#x ## " = %d\n", x); }
>
> The ## is not needed.

But taking it out changes the meaning of the program: the
compiler is no longer required to issue a diagnostic message.

--
Eric Sosman
eso...@ieee-dot-org.invalid

Seebs

unread,
Mar 24, 2010, 2:05:37 PM3/24/10
to

And indeed, "A"##"B" is invalid, because "A""B" is not a valid token.

Nilges, as per usual, is not merely wrong, but aggressively seeking out
new levels of wrongness to which ordinary people can only aspire.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Keith Thompson

unread,
Mar 24, 2010, 2:25:14 PM3/24/10
to
Seebs <usenet...@seebs.net> writes:
> On 2010-03-24, pete <pfi...@mindspring.com> wrote:
>> spinoza1111 wrote:
>>> WARNING TO ORIGINAL POSTER, DON'T LOOK AT THE ANSWER BELOW UNTIL
>>> YOU'VE TRIED # AND ## (THEY ARE BOTH NEEDED)
>
>>> #define PRINT(x) { printf(#x ## " = %d\n", x); }
>
>> The ## is not needed.
>
>> ("A" "B") means the exact same thing as ("AB").
>
> And indeed, "A"##"B" is invalid, because "A""B" is not a valid token.
>
> Nilges, as per usual, is not merely wrong, but aggressively seeking out
> new levels of wrongness to which ordinary people can only aspire.

To be fair, he did later acknowledge his error.

(Yes, I've killfiled him; no, that doesn't a commitment never to
read anything he posts.)

Richard Harter

unread,
Mar 24, 2010, 2:37:37 PM3/24/10
to
On Wed, 24 Mar 2010 08:26:36 -0700, Keith Thompson
<ks...@mib.org> wrote:

The first thing that is wrong is that you assume by default that
it is a homework question. Then you go on to say that if the OP
establishes his bona fides to your satisfaction then you will
actually answer his question.

If the question was not a homework question, the original poster
might very well think "Who is this clown and why is he putting me
through an inquisition." Moreover, if you believe that it is
cheating to go online and ask for help, then your response says
in effect that, you suspect him of being a cheat and that you
will treat him as a cheat until he clears his name with you.

The besides of which it may well have not have been a homework
question - many people either never knew about that little gadget
or have forgotten about it.

The unfortunate thing is that you did the right thing by pointing
to the # gismo in the preprocessor. If you had just done that
and only that it would have been an excellent response. As it
was, your response was patronizing and a slur.

Keith Thompson

unread,
Mar 24, 2010, 3:14:37 PM3/24/10
to

If the OP thinks I'm a clown, he's free to say so. Isn't it a bit
patronizing to be offended on his behalf?

In any case, if you're re-read what I actually wrote, I did not
accuse him of cheating. Asking for help with homework is not
generally cheating (unless the assignment specifically say so, but
that's not my concern). Looking back, I see that I opened with


"We get a lot of people here asking for us to do their homework

for them"; I can see how that might sound like an accusation,
but it certainly wasn't meant as one.

On the other hand, I was unwilling to discount the possibility that
the OP *was* trying to cheat on his homework. I mean no offense;
there just wasn't enough information to be sure.

If I were asked this question by a co-worker, or by someone else
whose primary goal was to get something done rather than to learn
the lagnuage, I'd probably just provide a snippet of working code
(though I might just mention the "#" operator if I didn't have
enough time). If I were asked this question in the context of help
with homework, I'd be more vague, as I was in my initial response.
Without knowing the context of this question, I chose to be cautious.

> The besides of which it may well have not have been a homework
> question - many people either never knew about that little gadget
> or have forgotten about it.
>
> The unfortunate thing is that you did the right thing by pointing
> to the # gismo in the preprocessor. If you had just done that
> and only that it would have been an excellent response. As it
> was, your response was patronizing and a slur.

So you don't object to my providing a reference to the "#" operator
without a full explanation.

You object to the fact that, in an attempt to be polite, I *explained*
to the OP why I wasn't providing the complete answer.

I'm sure that's not what you had in mind, but I think your objection
is based on a misunderstanding of what I actually wrote and meant.

spinoza1111

unread,
Mar 24, 2010, 8:46:26 PM3/24/10
to
On Mar 25, 2:05 am, Seebs <usenet-nos...@seebs.net> wrote:
> On 2010-03-24, pete <pfil...@mindspring.com> wrote:
>
> >spinoza1111wrote:

> >> WARNING TO ORIGINAL POSTER, DON'T LOOK AT THE ANSWER BELOW UNTIL
> >> YOU'VE TRIED # AND ## (THEY ARE BOTH NEEDED)
> >> #define PRINT(x) { printf(#x ## " = %d\n", x); }
> > The ## is not needed.
> > ("A"   "B") means the exact same thing as ("AB").
>
> And indeed, "A"##"B" is invalid, because "A""B" is not a valid token.
>
> Nilges, as per usual, is not merely wrong, but aggressively seeking out
> new levels of wrongness to which ordinary people can only aspire.

I suggest you stay out of conversations to which you did not
contribute, Dweebach. You have too much work to do on the pseudo.c
program in which you have presented, after two months work, switch
cases which fall through to errors for apparently valid cases.

>
> -s
> --
> Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

anvera

unread,
Mar 25, 2010, 9:04:04 AM3/25/10
to

You already had some specific help thanks to the other fellows. By the
way, the program expanding a macro is called the C preprocessor. There
is quite a nice manual on the gcc preprocessor. Here's the relevant
section for you, but I encourage you to bookmark the whole and be
aware of the existence this really nice doc:

http://gcc.gnu.org/onlinedocs/cpp/Stringification.html#Stringification

Cheers,
Antonio

Michael Tsang

unread,
Mar 28, 2010, 4:34:52 AM3/28/10
to
new wrote:

>
> 1.c
> -----------------------------
> #include<stdio.h>
>
> #define PRINT(x) printf("x = %d\n",x)
>
> int main()
> {
> int i =9, j=2;
> PRINT(i);
> PRINT(j);
> return 0;
> }
> -------------------------------
> The output will be :
> x = 9
> x = 2
>
> But what if I want to get the output as below:
> i = 9
> j = 2
>

#define PRINT(x) printf(#x " = %d\n", x)

spinoza1111

unread,
Mar 28, 2010, 8:16:04 AM3/28/10
to
On Mar 25, 2:25 am, Keith Thompson <ks...@mib.org> wrote:

> Seebs <usenet-nos...@seebs.net> writes:
> > On 2010-03-24, pete <pfil...@mindspring.com> wrote:
> >>spinoza1111wrote:
> >>> WARNING TO ORIGINAL POSTER, DON'T LOOK AT THE ANSWER BELOW UNTIL
> >>> YOU'VE TRIED # AND ## (THEY ARE BOTH NEEDED)
>
> >>> #define PRINT(x) { printf(#x ## " = %d\n", x); }
>
> >> The ## is not needed.
>
> >> ("A"   "B") means the exact same thing as ("AB").
>
> > And indeed, "A"##"B" is invalid, because "A""B" is not a valid token.
>
> > Nilges, as per usual, is not merely wrong, but aggressively seeking out
> > new levels of wrongness to which ordinary people can only aspire.
>
> To be fair, he did later acknowledge his error.

I wonder if you realize how beautiful your first three words are.

"To be fair".

I don't even need you to point out that I was the first to solve the
problem. Maybe I wasn't, and at this time I don't want to check.

But instead, you were enough of a scientist and a human being to
correct Dweebach and to take the "wrong" side.

The side of the "troll".

You were man enough at least for a moment to let truth in.

That is why I got into programming in the first place: managers didn't
make me cut my long hair and I could use foul language as we did back
then because my code worked, and the business majors' code failed. It
was quite a long time ago and at the time human beings of the working
class were actually allowed to use their critical reason because
companies needed software.

But now, they don't, and the result was that after the election of
Reagan, the usual crowd of back-stabbing authoritarian conformists
rushed in, and as a result you're actually sticking your neck out
saying that the Troll is right.

Keep up the good work.


>
> (Yes, I've killfiled him; no, that doesn't a commitment never to
> read anything he posts.)

What ev er.

Keith Thompson

unread,
Mar 28, 2010, 2:00:23 PM3/28/10
to

Michael:

It's been about 5 days since the question was posted, 4 days since
multiple correct answers similar to yours were posted, and 3 days
since Dr Malcolm McLean pointed out a flaw in everyone else's
solutions (if the argument uses a "%" operator, it can become a
format specifier in the expansion of #x and mess up the printf call).

I know that Usenet is an asynchronous medium and articles don't
necessarily arrive everywhere in the order that they were posted.
But it's a good idea to read an entire thread, especially one
that's a few days old, before posting a followup. (I haven't always
followed this advice myself.)

Peter Nilsson

unread,
Mar 28, 2010, 5:14:32 PM3/28/10
to
> Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> >   #define PRINT(x) printf(#x" = %d\n",x)

Observe what happens if you try PRINT(i%d).

I use macros like ...

#define dump_i(x) \
fprintf(stderr, "%s: %d\n", #x, (int) (x))

#define dump_lu(x) \
fprintf(stderr, "%s: %lu\n", #x, (unsigned long) (x))

spinoza1111 <spinoza1...@yahoo.com> wrote:
> No, there must be at least one space between the x and
> the pound sign, at least on my compiler.

Then use a conforming compiler instead.

--
Peter

0 new messages