#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
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)
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.
Among the numerous typos I made in that post, this one is serious. That
lastline should be:
#pragma message(STR(NUMBER)) //prints 10
It's ok I worked it out. Thanks.