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

3 Answers

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.

2

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.

1

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.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.