'长长整型'被解释为“long int”。我该如何解决这个问题?

发布于 2024-08-24 09:59:12 字数 526 浏览 3 评论 0原文

我正在为大学数学课程开展涉及 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

三生一梦 2024-08-31 09:59:12

当编译器编译 C 文件并遇到整数或浮点常量时,需要为其分配一个类型。它会隐式地为您选择默认类型。您可以通过为编译器提供整数后缀来显式设置类型。整数后缀可以告诉编译器它是否是一个长整数,
long long 或无符号类型。

  • 10 隐式是有符号整数
  • 10u, 10U 显式是无符号整数
  • 10l, 10L 显式是有符号长整数
  • win32 上的 10ll、10LL 或 10i64 显式是有符号 long long 整数
  • 10ull 显式是无符号 long long

浮点类型也有这个情况。类型可以是 float、double
或长双。浮点类型通常默认为 double。

  • 10.0 隐式为 double
  • 10.0f 或 10.0F 显式为 float
  • 10.0l 或 10.0L 显式为 long 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.

  • 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
水染的天色ゝ 2024-08-31 09:59:12

在整数常量末尾添加一个 ll

Add an ll at the end of your integer constant.

寂寞花火° 2024-08-31 09:59:12

在 Microsoft 环境中使用 printf 语法如下:

    __int64 i64 = 10000000000;
    unsigned __int64 u64 = 10000000000000000000;

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

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

逆夏时光 2024-08-31 09:59:12

嗯,Code::Blocks 使用 GCC 作为其常用的编译器。最新版本明确支持 64 位类型。

所以你应该能够

#include <inttypes.h>
uint64_t unsigned64BitNumber;
int64_t signed64BitNumber;

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;
浪菊怪哟 2024-08-31 09:59:12

您应该能够在 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 :-)

゛清羽墨安 2024-08-31 09:59:12

也许编译器对数据类型的 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.

小女人ら 2024-08-31 09:59:12

除了前面关于后缀和 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).

梦一生花开无言 2024-08-31 09:59:12

正如人们已经发布的那样,您应该检查您正在使用的编译器。
在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文