MACRO help

N

new

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.
 
S

spinoza1111

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.

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.
 
K

Keith Thompson

new said:
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.

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.
 
S

Squeamizh

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)

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 :).
 
S

spinoza1111

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, [email protected]://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.
 
S

spinoza1111

WARNING TO ORIGINAL POSTER, DON'T LOOK AT THE ANSWER BELOW UNTIL
YOU'VE TRIED # AND ## (THEY ARE BOTH NEEDED)

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 :).

#include <stdio.h>

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

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

Prints xx = 5.
 
S

spinoza1111

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.

This was incorrect.
 
B

Ben Bacarisse

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>
 
S

spinoza1111

(e-mail address removed) (Richard Harter) writes:




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

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

No, there must be at least one space between the x and the pound sign,
at least on my compiler.
 
S

spinoza1111

The ## is not needed.

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

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

spinoza1111

The ## is not needed.
("A"   "B") means the exact same thing as ("AB").

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





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
 
K

Keith Thompson

On Tue, 23 Mar 2010 19:51:07 -0700, Keith Thompson


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.
 
N

Nick Keighley

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.

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

Dr Malcolm McLean

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.
 
S

Seebs

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.

-s
 
K

Keith Thompson

Seebs said:
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.)
 
K

Keith Thompson

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.

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.
 
S

spinoza1111

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.
 
A

anvera

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top