-6

Today while experimenting with a C++ program i tried to get a random variable from a garbage value with code that read

int main(){
    int x,r;
    r = x%7
    cout << r;
}

needless to say this method didn't work since x was being used without initialization however when I looked at he variable watch I saw that the garbage value of x was -846.... and the same value for r. This confused me as to how could an integer hold such insanely huge garbage values. Normally C++ integers are ±32676 however the insane 7+ digit value I saw was never in this range. What could be the reason for this very large value if integers can hold only small values

Suhrid Mulay
  • 175
  • 10
  • No, the `sizeof` an `int` is implementation specific. On Linux/x86 systems (both 32 and 64 bits) it is 4 bytes so 32 bits. Today only very few systems have 16 bits `int` (mostly embedded things à la [Arduino](https://www.arduino.cc/)...) – Basile Starynkevitch Jun 24 '17 at 14:38
  • actually size of 'int' is defined as 32 bits, no matter what. If you need 16, it will be 'short int'; – Serge Jun 24 '17 at 14:42
  • print [`INT_MAX`](http://en.cppreference.com/w/c/types/limits) and see – phuclv Jun 24 '17 at 14:42
  • 3
    @Serge: are you sure of that? It depends upon the C++ standard – Basile Starynkevitch Jun 24 '17 at 14:45
  • 2
    "why is the garbage value for an integer so un-integerly huge" - it may be small or large, 32bit or 64bit or pigs may fly. Point is; it is *undefined* - you can't use/read it and *if* you do, then your entire program is undefined. – Jesper Juhl Jun 24 '17 at 14:46
  • 1
    @Serge No, the size of int depends on implementation. 16 bits would be quite typical on 16 bit systems. – eerorika Jun 24 '17 at 14:47
  • 1
    @Serge there are a lot of platforms where [`int` has 16, 18, 24, 36 bits...](https://stackoverflow.com/q/2098149/995714) The only requirement is that [`INT_MAX` must be at least 32767](https://stackoverflow.com/q/589575/995714) – phuclv Jun 24 '17 at 14:49
  • ok, ok,,,, i was wrong :-(. It only defines that 'int' should accommodate INT_MAX, where the latter could be platform dependent. Here is a nice overview of it: https://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be. So, as Luu suggested, find out the value of INT_MAX/MIN in your system. – Serge Jun 24 '17 at 14:58
  • Part way down this page (http://en.cppreference.com/w/cpp/language/types) is a chart listing the minimum sizes of integer data types. – user4581301 Jun 24 '17 at 16:22

4 Answers4

2

According to this answer, the minimum (emphasis mine) ranges of an integer is -32,767 to 32,767. The actual limits are implementation defined, and are most likely from -2 billion to positive 2 billion.

To check the integer limits on your device, you could use the header <limits> as follows:

int imin = std::numeric_limits<int>::min(); // minimum value
int imax = std::numeric_limits<int>::max(); //or INT_MAX
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
1

Normally C++ integers are ±32676

This is false on most systems (except embedded things like Arduino). On most current C++ systems, int uses 32 bits. IIRC the standard mandates at least 16 bits.

You should use the <limits> standard C++11 (or better) header.

BTW your program is a typical example of undefined behaviour so you need to be very scared (and you should not expect any particular concrete behavior).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Integers are ussually at least 32bit on most architectures you are likely to use. So can hold around +/-2 billion

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
0

Integer size is usually 4 bytes on most operating systems (platforms). Therefore, at the lowest, which is 32-bit architectures, it can hold values from (-2147483648) to (+2147483648). If you're working with a 64-bit architecutre, which is the common one today, you can hold values from (-2^64 / 2) to (+2^64 / 2).

Take a look at this link that shows the size of integer on different platforms: http://ivbel.blogspot.co.il/2012/02/size-of-primitive-types-in-c-language.html

Tomer Shay
  • 771
  • 6
  • 17