1

The code snippet below works as is but if I uncomment the first #define and comment the second the compiler complains about expecting a ')' at the assignment statement. Thought it might be wanting a cast but that did not help. Please point out my stupid oversight.

Thanks, jh

//#define SMI_READ  (0b10 << 10)
#define SMI_READ  (0x2 << 10)
    ...
    command |= SMI_READ;
5
  • 8
    0b10 is not part of standard C. Commented Jun 5, 2017 at 21:27
  • Why 0x2 << 10 vs 1 << 11 or 2 << 11? Commented Jun 5, 2017 at 21:29
  • @clcto Maybe there's a two-bit field at offset 10 in this hardware register. Commented Jun 5, 2017 at 21:32
  • #define SMI_READ (1u << 11) :: always force unsigned for bitmasks. (even in cases like this where it is not(yet) important) Commented Jun 5, 2017 at 22:14
  • I'd mention that C++ has added binary constants like 0b10, but the question is about C so I won't bring it up. I will, however, mention that gcc supports binary constants as an extension, and other C compilers probably do as well. I wouldn't be astonished if a future C standard added binary constants, but I'm not holding my breath. Commented Jun 5, 2017 at 22:19

2 Answers 2

3

In general, to answer a question like this we need to see the complete and unedited text of the error messages, and it also really helps if you provide a complete program that we can attempt to compile for ourselves. (It might seem to you that the error messages are useless, but often it's just that they only make sense if you know how to think like a compiler engineer.)

However, in this case, I can make a high-confidence guess, because the only difference between the two macros is that the one that doesn't work uses a binary number, 0b10, and the one that does work uses a hexadecimal number, 0x2. Binary numbers are not part of any version of the C standard, although they are a common extension. I therefore deduce that your compiler doesn't support them and is giving an unclear error message when it encounters them.

Sign up to request clarification or add additional context in comments.

3 Comments

Detail: C does not have hexadecimal literal, but string and compound literals, both of which can have their address taken. 0x2 is a C hexadecimal-constant. Its address can not be taken.
@chux I think calling them "hexadecimal constant" and "binary constant" will be confusing in a different way - constants have names, don't they? - so I've changed it to "binary number" and "hexadecimal number."
Is "hexadecimal number" more clear than the spec's "hexadecimal constant" . IDK. Constants in C include 123, 0x123, 'x', 2.0 & enumeration constants. The last has names.
2

From C standard (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)

6.4.4.1 Integer constants
...
octal-constant:
0
octal-constant octal-digit
...
hexadecimal-prefix: one of
0x 0X

No other prefixes are described, especially nothing which would cover 0b10.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.