user2684719
user2684719

Reputation:

How to read and assign big integers in C?

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); 
  }

Upvotes: 0

Views: 1253

Answers (3)

0xF1
0xF1

Reputation: 6116

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".

Upvotes: 0

user823738
user823738

Reputation: 17521

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

Upvotes: 6

keltar
keltar

Reputation: 18399

long a = 1234567890123456L;

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

long long a = 1234567890123456LL;

Upvotes: 2

Related Questions