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

Numbers and #pragma message?

465 views
Skip to first unread message

J anderson

unread,
Jul 23, 2003, 11:18:03 AM7/23/03
to
It's possible to output a message on compilations like:

#pragma message("Message")

Is is possible to convert a defined number to a string and print it using
something like pragma message?

ie -something like:

#define NUMBERS 10 + 5
...
#pragma message(NUMBERS) //prints 15


James Curran/MVP

unread,
Jul 23, 2003, 2:25:47 PM7/23/03
to
J anderson wrote:
> Is is possible to convert a defined number to a string and print it
> using something like pragma message?
>
> ie -something like:
>
> #define NUMBERS 10 + 5
> #pragma message(NUMBERS) //prints 15

Now, there are two parts to that question. 1) Printing a number, and 2)
printing a computer expression.

1) is easy, but it involves a trick.
#define NUMBER 10
#pragma message(NUMBER) // fails -- number encounter when a string
was expected.

To handle that, we must convert the number to a string. One would think
using the stringizing operator (#) would do it, but do to the arcane rules
of the preprocessor, we must force it to be evaluate twice:
#define NUMBER 10
#define STR1(x) #x
#define STR(x) STR1(x)
#pragma message(NUMBER) //prints 10

Which brings us to part 2). Here's we seem to be stick. If you change the
line to
#define NUMBER 10 + 5
It's just going to print "10 + 5" and not "15".

--
Truth,
James Curran
www.noveltheory.com (personal)
www.njtheater.com (professional)


J anderson

unread,
Jul 24, 2003, 3:02:23 AM7/24/03
to

"James Curran/MVP" <james...@mvps.org> wrote in message
news:OKIuXfUU...@tk2msftngp13.phx.gbl...

Thanks alot, "10 + 5", will probably be ok, users can do the maths in their
head :) thanks alot. Still, it would be a nice addition to the language.

James Curran/MVP

unread,
Jul 24, 2003, 2:38:38 PM7/24/03
to
>> #define STR1(x) #x
>> #define STR(x) STR1(x)
>> #pragma message(NUMBER) //prints 10

Among the numerous typos I made in that post, this one is serious. That
lastline should be:
#pragma message(STR(NUMBER)) //prints 10

J anderson

unread,
Jul 25, 2003, 5:39:21 AM7/25/03
to

"James Curran/MVP" <james...@mvps.org> wrote in message
news:uqdWPLhU...@TK2MSFTNGP10.phx.gbl...

It's ok I worked it out. Thanks.


0 new messages