Results 1 to 3 of 3
Hi,
Suppose I have some code like this:
Code:
const unsigned int = 0x1234abcdU;
I want to achieve this with a macro. Something like:
Code:
const unsigned int = CREATE_UINT(0x1234, ...
- 11-18-2009 #1Just Joined!
- Join Date
- Jun 2009
- Posts
- 20
ANSI C Pre-processor - how to use constant suffix?
Hi,
Suppose I have some code like this:
I want to achieve this with a macro. Something like:Code:const unsigned int = 0x1234abcdU;
Optimistically I tried this:Code:const unsigned int = CREATE_UINT(0x1234, 0xabcd);
The problem is that the "U" won't compile because of the brackets in the macro definition. I've tried many combinations of adding/removing brackets and of putting the U elsewhere but I still can't define a valid macro. Any ideas for a solution?Code:#define CREATE_UINT(a,b) ((a << 16) | (b))U
Thanks.
- 11-18-2009 #2
Everything that macros generate must be valid in the same sense as everything you would type directly must be valid.
Your makro writes this text:
((0x1234 << 16) | (0xabcd))U
which is not legal syntax, because the postfix "U" must follow a number (the figures written as text, not calculated).
My suggestion is to either write an ordinary function that returns, for example, an unsigned long.
Or you do a cast inside the macro, but you may get warnings for losing data.
(unsigned long*) a <<16 ...Debian GNU/Linux -- You know you want it.
- 11-18-2009 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
As GNU-fan said, you cannot use the 'U' directive unless it immediately follows a literal value. You can do this, however:
Code:#define CREATE_UINT(a,b) ((unsigned int)((a << 16) | (b)))
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote