【发布时间】:2016-05-09 14:50:04
【问题描述】:
谁能告诉我这个程序如何确定编译器是小端还是大端。
#include <stdio.h>
#include <sys/types.h>
int main(void) {
union {
long lungo;
char ch[sizeof(long)];
} unione;
unione.lungo = 1;
if (unione.ch[sizeof(long)-1] == 0)
printf("little endian\n");
else
printf("big endian\n");
return (0);
}
特别是我不明白这部分程序的作用:
union {
long lungo;
char ch[sizeof(long)];
} unione;
谢谢
【问题讨论】:
-
@SandBag_1996:通过工会为
char起别名是合法的。 -
请使用 C 书籍了解
union是什么。这是你的主要问题。 -
"little or big endian"` 是 2 种常见的 endian - 其他的存在,但往往很少见。
-
@SandBag_1996:两者是不同语言的另一个原因。
-
@chux:大端编译器可以很好地为小端目标生成代码。这对于交叉编译器来说是典型的。编译器的字节序对翻译后的程序代码无关紧要,而与目标的字节序无关。所以他是非常正确的(也许我们中的一个人误解了他的意思?)。
标签: c endianness