0

I am wondering if it is possible to store a digit larger than 9 in an array. For example is Int myarray[0] = 1234567898

From what I know about arrays they can only be integers in which the highest number will be a 9 digit number.

Is it possible to change the data type or have a larger digit. If not what are other ways to do it

Juan Sierra
  • 75
  • 2
  • 7

3 Answers3

2

It is quite possible. C++11 adds support for long long which is typically 64bit (much larger) that can store up to 19 digits and many compilers supported it before this as an extension. You can store arbitrarily large numbers in an array provided the array is large enough and you have the requisite mathematics. However, it's not pleasant and an easier bet is to download a library to do it for you. These are known as bignum or arbitrary size integer libraries and they use arrays to store integers of any size your machine can handle.

It's still not possible to express a literal above long long though.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Good information, though I dont think this really answers the question. I dont really think the asker understands that integral types are bits at all. You may want to introduce what an int actually is first. – Vality Feb 26 '14 at 00:58
  • 1
    He does, it's just poorly phrased. – Puppy Feb 26 '14 at 00:59
  • I assumed otherwise from the "I am wondering if it is possible to store a digit larger than 9 in an array." But now I read it again I think you could be right, looking at the second paragraph. – Vality Feb 26 '14 at 01:07
  • It's pretty poorly written but I think he does know what he's talking about and what he wants. – Puppy Feb 26 '14 at 01:10
  • If you download a "bignum" library, it might very well add bignum literals. You'd have to check the documentation. – MSalters Feb 26 '14 at 10:06
1

A 32bit int has a max value of 2147483647 - that is 10 digits. If you need values higher than that, you need to use a 64bit int (long long, __int64, int64_t, etc depending on your compiler).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
-1

I am afraid that your idea that ints can only represent a single decimal digit is incorrect. In addition your idea that arrays can only hold ints is incorrect. An int is usually the computer's most efficient integral type. This is on most machines 16, 32 or 64 bits of binary data, thus can go up to 2^16 (65k), 2^32 (4million), or 2^64(very big). It is most likely 32 bits on your machine, thus numbers up to just over 4million can be stored. If you need more than that types such as long and long long are available.

Finally, arrays can hold any type, you just declare them as:

type myarray[arraysize];
Vality
  • 6,577
  • 3
  • 27
  • 48