Storing and printing 10+ digit integer in c++

后端 未结 8 1694
北荒
北荒 2020-12-21 14:39

I\'m using cout to print digits to the console. I am also storing values of up to 13+billion as a digit and doing computations on it. What data type should I use?

Wh

相关标签:
8条回答
  • 2020-12-21 15:19

    unsigned long long

    can be used

    0 讨论(0)
  • 2020-12-21 15:20

    Use int64_t to guarantee you won't overflow. It is available from stdint.h.

    0 讨论(0)
  • 2020-12-21 15:23

    You could use a long int:

    long int a
    

    Or if it's always going to be positive, an unsigned long int:

    unsigned long int a
    

    See: http://forums.guru3d.com/showthread.php?t=131678

    0 讨论(0)
  • 2020-12-21 15:33

    Your data type (int) is too small to hold such large numbers. You should use a larger data type or one of the fixed size data types as given in the other answer (though you should really use uint64_t if you're not using negative numbers).

    0 讨论(0)
  • 2020-12-21 15:37

    long long can hold up to 9223372036854775807. Use something like gmp if you need larger.

    0 讨论(0)
  • 2020-12-21 15:37

    It's a good idea to understand the range limits of different sized types.

    A 32 bit type (on most 32 bit platforms, both int and long are 32 bit) have the following ranges:

    signed: -2,147,483,648 to 2,147,483,647
    unsigned: 0 to 4,294,967,295
    

    While 64 bit types (typically long long's are 64 bit, on most Unix 64 bit platforms a long is also 64) have the following range:

    signed: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
    unsigned: 0 to 18,446,744,073,709,551,615
    
    0 讨论(0)
提交回复
热议问题