0

I know that c works in two's complement but still i can't undrstand how the program below gives me 2147483647 as output.

#include<stdio.h>
int main(){

int a=-2147483648;
a-=1;
printf("%d",a);

}
amirm
  • 54
  • 4
  • I think that is because your using and printing an integer (which is an unsigned data type) instead of signed int. – Sayed Hussainullah Sadat Nov 23 '21 at 09:04
  • 3
    It's funny that a [search query](https://stackoverflow.com/search?q=2147483647) for this *specific* number on Stack Overflow gives more than 14.000 results and yet you could not find any helpful answer. – wovano Nov 23 '21 at 09:05
  • Does this answer your question? [why does long long 2147483647 + 1 = -2147483648?](https://stackoverflow.com/questions/61624859/why-does-long-long-2147483647-1-2147483648) – wovano Nov 23 '21 at 09:07

2 Answers2

1

Becuase an int can't contains the value -2147483648 - 1, so it result in an integer overflow, which leads to undefined behaviour. Undefined behaviour can result in anything.

  • Undefined behavior cannot result in anything. When the behavior is not defined by the C standard, then any behavior conforms to the C standard. But the behavior is still limited by other constraints, including logic, physics, law, and features imposed by manufacturing, software architecture, and more. For example, integer overflow will not cause a general market computer to catch on fire. Of particular relevance is the fact that compiler design and processor architecture have huge influences on program behavior even when the C standard does not define the behavior… – Eric Postpischil Nov 23 '21 at 14:16
  • … Knowing about those influences is of great help in diagnosing bugs in programs from observations of their behavior. When there is a program, programs misbehave in largely predictable ways, due to the nature of compiler construction and processor design. Understanding that helps debug programs. So it is a disservice to tell people “Undefined behavior can result in anything.” – Eric Postpischil Nov 23 '21 at 14:17
0

minimum number in twos comlement is 100000...0 if you reduce one from that it means computer will add -1 to it -1 in twos complement is: 11111...1 so if you add them the answer is 0111111...1 which is maximum integer or 2147483647

  • 2
    This is true when speaking of pure mathematics or assembler instructions, but not when speaking of C, which has no predictable behavior here. So this answer is incorrect in the context of C. – Lundin Nov 23 '21 at 09:16
  • 1
    @Lundin: This answer is not incorrect “in the context of C” or otherwise. The question asks how the program gives 2147483647 as output, not whether that is defined by the C standard. The fact that the two’s complement representations wrap is fundamentally the reason the program behaved the way it did, so this answer does answer the question that was asked. It would be good to also mention this behavior is not defined by the C standard and may fail in other circumstances, such as when optimization is enabled, but nonetheless the answer is correct. – Eric Postpischil Nov 23 '21 at 13:45
  • @EricPostpischil Silently pretending that the code is fine and well-defined when it is not isn't doing anyone any favours. That's exactly how you teach beginners bad and incorrect programming practices. – Lundin Nov 23 '21 at 14:03
  • 1
    @Lundin: We do not have to “silently pretend” that the code is well defined in order to explain why the program produced the behavior it did. Your **opinion** about what is good or bad has no effect on the **fact** that this answer states the reason that the program behaved the way it did. If you want to say this answer should explain that the behavior is undefined, fine, do that. If you say this answer is incorrect, then your statement is false. – Eric Postpischil Nov 23 '21 at 14:12