-4

I wrote in c the code:

#include <stdlib.h>
#include <stdio.h>

int main()
{

    int num= 2147483646;
    printf("%d,%d" , num+1, num+2);

    return 0;
} 

and the output is -2147483647,-2147483648

Why does the number becomes negative when I add 2 to the variable num?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Yael123
  • 1
  • 1
  • 1
    You should read about 2's complement representation of negative numbers. It explains it all. – Paul Ogilvie Nov 01 '17 at 12:05
  • 1
    Post the real code and the real output. Don't manually re-type it here, copy paste it. – Lundin Nov 01 '17 at 12:08
  • Where can I read about it? I'm totally new to this website and programming in general. – Yael123 Nov 01 '17 at 12:09
  • 1
    @Yael123: It's buried quite deeply within the rules of C (which legitimises this question IMHO). Start reading a book: https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – Bathsheba Nov 01 '17 at 12:10
  • If you study programming in a proper school, they teach you binary, hex and two's complement before you are even allowed to write a single line of code. [Google two's complement](https://en.wikipedia.org/wiki/Two%27s_complement). – Lundin Nov 01 '17 at 12:21
  • 2
    @Bathsheba This has little to do with C and everything to do with how CPUs work. And unlike C, CPUs have well-defined two's complement wrap-around. (Which would be why "integer overflow is UB" is a very stupid language restriction in the first place.) – Lundin Nov 01 '17 at 12:26
  • @Lundin: I wonder why it wasn't promoted to "implementation defined" in C99? On the C++ side, from C++11 we now have a well-defined `%` for negative arguments. Whoop-de-doo. But nothing for `int` overflow. – Bathsheba Nov 01 '17 at 12:45
  • @Bathsheba It is because all the old men in ivory towers who still insist that C must support signed magnitude and one's complement computers. Because if C doesn't, then they can no longer make besserwisser comments to make themselves seem smart. "But what if another signedness format is used!" "But what about integer padding bits!" "But what if the number is a trap representation!" And similar BS. Of course, there have never existed any signed magnitude or one's complement computers as mainstream products. But they must be supported, come hell or high water! – Lundin Nov 01 '17 at 13:04

3 Answers3

5

Are you sure the output is not 2147483647, -2147483648? I've never come across a platform where 2147483646 is a valid int and 2147483647 is not (despite it being theoretically possible).

The behaviour of overflowing an int type is undefined in C.

What appears to be happening here is a wraparound to INT_MIN once INT_MAX is attained (a fairly common manifestation of this particular undefined construct, but don't rely on that).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

The output is 2147483647, -2147483648 and that's due to overflowing of the int type. This type in a 4 bytes and 2's complement implementation have a range of [–2,147,483,648, 2,147,483,647]. Your overflow is undefined behaviour.

César PA
  • 31
  • 4
-3

C is designed to work as closely as possible to the way a computer works. This means that numbers (or any data type, for that matter), is represented in memory in a very strict way. For instance, integers are represented by 32 bits, with 2^32 possible values.

Depending on your computer, signed integers range from –2147483647 to 2147483647, where as unsigned integers range from 0 to 4294967295 (see https://msdn.microsoft.com/en-us/library/7fh3a000.aspx or http://www.cplusplus.com/reference/climits/)

The computer adds numbers mechanically, and doesn't check that adding caused it to "go over" the limit. Instead, the number just goes from the maximum possible to the minimum possible values.

moomima
  • 1,200
  • 9
  • 12
  • "integers are represented by 32 bits, with 2^32 possible values" - very far from being necessarily the case. – Bathsheba Nov 01 '17 at 12:11
  • 1
    32 bit 2's complement numbers range from –2147483648. Also, this is nonsense: "The computer adds numbers mechanically, and doesn't check that adding caused it to "go over" the limit." Pretty much every single CPU in the world has some manner of condition code register where there is an overflow bit, called something like 'O' or 'V'. The computer does check for overflow and then uses a well-defined wrap-around. – Lundin Nov 01 '17 at 12:36