0

I am trying to assign big integer value to a variable in c and when I print I only get 10123456.

What is the issue?

  int main(){
      long a = 1234567890123456;
      printf("\n",sizeof(a));
      printf("%ld",a); 
  }
TryinHard
  • 4,078
  • 3
  • 28
  • 54

3 Answers3

6

Largest integer type is:

unsigned long long

dont forget about ULL suffix.
or if you need larger integers, take a look for some bigint libraries like gmp.

Of course, there is also long long, but it is also for negative integers and have smaller limits.

 Type                min                      max

 (signed) long long  -9223372036854775808     9223372036854775807
 unsigned long long  0                        18446744073709551615
Zaffy
  • 16,801
  • 8
  • 50
  • 77
  • In C 1999 and later, the largest integer types described by the standard are `intmax_t` and `uintmax_t`. They are at least as large as `unsigned long long` and might be larger. – Eric Postpischil Aug 20 '13 at 13:08
2
long a = 1234567890123456L;

If long is long enough, depends on compiler/OS. If not

long long a = 1234567890123456LL;
keltar
  • 17,711
  • 2
  • 37
  • 42
0

If the number is greater than 64-bit, i.e. longer than what unsigned long long can hold, then no data type in C other than string(char[]) will be able to accomodate that value, store it as a string & try writing your own functions to operate (e.g. add, subtract, etc.) on these "very large numbers".

0xF1
  • 6,046
  • 2
  • 27
  • 50
  • There is no `string` in C – Zaffy Aug 20 '13 at 10:07
  • 1
    @Zaffy I know, that's why I wrote string(`char[]`), sorry for writing _string_ as a `code`. – 0xF1 Aug 20 '13 at 10:20
  • @Zaffy C11 7.1.1 1 "Definitions of terms. A _string_ is a contiguous sequence of characters terminated by and including the first null character. ..." – chux - Reinstate Monica Aug 20 '13 at 12:22
  • @Chux I meant `string` datatype. – Zaffy Aug 20 '13 at 12:43
  • 1
    It is not generally true that an integer greater than what fits into 64 bits is not representable in some integer type in C. C 1999 and later provides for a variety of integer types, and an implementation may provide integer types of any size. – Eric Postpischil Aug 20 '13 at 13:09
  • @Eric Postpischil: I didn't know that, thanks for the heads up. Can you please share some example of those data types, or give some link or other resource, where I can read more about them. – 0xF1 Aug 20 '13 at 17:27
  • 1
    @nishant: Clause 7.20 of [the C standard](http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf) describes the `stdint.h` headers, which describes types such as `intmax_t` and `uintmax_t` for integers of the greatest width in the implementation, along with types for specific widths, macros for writing constants of the various types, and other embellishments. The `inttypes.h` header in clause 7.8 provides format specifiers for using the types with `printf`. – Eric Postpischil Aug 20 '13 at 17:51