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 ..?

Attachments Capture.JPG 25.39 KB
7 Years
Discussion Span
Last Post by firstPerson

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.

Edited by jonsca: n/a


>>short shortEgyPop = 80000000

for_each(seconds : 1 minute)
//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
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.