I declare an integer and input value from user when i check this program i give the value 40,000 its a overflow but program generate no any type of error why?

8 Months
Discussion Span
Last Post by JamesCherrill
using namespace std;
int main()
    int n;
    cout<<"Enter number"<<endl;
    cout<<"You enter:"<<n<<endl;
    return 0;

when i enter 9999999999 than it shows error that output is you enter 2147483647


It would seem that int declares a 32 bit signed integer. You could try

long long unsigned int n;

but you'd still end up with a problem if you added a few more digits to the input string.

As to why an error is not generated that might depend of one or more compiler switches/options. I used the defaults in Visual Studio 2012 and also did not get an error.

Edited by Reverend Jim


The C++11 Standard says that signed integer overflow/underflow behaviour is "undefined", so any compiler can legally do whatever it wants, including ignore it. Ignoring it (on normal computers) is a lot easier and faster that detecting it and doing something sensible. That's C++ for you.

8.4: If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.