MACRO help

M

Michael Tsang

new said:
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)
 
S

spinoza1111

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

Keith Thompson

Michael Tsang said:
#define PRINT(x) printf(#x " = %d\n", x)

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

Peter Nilsson

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

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,179
Latest member
pkhumanis73
Top