'长长整型'被解释为“long int”。我该如何解决这个问题?
我正在为大学数学课程开展涉及 C 编程的项目。 我需要能够处理大整数,大于可以存储在“long int”数据类型中的整数。所以我尝试使用“long long int”,但如果我尝试这样的操作:
long long int number;
number = 10000000000;
那么错误消息会显示“错误:整数常量对于“long”类型来说太大”。
我尝试过其他数据类型,如“___int64”和“int_64t”,我尝试过包括所有标准 c 库,但仍然遇到同样的问题。
奇怪的是,当我尝试 'printf("LLONG_MAX = %lld\n", LLONG_MAX);'
时,我得到以下信息:
LLONG_MAX = -1
我在 Windows XP 上使用 Codeblocks 8.02,但我不确定是什么由于我在校园内使用网络计算机并且无权访问主文件系统,因此安装了 gcc 编译器版本。我不想每天都把笔记本电脑带进校园。请帮忙!谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
当编译器编译 C 文件并遇到整数或浮点常量时,需要为其分配一个类型。它会隐式地为您选择默认类型。您可以通过为编译器提供整数后缀来显式设置类型。整数后缀可以告诉编译器它是否是一个长整数,
long long 或无符号类型。
浮点类型也有这个情况。类型可以是 float、double
或长双。浮点类型通常默认为 double。
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.
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.
在整数常量末尾添加一个
ll
。Add an
ll
at the end of your integer constant.在 Microsoft 环境中使用 printf 语法如下:
In Microsoft environment use printf with this syntax :
嗯,Code::Blocks 使用 GCC 作为其常用的编译器。最新版本明确支持 64 位类型。
所以你应该能够
Um, Code::Blocks uses GCC as its usual compiler. And recent versions of that support 64bit types explicitly.
So you should be able to
您应该能够在 gcc 编译器中使用 long long int ,但我认为它可能需要使用 c99 std 代码,其中您的默认值可能是 c89 模式。尝试将 --std=c99 添加到编译器命令行中,看看这是否有帮助:-)
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 :-)
也许编译器对数据类型的 int 部分感到困惑 - 您是否尝试过使用
long long
来代替?此网站可能会帮助您。
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.
除了前面关于后缀和 gcc C99 模式的评论之外,如果您无法使用 long long 来工作,并且您只需要最大 2^52 的整数,那么您可以使用 double 。假设采用 IEEE 双精度格式(0 +1 偏差指数),最大 2^52 的整数应精确表示为 double。
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).
正如人们已经发布的那样,您应该检查您正在使用的编译器。
在 Code:Blocks 中,您所做的(无论如何,在我的版本中,希望它也适合您):
首先通过选择来找出为您的项目选择的编译器
项目->构建选项...并查看“选定的编译器”中的内容
然后选择:
Settings->Compiler and debugger...并选择你刚刚找到的编译器。
然后单击“工具链可执行文件”并查看其内容,例如“C 编译器”。
也许如果您成功并发布您的结果,这里的人将能够帮助您。
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.