字節序測試

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <strings.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
int main(int argc, char **argv)
{
 union
 {
  short s;
  char c[sizeof(short)];
 } un;socket

 un.s = 0x0102;
// printf("%s: ", CPU_VENDOR_OS);
 if (sizeof(short) == 2)
 {
  if (un.c[0] == 1 && un.c[1] == 2)
   printf("big-endian\n");
  else if (un.c[0] == 2 && un.c[1] == 1)
   printf("little-endian\n");
  else
   printf("unknown\n");
 } else
  printf("sizeof(short) = %d\n", sizeof(short));string

 exit(0);
}it

相關文章
相關標籤/搜索