Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'long long int' is interpreted as 'long int'. How do I get round this?

Tags:

c++

c

integer

I'm working on project involving c programming for my mathematics course at university. I need to be able to handle large integers, larger than those that can be stored in a 'long int' datatype. So I tried using 'long long int', but if I try something like this:

long long int number;
number = 10000000000;

Then the error message says 'error: integer constant too large for "long" type'.

I've tried other datatypes like '___int64' and 'int_64t' I've tried including all the standard c libraries and I still get the same problem.

Strangely, when I try 'printf("LLONG_MAX = %lld\n", LLONG_MAX);', I get this:

LLONG_MAX = -1

I'm using Codeblocks 8.02 on windows xp, but I'm not sure what version of gcc compiler is installed since I'm using network computers on campus and I don't have permission to access the main filesystem. I don't want to have to bring my laptop into campus everyday. Please help! Thanks

like image 236
Eddy Avatar asked Mar 10 '10 15:03

Eddy


3 Answers

When the compiler is compiling your C file and comes across an integer or floating point constant it needs to assign it a type. It will implicitly choose a default type for you. You can explicitly set the type by providing the compiler the integer suffix. An integer suffix can tell the compiler if it's a long, long long, or unsigned type.

  • 10 is implicitly a signed integer
  • 10u, 10U is explicitly an unsigned integer
  • 10l, 10L is explicitly a signed long integer
  • 10ll, 10LL or 10i64 on win32 is explicitly a signed long long integer
  • 10ull is explicitly an unsigned long long

Floating point types also have this situation. A type can either be a float, a double or a long double. A floating point type usually defaults to double.

  • 10.0 is implicitly a double
  • 10.0f or 10.0F is explicitly a float
  • 10.0l or 10.0L is explicitly a long double
like image 69
Chris Wilson Avatar answered Oct 09 '22 18:10

Chris Wilson


Add an ll at the end of your integer constant.

like image 42
pavpanchekha Avatar answered Oct 09 '22 18:10

pavpanchekha


In Microsoft environment use printf with this syntax :

    __int64 i64 = 10000000000;
    unsigned __int64 u64 = 10000000000000000000;

    printf ( "%I64d\n", i64 );
    printf ( "%I64u\n", u64 );
    printf ( "%I64d\n", u64 ); <-- note this typo

like image 38
lsalamon Avatar answered Oct 09 '22 18:10

lsalamon