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?

Dani AI

Generated

The observation in this thread is normal for C++: arithmetic overflow and input overflow are different things, and neither usually raises a runtime “error” by itself. ’s program shows a large decimal string being converted into an int; that conversion (via the IO library) will clamp and set the stream error flag rather than throwing, while plain arithmetic overflow is unspecified/undefined behavior at the language level. (en.cppreference.com) C++: arithmetic overflow (cppreference) C++: istream extraction behavior (cppreference).

Clarifying the two points already made by and : signed integer arithmetic overflow is undefined behavior (the compiler is free to do nothing, wrap, or optimize assuming overflow cannot occur), so it won’t reliably produce a runtime message. For finding arithmetic overflows during testing, turn on sanitizers or trapping options (for example GCC/Clang’s UBSan flags or -ftrapv/related options). (en.cppreference.com)

Stream extraction (cin >>) changed in C++11: if the parsed numeric string doesn’t fit the target type the implementation stores the type’s min/max and sets failbit (which explains the printed 2147483647). Always check the stream state after extraction (if(!(cin >> x)) { ... }) and clear/ignore the rest of the line before retrying. (en.cppreference.com)

Practical fixes (short examples):

Read a line and parse with non-throwing, range-aware parser:

#include <charconv>
std::string s;
std::getline(std::cin, s);
int v;
auto r = std::from_chars(s.data(), s.data()+s.size(), v);
if (r.ec == std::errc::result_out_of_range) { /* too big */ }
else if (r.ec == std::errc::invalid_argument) { /* not a number */ }
else { /* v valid */ }

Or keep using cin >> but check and recover:

int n;
if(!(std::cin >> n)) {
  std::cin.clear();
  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  // handle bad/overflow input
}

To detect arithmetic overflows in computations, use compiler intrinsics or builtins (e.g. __builtin_add_overflow on GCC/Clang) or run with UBSan (-fsanitize=signed-integer-overflow) during testing. (en.cppreference.com)

Summary: no surprise that the program didn’t “throw” — parsing writes min/max and sets failbit, and arithmetic overflow is not a built-in exception in C++. The robust approach is explicit validation: use wider types where appropriate, parse with range-aware routines, check stream state, and enable sanitizers while testing.

Recommended Answers

All 5 Replies

It would be a lot easier to answer that if you posted the code.

#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"Enter number"<<endl;
    cin>>n;
    cout<<"You enter:"<<n<<endl;
    system("pause");
    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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.