如何判断机器的大小端的问题,在阅读ORBacus的代码中看到的,特此载出
1.
int
main ()
{
/* Are we little or big endian? From Harbison&Steele. */
union
{
long l;
char c[sizeof (long)];
} u;
u.l = 1;
exit (u.c[sizeof (long) - 1] == 1);
}
2.
#include <sys/types.h>
#include <sys/param.h>
int
main ()
{
#if BYTE_ORDER != BIG_ENDIAN
not big endian
#endif
;
return 0;
}
3.
#include <sys/types.h>
#include <sys/param.h>
int
main ()
{
#if !BYTE_ORDER || !BIG_ENDIAN || !LITTLE_ENDIAN
bogus endian macros
#endif
;
return 0;
}
博客分享了在阅读ORBacus代码时获取的判断机器大小端的方法。给出了三种C语言代码示例,第一种通过联合体判断,后两种利用系统头文件中的宏定义进行判断,为解决机器大小端判断问题提供了思路。
609

被折叠的 条评论
为什么被折叠?



