2

How can I store a large integer value in a variable of C ? If i am declaring a with int a; it won't work. I have used this with long long int.It is not working.

if( a>=0 && a <= (1000000000000000000))

What to declare variable a so that it will not so any error.It should be integer.

Compiler error
integer constant is too large for long type.
James King
  • 6,229
  • 3
  • 25
  • 40
user3599293
  • 61
  • 1
  • 1
  • 2

3 Answers3

1

Try a unsigned long long, assuming the value is positive, it can hold up to

18,446,744,073,709,551,615

(VS 13) However, you must use the ULL syntax.

yizzlez
  • 8,757
  • 4
  • 29
  • 44
  • 4
    You need to inform the OP that to do this he requires the `ULL` suffix (otherwise the value will be a [signed] `long long` _if_ his `long long` is long enough). Otherwise he's just going to change the type of `a` and nothing will happen. You also need to inform him that C++ didn't not even define `long long` until C++11, which he may not be using. – Lightness Races in Orbit May 03 '14 at 14:29
  • @user3599293: To read a long long you have to use `scanf("%lld",&a)` . – Martin R May 03 '14 at 18:26
1

Lets look at this if statement you wrote:

if( a>=0 && a <= (1000000000000000000))

1000000000000000000 is too big for an integral literal so you will need a bigger literal type. You should declare a as an int64_t and do the comparion like that:

if( a>=INT64_C(0) && a <= INT64_C(1000000000000000000))

Note that this will only work in a C99 or C++11 compiler when you #include <cstdint> or #include <stdint.h>

Edit: in current draft of the standard you can find this sentence (2.14.2/2):

The type of an integer literal is the first of the corresponding list in Table 6 in which its value can be represented.

It means that compiler should use the required literal type automatically to make your literal fit. Btw I didn't see that kind of compiler.

Nazar554
  • 4,105
  • 3
  • 27
  • 38
0

You can use a long long to store this integer. A long long is guranteed to hold at least 64 bits.

The problem with this code: if( a>=0 && a <= (1000000000000000000)) is that you need to give the literal (1000000000000000000) the suffix LL if you want it to be of type long long or ULL for unsigned long long.

  • 5
    `you need to give the literal (1000000000000000000) the suffix LL if you want it to be of type long long` Once again, no, that is not true. – Lightness Races in Orbit May 03 '14 at 14:32
  • @LightnessRacesinOrbit: Indeed the new C++ standards mandate that the integral literal be of type `long long` if `int` and `long` is not enough to represent it. However, you can never trust the C++ compiler (especially those from Microsoft) for its standard compliance. It's better to be explicit. – Siyuan Ren May 03 '14 at 14:44
  • @C.R. I would agree with that. Still, one should not make patently false claims. – Lightness Races in Orbit May 03 '14 at 14:57