Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing and printing 10+ digit integer in c++

Tags:

c++

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?

When I do the following:

int a = 6800000000;
cout << a;

It prints -1789934592.

thanks.

like image 203
Shawn Mclean Avatar asked Jan 19 '26 03:01

Shawn Mclean


2 Answers

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

like image 187
Ignacio Vazquez-Abrams Avatar answered Jan 21 '26 19:01

Ignacio Vazquez-Abrams


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

like image 34
Matthew Flaschen Avatar answered Jan 21 '26 18:01

Matthew Flaschen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!