7 ms·
What does the provided example do? 5) Surprising math: int x = 0xfffe+0x0001;
by freefal 6y ago
What does the provided example do?
5) Surprising math:
int x = 0xfffe+0x0001;
- moonchild 6y agoIt looks a bit like a binary exponent, but according to clang and gcc it's erroneous: 'invalid suffix '+0x0001' on integer constant'.
- monocasa 6y agoIt doesn't compile. It's trying to parse the e as scientific notation I think. surprising_math.c: In function ‘main’: surprising_math.c:4:10: error: invalid suffix "+0x0001" on integer constant int x = 0xfffe+0x0001; ^~~~~~~~~~~~~
- inetknght 6y agoIt produces an error message. error: invalid suffix '+0x0001' on integer constant [0] https://gcc.godbolt.org/z/pR5L6v https://gcc.godbolt.org/z/pR5L6v
- rightbyte 6y agoThat is really stupid. How is that not a bug? I can't belive GCC wont compile it. What does other compilers do. Edit: Clang also gives an error. Mscv seems to compile. I wonder who follows spec. I assume not mscv ...
- _kst_ 6y agoIt's not a bug. 0xfffe+0x0001 satisfies the syntax of a "preprocessing number". In a later translation phase, preprocessing numbers are converted to numeric constants. Not all valid preprocessing numbers are valid numeric constants. The syntax of preprocessing numbers is deliberately permissive to avoid complicating the implementation of the preprocessor. I'm sure it could have been made stricter, allowing 0xfffe+0x0001 to be treated as 0xfffe + 0x0001 -- but the solution is simply to write 0xfffe + 0x0001 in the first place. The language grammar has to be consistently defined even if it leads to surprises now and then.
- deleted 6y ago[deleted]