For some reason I am led to believe that this actually refers to -128 as an integer but why not just write -128?
-
4[*citation needed*] :) – Jongware Feb 01 '17 at 18:15
-
3I don't see it in the [c standard](http://port70.net/~nsz/c/c11/n1570.html) – Eugene Sh. Feb 01 '17 at 18:18
-
2Can you give some context as to where you've seen this, i.e. a compileable piece of code? You'll get a better answer that way. – dbush Feb 01 '17 at 18:20
-
1It would be relevant if the `int` size is 8 bits. With my 32-bit `int` the library has `#define INT_MIN (-2147483647 - 1)` because `2147483648` is outside `int` range, so `-2147483648` does not work. – Weather Vane Feb 01 '17 at 18:31
-
Related: http://stackoverflow.com/questions/34182672/why-is-0-0x80000000 – dbush Feb 01 '17 at 18:32
-
@WeatherVane: however, the standard guarantees that `int` is at least 16 bit. – Matteo Italia Feb 01 '17 at 18:43
2 Answers
-127-1U
is a constant expression.
127
is an integer constant. It is of type int
and has the obvious value. The unary -
operator is applied to it, yielding the value -127
, also of type int
.
1U
is also an integer constant. The U
suffix causes it to be of type unsigned int
.
The second -
is a subtraction operator, which takes two operands. Under the rules for resolving binary (two-operand) operators with different types, the int
operand is implicitly converted to unsigned int
. Converting -127
from int
to unsigned int
(assuming a typical 32-bit 2's complement representation for int
) yields 4294967169
of type unsigned int
(more generally it's UINT_MAX - 126U
). Subtracting 1U
from that yields 4294967168
, again of type unsigned int
.
I have no idea what the purpose of this expression might be. I'd have to see more context to speculate. It would be clearer to write UINT_MAX - 127U
.

- 254,901
- 44
- 429
- 631
They may be trying to determine the size of bits used to hold the data on that particular machine or compiler. Using an 8 bit sign calculation, the calculation will under flow giving 128 or not underflow giving -128.

- 1,247
- 13
- 18
-
1
-
3
-
There is no "U" integer type. A `U` suffix on an integer constant causes it to be of type `unsigned int` (or of some wider unsigned integer type if the value exceeds `UINT_MAX`). `1` is of type `int`; `1U` is of type `unsigned int`. – Keith Thompson Feb 01 '17 at 18:21
-
@mch Yes, but then it won't "underflow", but first the `-127` will be converted to `unsigned int` by adding `UINT_MAX+1` and then `1u` subtracted. – Eugene Sh. Feb 01 '17 at 18:22
-
So basically -128 would have caused an underflow, which can be overridden by using -127-1U? – Team. Coco Feb 01 '17 at 18:24
-
@Team.Coco It is pretty much the same as `(unsigned int)(-128)` (unless `-128` is outside of the range of `int`...) – Eugene Sh. Feb 01 '17 at 18:26
-
-
@KeithThompson Can't it be of 8-bit size with sign-and-magnitude representation? Then it would be [-127, +127] range. – Eugene Sh. Feb 01 '17 at 18:32
-
@EugeneSh.: No. The range of `int` must be at least `-32767` .. `+32767`. See [N1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) 5.2.4.2.1, `
`. ((I think some compilers for very small embedded systems use 8-bit `int`, but they're non-conforming.) – Keith Thompson Feb 01 '17 at 18:38 -
@KeithThompson Hm Wasn't aware of the "*Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.*" statement. Thanks. – Eugene Sh. Feb 01 '17 at 18:40
-
@Keith Thompson: Sure in 2011, I would expect that of any C complier or system. I was writing C code back on 8 bit machines. I would bet the rule didn't apply then. I would also bet that there is plenty of C code left over from when a 2011 specification was not valid. – Mike Wodarczyk Feb 01 '17 at 18:42
-
@MikeWodarczyk But your answer is incorrect even if everything is 8-bit. In that case the expression will result in `128`. – Eugene Sh. Feb 01 '17 at 18:48
-
@MikeWodarczyk: That requirement has been in place at least since 1989. K&R1, published in 1978, doesn't say that int is at least 16 bits, but the examples it shows are all at least that big. If you were using a compiler with 8-bit int after 1989, then it was non-conforming. (That's not necessarily a bad thing. A non-conforming compiler can be perfectly useful. Just think of it as a compiler for a non-C language that's very similar to C.) – Keith Thompson Feb 01 '17 at 18:54
-
@Eugene Sh: You are right, it gives 128. It doesn't change the basic idea that using a calculation will give a different result depending on the number of bits. – Mike Wodarczyk Feb 01 '17 at 18:58