tags:

views:

417

answers:

8

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

+3  A: 

Add an ll at the end of your integer constant.

pavpanchekha
I've tried that. doesn't worknumber = 10000000000llprintf("number = %lld, number);gives me 'number = 1410065408'
Eddy
Perhaps you might specify that "ll" is lowercase "LL" instead, I wasn't sure of the difference between what you wrote and eleven (11)[EDIT]If you know where gcc is installed, you can type "gcc -v" to get the version.
Arthur Kalliokoski
Yeah sorry about the confusion. I'm not sure how to get the version of gcc, I'm using windows xp not linux so the command 'gcc' is not recognised in command prompt.
Eddy
A: 

Um, Code::Blocks uses GCC as its usual compiler. And recent versions of that support 64bit types explicitly.

So you should be able to

#include <inttypes.h>
uint64_t unsigned64BitNumber;
int64_t signed64BitNumber;
Chris Becke
A: 

You should be able to use long long int with a gcc compiler but i think it may require using the c99 std code where as your default may be c89 mode. try adding --std=c99 to your compiler commandline and see if this helps :-)

Thats in addition to pavpanchekha's response
Sorry I'm a bit of a newb, I'm not using a commandline I'm just clicking on 'build and run' in Codeblocks. How do I do this?
Eddy
No problem. Its been a while since i used codeblocks but i think going to "settings" then "compiler and debugger" brings up the relevant window. From there you want the the "Compiler settings/ compiler flags tab" and look for and option to use C99 mode or ISO C90 for c programs. I'm not sure of the exact option name.Halfway down http://www.codeblocks.org/docs/main_codeblocks_en3.htmldescibes hthe compiler options window
Actualy, make sure you use C99 im not sure even c90 included long long support
I've tried checking the box 'In C mode, support all ISO C90 programs'. That doesn't work, and now it says 'LLONG_MAX was not declared in this scope'
Eddy
Woops didn't refresh page for a while.There isn't an option for C99 standard in the compiler flags settings.
Eddy
A: 

Perhaps the compiler is confused by the int portion of the datatype - have you tried using a long long instead?

This website might help you out.

CrimsonX
I've tried that. I had a look at this website too, which led me to try 'printf("LLONG_MAX = %lld" LLONG_MAX);'. It gives me LLONG_MAX = -1 which is rather odd :(
Eddy
+8  A: 

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
Chris Wilson
Thanks, this was also very helpful
Eddy
A: 

In addition to the previous comments about suffixes and gcc C99 mode, if you can't get long long to work, AND you only need integers up to 2^52, you can get away with using double. Integers up to 2^52 should be exactly representable as double assuming an IEEE double precision format (0 +1 bias exponent).

Mark B
+1  A: 

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

lsalamon
Thanks! It's working now :)
Eddy
A: 

As people already posted, you should check what compiler you're using. In Code:Blocks you do (in my version anyway, hopefully it will work for you too):

First find out what compiler is selected for your project by selecting Project->Build options... and see what it says in the "Selected compiler"

Then select: Settings->Compiler and debugger... and select the compiler you just found out. Then click the "Toolchain executables" and see what it says for e.g. "C compiler".

Maybe if you succeed, and post your results, someone here will be able to help you.

S.C. Madsen