0

I'm currently working on a project in C and I have an issue with my #defines. When I'm defining a negative number (for example, #define MIN_W_RANGE -2147483648) the compiler doesn't recognise the minus sign. I've attached a screenshot of the debug that shows my defines and the values that the compiler recognises.

I have also tried to put the number in brackets, but that also hasn't worked.

What am I doing wrong???

Thanks :)

enter image description here

  • Which compiler are you using? Have you tried printing it our instead of looking it under the debugger? – Miigon Aug 12 '21 at 05:52
  • 2
    The typical definition is `(-2147483647 - 1)` – user3386109 Aug 12 '21 at 06:03
  • Does this answer your question? [Why do we define INT\_MIN as -INT\_MAX - 1?](https://stackoverflow.com/questions/26003893/why-do-we-define-int-min-as-int-max-1) – phuclv Aug 12 '21 at 07:47
  • duplicates: [Why we write TMin32 as -2147483647-1?](https://stackoverflow.com/q/30194153/995714), [How do I define a constant equal to -2147483648?](https://stackoverflow.com/q/27612996/995714). See also [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/995714) – phuclv Aug 12 '21 at 07:49
  • So what does `printf("%d\n", (int)MIN_W_RANGE);` output? It should print `-2147483648` if `int` is 32-bit 2's complement. (The cast to `(int)` is needed because the type of `MIN_W_RANGE` as defined is likely to be `long` or `long long` depending on the system (which is why `INT_MIN` is typically defined as `(-INT_MAX - 1)` to keep its type as `int`).) – Ian Abbott Aug 12 '21 at 09:52
  • 3
    The screenshot seems to show the **debugger** thinks `MIN_W_RANGE` is a `long long` with value 217483648, but we cannot tell what the **compiler** thinks it is, so we cannot tell whether there is a bug in the compiler, a bug in the debugger, both of those, or something else in the source code you are not showing us. Edit the question to provide a [mre] in text form. – Eric Postpischil Aug 12 '21 at 10:24
  • 1
    Show the output of `printf("%s\n", _Generic((MIN_W_RANGE), int: "int", unsigned int: "unsigned int", long: "long", unsigned long: "unsigned long", long long: "long long", unsigned long long: "unsigned long long", default: "unknown")); printf("%lld\n", (long long) (MIN_W_RANGE));` when inserted as the first statements in `main`. – Eric Postpischil Aug 12 '21 at 10:25
  • 1
    By the way, we call `(` and `)` parentheses, not brackets. Apologies if folks elsewhere use the words differently, but generally `[` and `]` are brackets and `{` and `}` are braces. The terms do get muddled when mathematicians use them all as multiple levels of parentheses in complicated expressions, but when referring to specific instances, “parentheses” is preferred for `(` and `)`. – Eric Postpischil Aug 12 '21 at 10:28

2 Answers2

0

It's not clear what you mean with "the compiler doesn't recognise the minus sign".

Each integer constant in your code has a type just like declared variables. Also, C does not have "minus signs" as part of the integer constant, the - is the unary minus operator applied after the integer constant has been given a type.

2147483648 is too large to get the type int so the compiler will make it long or long long depending on system. Then it will apply unary - and you get -2147483648 of type long or long long. (Judging by your debugger output, it seems to be long long.)

If you need it to have type int then as someone mentioned (-2147483647 - 1) is a commonly used trick.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • This tells us why `-2147483648` would produce the value of −2147483648 as a `long` or `long long`. What does that have to do with the debugger saying it has a value of 2147483648? – Eric Postpischil Aug 12 '21 at 10:26
0

Debugger reporting MIN_W_RANGE = (long long) 2147483648 implies debugger has a bug. The long long part is OK, the value is not. MIN_W_RANGE = (long long) -2147483648 is expected.

OP reports: "the compiler doesn't recognise the minus sign." is not well supported. We are seeing the result of the debugger, not directly the compiler. It could be the compiler, I just find debuggers more buggy than compilers.

Seeing more of the code, as text, would help.


-2147483648 is a two step: negation of the constant 2147483648. 2147483648 is outside the int and long range here, so is type long long.

To form the int value of -2147483648, avoid constants outside the int range.
Use (-2147483647 - 1)


What am I doing wrong???

Using a broken debugger is my first suspect.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256