整数文字的类型默认情况下不诠释? [英] Type of integer literals not int by default?

查看:151
本文介绍了整数文字的类型默认情况下不诠释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才已经回答这个问题,该问为什么迭代直到晚上10十亿在for循环需要这么长很多(中OP实际上10分钟后中止它),比迭代,直到1十亿:

I just answered this question, which asked why iterating until 10 billion in a for loop takes so much longer (the OP actually aborted it after 10 mins) than iterating until 1 billion:

for (i = 0; i < 10000000000; i++)

现在我和许多其他人的明显的答案是,这是由于迭代变量为32位(从未达1000十亿)和循环得到一个无限循环。

Now my and many others' obvious answer was that it was due to the iteration variable being 32-bit (which never reaches 10 billion) and the loop getting an infinite loop.

不过,虽然我意识到这个问题,我仍然不知道什么是真的要在里面编译?

But though I realized this problem, I still wonder what was really going on inside the compiler?

由于文字不是用的 L时,应恕我直言,是类型 INT 的追加,也和因此32位。因此,由于溢出它应该是一个正常的 INT 的范围内可到达。要真正认识到,它不能从 INT 到达,编译器需要知道它在10十亿,因此认为这是一个更比32位常数。

Since the literal was not appended with an L, it should IMHO be of type int, too, and therefore 32-bit. So due to overflow it should be a normal int inside the range to be reachable. To actually recognize that it cannot be reached from int, the compiler needs to know that it is 10 billion and therefore see it as a more-than-32-bit constant.

请问这样的文字得到提拔到一个合适的(或至少实现定义)范围内(至少64位,在这种情况下)自动,即使不追加一个,是这一标准的行为吗?或者是不同的东西会在幕后,就像UB由于溢出(是整数溢出实际上UB)?从标准的一些报价可能是好的,如果有的话。

Does such a literal get promoted to a fitting (or at least implementation-defined) range (at least 64-bit, in this case) automatically, even if not appended an L and is this standard behaviour? Or is something different going on behind the scenes, like UB due to overflow (is integer overflow actually UB)? Some quotes from the Standard may be nice, if any.

尽管最初的问题是C,我也美联社preciate C ++的答案,如果有什么不同。

Although the original question was C, I also appreciate C++ answers, if any different.

推荐答案

至于C ++而言:

C ++ 11,[lex.icon]¶2

C++11, [lex.icon] ¶2

类型字面的整数的是第一表6中的相应的列表中其值可以被重新presented的

The type of an integer literal is the first of the corresponding list in Table 6 in which its value can be represented.

和表6,因为没有后缀和小数常量文字,给人:

And Table 6, for literals without suffixes and decimal constants, gives:

int
long int
long long int

(有趣的是,十六进制或八进制常数也无符号类型允许的 - 但每一个来的之后的相应签订一个在名单)

(interestingly, for hexadecimal or octal constants also unsigned types are allowed - but each one come after the corresponding signed one in the list)

所以,很显然,在这种情况下,恒一直PTED为长整型(或得到long long int 如果长整型太32位)。

So, it's clear that in that case the constant has been interpreted as a long int (or long long int if long int was too 32 bit).

请注意,大文字应导致编译错误:

Notice that "too big literals" should result in a compilation error:

是形成不良的程序,如果它的翻译单位之一,包含文字的整数,不能再以任何允许的类型psented $ P $。

A program is ill-formed if one of its translation units contains an integer literal that cannot be represented by any of the allowed types.

(ibidem,¶3)

(ibidem, ¶3)

这是迅速出现在这个样本 ,这提醒我们,ideone.com使用32位编译器。

which is promptly seen in this sample, that reminds us that ideone.com uses 32 bit compilers.

我现在看到的问题是关于C ...好,这或多或少是一样的:

I saw now that the question was about C... well, it's more or less the same:

C99,§6.4.4.1

C99, §6.4.4.1

类型的整型常量是第一个相应的列表中,它的价值可以重新presented的。

The type of an integer constant is the first of the corresponding list in which its value can be represented.

列表是一样的,在C ++标准。

list that is the same as in the C++ standard.

附录:既C99和C ++ 11允许也是文字是扩展整型(即其他特定于实现的整数类型),如果一切失败。 (C ++ 11,[lex.icon]¶3; C99,§6.4.4.1¶5表后)

Addendum: both C99 and C++11 allow also the literals to be of "extended integer types" (i.e. other implementation-specific integer types) if everything else fails. (C++11, [lex.icon] ¶3; C99, §6.4.4.1 ¶5 after the table)

这篇关于整数文字的类型默认情况下不诠释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆