Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid suffix "-252" on integer constant

Tags:

c

integer

I've been trying to run a C file from https://github.com/amree/mykad-c/blob/master/scdump.c, but I keep getting Invalid suffix "-252" on integer constant at this part of the code:

        if (RxBuffer[0x11E-252] == 'P')
            printf("Female\n");
        else if (RxBuffer[0x11E-252] == 'L')
            printf("Male\n");
        else
            printf("%c\n", RxBuffer[0x11E-252]);

I'm pretty sure it's syntax related. But I don't really understand code (I'm trying to, but I'm pretty much stuck). Can anyone help me?

like image 268
Amree Avatar asked Aug 15 '12 21:08

Amree


1 Answers

While this problem is related in a way to hex floating point constants, the root cause of the problem isn't because 0x11E is the start of a hex float constant (because it's not the start of a hex floating constant).

The problem is that 0x11E-252 is a single token instead of three tokens like 0x11F-252 is. If you look at C99 6.4.8 "Preprocessing numbers", you'll see that a pp-number token is:

A preprocessing number begins with a digit optionally preceded by a period (.)and may be followed by valid identifier characters and the character sequences e+, e-, E+, E-, p+, p-, P+, or P-

So, 0x11E-252 is a single token and tokenizing occurs in translation phase 3, but when it comes time to interpret the token syntactically and semantically (which happens in translation phase 7), it's not syntactically valid so you get a compiler error.

On the other hand, 0x11F-252 is three tokens because the - is not part of a preprocessing number token unless it's immediately preceded by a P or E (upper or lowercase).

Of course, this is related to float constants (hexadecimal or otherwise) because that's why the - character can end up in the middle of a preprocessing number token. However, note that you will get a similar error messages for tokens like 0xx11F or 22bad_token which have no resemblance to a hex floating point constant.

The fix is as ouah noted, insert whitespace before the - to force the compiler to treat the sequence as more than one token. If this were your code, an even better solution might be to give names to all those magic numbers (enums or macros). An identifier followed by - won't be considered a single token. Plus you'd hopefully have the bonus of making the code a little more self documenting.

like image 134
Michael Burr Avatar answered Nov 18 '22 11:11

Michael Burr