Discussion:
How to get calculation eveluated at preprocessor
(too old to reply)
Angela Yan
2010-03-12 06:14:51 UTC
Permalink
Hi everyone,

I got a following issue.

#define M_NUM 3
#define ABC (M_NUM * 10 * 10)

#define ABCCC_(x) #x
#define HALF_STRING(a) "Result is " ABCCC_(a)

#define RESULT_STRING HALF_STRING(ABC)

The output of the RESULT_STRING will be "Result is (3 * 10 * 10)" instead of
"Result is 300".

Anyone has any idea how to make the output to "Result is 300"??

Any help will be greatly appreciated.

ps. The value of M_NUM is generated during build time, so I can't calculated
the value of ABC first and directly insert it into the string..

Thank you very much.

Cheers,
Angela
Igor Tandetnik
2010-03-12 13:26:21 UTC
Permalink
Post by Angela Yan
I got a following issue.
#define M_NUM 3
#define ABC (M_NUM * 10 * 10)
#define ABCCC_(x) #x
#define HALF_STRING(a) "Result is " ABCCC_(a)
#define RESULT_STRING HALF_STRING(ABC)
The output of the RESULT_STRING will be "Result is (3 * 10 * 10)" instead of
"Result is 300".
Anyone has any idea how to make the output to "Result is 300"??
#define XTIMES100(x) x##00
#define TIMES100(x) XTIMES100(x)
#define ABC TIMES100(M_NUM)

Preprocessor doesn't do math (well, it does, but only in #if directives). It's a text manipulation engine.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Tim Roberts
2010-03-15 01:56:34 UTC
Permalink
Post by Angela Yan
I got a following issue.
#define M_NUM 3
#define ABC (M_NUM * 10 * 10)
#define ABCCC_(x) #x
#define HALF_STRING(a) "Result is " ABCCC_(a)
#define RESULT_STRING HALF_STRING(ABC)
The output of the RESULT_STRING will be "Result is (3 * 10 * 10)" instead of
"Result is 300".
Anyone has any idea how to make the output to "Result is 300"??
Any help will be greatly appreciated.
ps. The value of M_NUM is generated during build time, so I can't calculated
the value of ABC first and directly insert it into the string..
Unless you really do just need to add two zeros, the only solution to this
is to use sprintf.

CString sz;
sz.Format( "Result is %d", ABC );
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Angela Yan
2010-03-15 03:43:57 UTC
Permalink
Thanks Igor and Tim..

hmm.. Actually I need
#define ABC (M_NUM / 3600 / 24)

And the resulted string is used in a RC file to show the "Special build
description":

VALUE "SpecialBuild", RESULT_STRING

So I guess.. does it mean there is no way to do it??

Thank you very much.

Angela
Post by Tim Roberts
Post by Angela Yan
I got a following issue.
#define M_NUM 3
#define ABC (M_NUM * 10 * 10)
#define ABCCC_(x) #x
#define HALF_STRING(a) "Result is " ABCCC_(a)
#define RESULT_STRING HALF_STRING(ABC)
The output of the RESULT_STRING will be "Result is (3 * 10 * 10)" instead of
"Result is 300".
Anyone has any idea how to make the output to "Result is 300"??
Any help will be greatly appreciated.
ps. The value of M_NUM is generated during build time, so I can't calculated
the value of ABC first and directly insert it into the string..
Unless you really do just need to add two zeros, the only solution to this
is to use sprintf.
CString sz;
sz.Format( "Result is %d", ABC );
--
Providenza & Boekelheide, Inc.
Igor Tandetnik
2010-03-15 04:46:06 UTC
Permalink
Post by Angela Yan
Thanks Igor and Tim..
hmm.. Actually I need
#define ABC (M_NUM / 3600 / 24)
And the resulted string is used in a RC file to show the "Special build
VALUE "SpecialBuild", RESULT_STRING
So I guess.. does it mean there is no way to do it??
Not with the preprocessor, no. I suppose the same build process that generated M_NUM could also be made to generate ABC.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Angela Yan
2010-03-15 06:24:22 UTC
Permalink
hmm.. I don't have the source code of the build process which generates
M_NUM..
hmm.. maybe I write my own process to do the calculation and add the string
during build time..
or.. maybe I can live with the fact that just show the M_NUM on the file and
let people to do the calculation themselves.. haa...

Thank you for both Igor and Tim..

Have a nice day~~

Angela
Post by Angela Yan
Thanks Igor and Tim..
hmm.. Actually I need
#define ABC (M_NUM / 3600 / 24)
And the resulted string is used in a RC file to show the "Special build
VALUE "SpecialBuild", RESULT_STRING
So I guess.. does it mean there is no way to do it??
Not with the preprocessor, no. I suppose the same build process that
generated M_NUM could also be made to generate ABC.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily
a good idea. It is hard to be sure where they are going to land, and it
could be dangerous sitting under them as they fly overhead. -- RFC 1925
Loading...