这个题目最早是在某个公司的笔试题中碰到的,当时尽然给做出来了,而且还是用指针。后来在网上找了一下,也有这个题目,是用联合的特性来解决的。在这就秀一下自己的方法!
#include
<
stdio.h
>
#include < stdlib.h >
int check_endian( void );
int main( void )
{
if (check_endian()) {
printf( " Little endian! " );
} else {
printf( " Big endian! " );
}
system( " PAUSE " );
return 0 ;
}
/*
****************************************************
Check the system's endian type, if little endian
return 1, if big endian return 0.
****************************************************
*/
int check_endian( void )
{
unsigned int test = 1 ;
return (( * ((unsigned char * ) & test) == test) ? 1 : 0 );
}
#include < stdlib.h >
int check_endian( void );
int main( void )
{
if (check_endian()) {
printf( " Little endian! " );
} else {
printf( " Big endian! " );
}
system( " PAUSE " );
return 0 ;
}
/*
****************************************************
Check the system's endian type, if little endian
return 1, if big endian return 0.
****************************************************
*/
int check_endian( void )
{
unsigned int test = 1 ;
return (( * ((unsigned char * ) & test) == test) ? 1 : 0 );
}