since few minutes I tried practice "static_cast" to convert from short variable to int variable, I wrote this trivial program to print the variables before using static_cast and after using static_cast in console screen, but the variable not converted..!

//EGYPT population | static cast
#include <iostream>
using namespace std;

int main()
{
	short shortEgyPop = 80000000;
	shortEgyPop = (shortEgyPop * 10)/10;
	cout << "shortEgyPop = " << shortEgyPop << endl;
	shortEgyPop = 80000000;
	shortEgyPop = (static_cast<int>(shortEgyPop)*10)/10;
	cout << "shortEgyPop = " << shortEgyPop << endl;

	return 0;
}

And the following attachment includes my output, the second answer must be 80,000,000 not -19456 ..?

Recommended Answers

All 3 Replies

hey though u r converting it to int, u r trying to store in a short int variable.
So it is implicitly being converted to short int.

so the op is like this !!!

That is true.

Also check the number of bytes a short int occupies on your machine (using sizeof). Mine is 2 bytes. Since short is signed so you only get half of the range. The maximum in that case is 32767. You're way over that from the get-go on line 7.

>>short shortEgyPop = 80000000

for_each(seconds : 1 minute)
   printBig("OVERFLOW");
//From MSDN
//"Microsoft Visual C++ recognizes the types shown in the table below."

Type Name      Bytes      Other Names                                Range of Values
short            2      short int, signed short int short        -32,768 to 32,767
unsigned short   2       unsigned short int                       0 to 65,535
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.