I am a little confused on why I do not get an overflow when I use the the following code.
I am using testvar+1 as an expression in cout. This should cause an overflow but doesn't
Code:#include<iostream> #include<cstdlib> #include<cmath> using namespace std; int main() { short testvar=32767; cout<<"The contents of testvar is"<<testvar<<endl; cout<<"The contents of testvar with one added is: "<<testvar+1<<endl; system("pause"); return 0; }
However
The code below causes an overlow, with the variable testvar in cout.
Not sure I understand what the differences are between the two code segments. In both instances you are adding 1 to testvar which should cause an overflow.Code:#include<iostream> #include<cstdlib> #include<cmath> using namespace std; int main() { short testvar=32767; cout<<"The contents of testvar is"<<testvar<<endl; testvar+=1; cout<<"The contents of testvar with one added is: "<<testvar<<endl; system("pause"); return 0; }