How to use 12 digit number in C?
Asked Answered
K

3

6

I am dealing with a math example. I need to use 12 digit number for my code. So which datatype should i use, to use the number in my functions?

Krystalkrystalle answered 13/8, 2010 at 11:29 Comment(0)
D
2

64-bit integers (long, int64_t, unsigned long, uint64_t) should do the trick, or if you need decimals, double or long double.

Deyo answered 13/8, 2010 at 11:32 Comment(6)
If you need decimals you're out of luck in pure C; double and long double are floating point types, not decimal ones.Pitzer
No. I did mention 64-bit integers too, which can hold well over 12 digits even when signed!Deyo
what should i use for example while printing them? "%ld" was for long as far i remembered?Krystalkrystalle
Yes. %ld is the correct format specifier. Use %lu if it's an unsigned long, however.Deyo
Check out <inttypes.h>. It has printf() / scanf() format specifiers for the intX_t types (among others).Intolerant
You should not recommend long as a 64-bit type. It's 32-bit on most systems and the C standard only requires it to be at least 32-bit.Jarredjarrell
F
5

If you have a 64-bit integer type, I'd go with that, since it gives you the (18 full digits) range:

−9,223,372,036,854,775,808 to
+9,223,372,036,854,775,807

For other tasks (even bigger integers or massive floating point values), I use GMP, the GNU multi-precision library. It's performance is impressive.

Febricity answered 13/8, 2010 at 11:44 Comment(0)
D
2

64-bit integers (long, int64_t, unsigned long, uint64_t) should do the trick, or if you need decimals, double or long double.

Deyo answered 13/8, 2010 at 11:32 Comment(6)
If you need decimals you're out of luck in pure C; double and long double are floating point types, not decimal ones.Pitzer
No. I did mention 64-bit integers too, which can hold well over 12 digits even when signed!Deyo
what should i use for example while printing them? "%ld" was for long as far i remembered?Krystalkrystalle
Yes. %ld is the correct format specifier. Use %lu if it's an unsigned long, however.Deyo
Check out <inttypes.h>. It has printf() / scanf() format specifiers for the intX_t types (among others).Intolerant
You should not recommend long as a 64-bit type. It's 32-bit on most systems and the C standard only requires it to be at least 32-bit.Jarredjarrell
M
2

you can also use "unsigned long long" with format specifier "llu". It works fine for 12 digit number in C.

unsigned long long i=600851475143;
printf("%llu",i);
Milkwort answered 30/9, 2013 at 6:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.