If the range of int is up to 32768, then I have to input a value of around 50000 and use it,I want to input it without using long and if possible without using typecasting also. Is there any way to do it. I want the datatype to remain int only.
-
1Where did you get this range? it's platform and implementation specific. – EdChum Jan 28 '15 at 11:28
-
Can you explain why you only want int? – tohava Jan 28 '15 at 11:29
-
1The size of `int` actually depends on your platform / compiler, it's not fixed-size – notadam Jan 28 '15 at 11:29
-
I5 4th gen in old turbo c....I actually want the range of int to be increased either In turbo or Dev or gcc – sharan Jan 28 '15 at 11:30
-
You can't do it. See if your platform supports a wider integer type (say 32 bits) and use that. – juanchopanza Jan 28 '15 at 11:30
-
1Using an unsigned int will increase the positive range – abcthomas Jan 28 '15 at 11:32
-
I guess the only thing you can do is use int64_t (if your platform supports it) – notadam Jan 28 '15 at 11:34
-
It supports a long type and long long ..But this is one of our entry question into project team – sharan Jan 28 '15 at 11:34
-
Can we by some means increase the bit field of int – sharan Jan 28 '15 at 11:35
-
@abcthomas I agree that the question probably aims at using unsigned int. Storing 65537 will be harder. – Peter - Reinstate Monica Jan 28 '15 at 11:36
-
The range of `int` isn't 'up to 32768' in any version of C I have used since 1982. Check your facts. It was -32768..32767 in 16-bit versions, but I haven't used one of those for about 20 years. – user207421 Jan 28 '15 at 11:37
-
Actually you may be able to use `int i; scanf("%u", &i);` for input of the 50000 if you know how that overflow is handled, or in C99 where, iirc, the overflow is specified to have 2-complement semantics. . Likewise you should be able to printf that int with `%u`. Strictly spoken that should be UB though. – Peter - Reinstate Monica Jan 28 '15 at 11:42
-
@EJP I understand that 16 bit is not used now a days .. but this is a quiz question I was asked.. They asked for a way to get a out of range value in int ..With out changing the datatype int and type casting is also not allowed – sharan Jan 28 '15 at 11:45
-
possible duplicate of [Does the size of an int depend on the compiler and/or processor?](http://stackoverflow.com/questions/2331751/does-the-size-of-an-int-depend-on-the-compiler-and-or-processor) – sashoalm Jan 28 '15 at 12:02
6 Answers
Any built-in type cannot be altered nor expanded in any sense. You have to switch to a different type.
The type int
has the following requirements:
- represents at least the range -32767 to 32767 (16bit)
- is at least as large as
short
(sizeof(short) <= sizeof(int)
)
This means, that strictly speaking (although most platforms use at least 32bit for int
), you can't safely store the value 50000 in an int
.
If you need a guaranteed range, use int16_t
, int32_t
or int64_t
. They are defined in the header <cstdint>
. There is no arbitrary precision integer type in the language or in the standard library.
If you only need to observe the range of valid integers, use the header <limits>
:
std::cout << std::numeric_limits<int>::min() << " to " << std::numeric_limits<int>::max() << "\n";

- 10,215
- 4
- 49
- 90
Several possibilities come to mind.
- @abcthomas had the idea to use unsigned; since you are restricted to int, you may abuse int as unsigned. That will probably work, although it is UB according to the standard (cf. Integer overflow in C: standards and compilers).
- Use two ints. probably involves writing your own scanf and printf versions, but that shouldn't be too hard. Strictly spoken though, you still haven't expanded the range of an int.
- [Use long long] Not possible since you must use int.
- You can always use some big number library. Probably not allowed either.
- Keep the numbers in strings and do arithmetic digit-wise on the strings. Doesn't use int though.
But you'll never ever be able to store something > MAX_INT in an int.

- 1
- 1

- 15,048
- 4
- 37
- 62
-
@sashoalm Not sure to which of my suggestions you referred, but no, it wouldn't. It wouldn't because it's not possible. – Peter - Reinstate Monica Jan 28 '15 at 12:00
-
I was referring to the "Use long long", but that was before your edit. – sashoalm Jan 28 '15 at 12:00
No!
An int depends on the native machine word, which really means it depends on 3 things - the processor, the OS, and the compiler.
The only way you can "increase" an int foo;
(not a long foo;
, int is not a long) is:
You are compiling with Turbo-C or a legacy 16-bit DOS compiler on a modern computer, likely because your university requires you to use that, because that's what your professor knows. Switch the compiler. If your professor insists you use it, switch the university.
You are compiling with a 32-bit compiler on a 64-bit OS. Switch the compiler.
You have 32-bit OS on a 64-bit computer. Reinstall a 64-bit OS.
You have 32-bit processor. Buy a new computer.
You have a 16-bit processor. Really, buy a new computer.

- 75,001
- 122
- 434
- 781
-
I'm sure one could emulate a 32 bit machine on a 16 bit architecture. After all, even the zx80 was turing complete within its 4k. Chances are though that *somewhere* in the hypervisor would be a hidden cast which invalidates that idea as a solution to your problem. – Peter - Reinstate Monica Jan 28 '15 at 11:44
-
@PeterSchneider If the OP feels up to it, he's free to create his own compiler that does that. I'd tell him "good luck doing it". – sashoalm Jan 28 '15 at 11:48
You may try unsigned int. Its same as int but with positive range(if you really dont want to use long).
see this for the range of data types
suggestion: You might aswell consider switching your compiler. From the range you've mentioned for int, it seems you are using a 16 bit compiler(probably turbo c). A 16-bit compiler would restrict unsigned int range to 0-65536(2^16) and signed int to –32,768 to 32,767.

- 7,375
- 24
- 33
Try splitting up your value (that would fit inside a 64-bit int) into two 32-bit chunks of data, then use two 32-bit ints to store it. A while ago, I wrote some code that helped me split 16-bit values into 8-bit ones. If you alter this code a bit, then you can split your 64-bit values into two 32-bit values each.
#define BYTE_T uint8_t
#define TWOBYTE_T uint16_t
#define LOWBYTE(x) ((BYTE_T)x)
#define HIGHBYTE(x) ((TWOBYTE_T)x >> 0x8)
#define BYTE_COMBINE(h, l) (((BYTE_T)h << 0x8) + (BYTE_T)l)
I don't know if this is helpful or not, since it doesn't actually answer your original question, but at least you could store your values this way even if your platform only supports 32-bit ints.

- 2,754
- 2
- 19
- 35
Here is an idea to actually store values larger than MAX_INT in an int. It is based on the condition that there is only a small, known number of possible values.
You could write a compression method which computes something akin to a 2-byte hash. The hashes would have to have a bijective (1:1) relation to the known set of possible values. That way you would actually store the value (in compressed form) in the int, and not in a string as before, and thus expand the range of possible values at the cost of not being able to represent every value within that range.
The hashing algorithm would depend on the set of possible values. As a simple example let's assume that the possible values are 2^0, 2^1, 2^2... 2^32767. The obvious hash algorithm is to store the exponent in the int. A stored value of 4 would represent the value 16, 5 would represent 32, 1000 would represent a number close to 10^301 etc. One can see that one can "store" extraordinarily large numbers in a 16 bit int ;-). Less regular sets would require more complicated algorithms, of course.

- 15,048
- 4
- 37
- 62