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

Dani AI

Generated

, and are correct that the value was lost by the time you tried to cast it back. The key point to add: the truncation happens at the moment you initialize a too-small object, so a later static_cast only converts whatever truncated bits remain — it cannot "recover" the original 80,000,000.

A quick way to see why you got -19456 is to look at the 16-bit wrap:

80000000 mod 2^16 = 80000000 - 65536*1220 = 46080
interpreting 46080 as signed 16-bit gives 46080 - 65536 = -19456

That arithmetic explains the observed value.

Fixes and practical tips:

  • Use a type that can hold the value (for example int, long, or fixed-width types from <cstdint> such as int32_t/int64_t) and do arithmetic in that type.
  • If you must convert a large integer down to a smaller type, check the range first with std::numeric_limits and handle overflow explicitly:
    #include <limits>
    long long big = 80000000;
    if (big >= std::numeric_limits<short>::min() &&
      big <= std::numeric_limits<short>::max()) {
      short s = static_cast<short>(big);
    } else {
      // handle overflow
    }
  • Enable compiler diagnostics and use list-initialization to catch narrowing at compile time (e.g. short s{80000000}; will fail to compile in C++11+).

For reference on narrowing and integral conversions see the C++ reference pages on list-initialization and integral conversions:
list-initialization
integral conversions

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.