L&QUOT的温度间$ P $的十六进制长整型常量&QUOT ptation; [英] C interpretation of hexadecimal long integer literal "L"
问题描述
如何做一个C编译器跨preT的L,表示一个长整型常量,在光的自动转换?下面code,在32位平台上运行时(32位长,64位长的长),似乎蒙上前pression(0xffffffffL)进入64位整数4294967295,没有32位的-1。
样品code:
的#include< stdio.h中>
INT主要(无效)
{
很长很长X = 10;
长长的Y =(0xffffffffL);
很长很长Z =(长)(0xffffffffL);
的printf(长长×==%LLD \ N,X);
的printf(长大Y ==%LLD \ N,Y);
的printf(很久很久ž==%LLD \ N,Z);
的printf(0xffffffffL ==%LD \ N,0xffffffffL);
如果(X>(长)(0xffffffffL))
的printf(X>(长)(0xffffffffL)的\ n);
其他
的printf(X< =(长)(0xffffffffL)的\ n);
如果(X>(0xffffffffL))
的printf(X>(0xffffffffL)的\ n);
其他
的printf(X< =(0xffffffffL)的\ n);
返回0;
}
输出(用GCC 4.5.3编译在32位的Debian):
长的长×== 10
久大Y == 4294967295
长长ž== -1
0xffffffffL == -1
X - GT; (长)(0xffffffffL)
X - LT; =(0xffffffffL)
这是一个十六进制的文字,所以它的类型可以是无符号。它适合于无符号长
,所以这类型的就愈大。见标准第6.4.4.1:
的整数常量的类型是第一相应列表的在其值可以 重新presented。
在这里有一个后缀为十六进制的文字列表→
是
-
长
-
无符号长
-
很长很长
-
无符号长长
由于它不适合在32位有符号长
,而是一个无符号32位无符号长
,这就是它变得。
How does a C compiler interpret the "L" which denotes a long integer literal, in light of automatic conversion? The following code, when run on a 32-bit platform (32-bit long, 64-bit long long), seems to cast the expression "(0xffffffffL)" into the 64-bit integer 4294967295, not 32-bit -1.
Sample code:
#include <stdio.h>
int main(void)
{
long long x = 10;
long long y = (0xffffffffL);
long long z = (long)(0xffffffffL);
printf("long long x == %lld\n", x);
printf("long long y == %lld\n", y);
printf("long long z == %lld\n", z);
printf("0xffffffffL == %ld\n", 0xffffffffL);
if (x > (long)(0xffffffffL))
printf("x > (long)(0xffffffffL)\n");
else
printf("x <= (long)(0xffffffffL)\n");
if (x > (0xffffffffL))
printf("x > (0xffffffffL)\n");
else
printf("x <= (0xffffffffL)\n");
return 0;
}
Output (compiled with GCC 4.5.3 on a 32-bit Debian):
long long x == 10
long long y == 4294967295
long long z == -1
0xffffffffL == -1
x > (long)(0xffffffffL)
x <= (0xffffffffL)
It's a hexadecimal literal, so its type can be unsigned. It fits in unsigned long
, so that's the type it gets. See section 6.4.4.1 of the standard:
The type of an integer constant is the first of the corresponding list in which its value can be represented.
where the list for hexadecimal literals with a suffix L
is
long
unsigned long
long long
unsigned long long
Since it doesn't fit in a 32-bit signed long
, but an unsigned 32-bit unsigned long
, that's what it becomes.
这篇关于L&QUOT的温度间$ P $的十六进制长整型常量&QUOT ptation;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!