Find the answer to your Linux question:
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, ...
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    20

    ANSI C Pre-processor - how to use constant suffix?

    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, 0xabcd);
    Optimistically I tried this:

    Code:
    #define CREATE_UINT(a,b) ((a << 16) | (b))U
    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?

    Thanks.

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    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.

  3. #3
    Linux Guru Rubberman's Avatar
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...