开发者

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

开发者 https://www.devze.com 2022-12-22 07:14 出处:网络
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. S

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


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


Add an ll at the end of your integer constant.


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


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;


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 :-)


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号