如何判断机器的大小端的问题

来源:互联网 发布:网络实名制的国家 编辑:程序博客网 时间:2024/05/01 22:17

如何判断机器的大小端的问题,在阅读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;

}

原创粉丝点击