1

I have the following program in C++

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <limits>
using namespace std;

int main()
{
    printf("range of int: %d to %d", SHRT_MIN, SHRT_MAX);
    int a = 1000006;
    printf("\n Integer a is equal to %d", a);
    return 0;
}

My question is - How is a able to store an integer larger than the MAX limit?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
jacksparrow007
  • 1,298
  • 4
  • 19
  • 30
  • 5
    `SHRT` stands for `short`. – Yu Hao May 17 '14 at 12:38
  • 1
    in C++ you should use `std::numeric_limits::max()` anyway, as then you can be sure you're getting the limits of the right type. Also the answer is to use a `long long` which is guaranteed by the standard to be larger than an `int` – Mgetz May 17 '14 at 12:39
  • @YuHao the argument could be made that your edit was correct as `` is not guaranteed to include `` and the macros the OP is using are available in `` not ``. However what the OP should be using [`std::numeric_limits::max()`](http://en.cppreference.com/w/cpp/types/numeric_limits) is not available in `` hence me removing my comment. – Mgetz May 17 '14 at 12:49

2 Answers2

5

See http://en.cppreference.com/w/cpp/header/climits and http://en.cppreference.com/w/cpp/types/numeric_limits

SHRT_MAX is the maximum value for an object of type short int, but a is of type int, so the appropriate constant would be INT_MAX. A usual value for this on 32-bit systems would be 32767 ( 2¹⁵-1). You probably have a 64-bit system, where 2147483647 ( 2³¹-1 ) might be the upper bound.

Also, as pointed out in a comment above, you might also rather want to run

#include <limits>
#include <iostream>

int main() {
    std::cout << "type\tlowest\thighest\n";
    std::cout << "int\t"
              << std::numeric_limits<int>::lowest() << '\t'
              << std::numeric_limits<int>::max() << '\n';
    return 0;
}

in some cases (see INT_[MIN|MAX] limit macros vs numeric_limits<T> ) to determine these values (code copied from reference page mentioned above).

On a side note, if for some reason the width of the integer types is relevant to your code, you might also want to consider looking at http://en.cppreference.com/w/cpp/types/integer and http://en.cppreference.com/w/cpp/header/cstdint for fixed width integer types (see also Is there any reason not to use fixed width integer types (e.g. uint8_t)? for a discussion).

Community
  • 1
  • 1
godfatherofpolka
  • 1,645
  • 1
  • 11
  • 24
  • @Mgetz Thanks, I've added it to the answer, the page you provided is much nicer indeed (note to myself: don't always pick the first Google hit for reference purposes...) – godfatherofpolka May 17 '14 at 12:52
2

An integer type variable is a variable that can only hold whole numbers (eg. -2, -1, 0, 1, 2). C++ actually has four different integer variables available for use: char, short, int, and long. The only difference between these different integer types is that they have varying sizes

Your variable is of type int ( not short )

Minimum value for a variable of type short.

SHRT_MIN
–32768

Maximum value for a variable of type short.

SHRT_MAX
32767

Minimum value for a variable of type int.

INT_MIN
–2147483647 – 1

Maximum value for a variable of type int.

INT_MAX
2147483647

And a is able to store 1000006 , because

a = 1000006 <  2147483647

So there is not an issue :)

MCHAppy
  • 1,012
  • 9
  • 15