0

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?

3
Contributors
5
Replies
22
Views
4 Months
Discussion Span
Last Post by JamesCherrill
0
#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"Enter number"<<endl;
    cin>>n;
    cout<<"You enter:"<<n<<endl;
    system("pause");
    return 0;
}
0

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

0

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

1

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.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.