staticassert.h
#ifndef _static_assert_h_
#define _static_assert_h_
template <bool> struct CstaticAssert ;
template <> struct CstaticAssert<true> {} ;
#define StaticAssert(exp) (void)sizeof(CstaticAssert<(exp) != 0>)
#endif
main.cpp
#include <stdio.h>
#include "staticAssert.h"
static const int _ONE_ = 1 ;
#define isLittleEndianCPU() (\
StaticAssert(sizeof(char) < sizeof(int)),\
*((char*)(&_ONE_)) != 0\
)
int main(void){
if (isLittleEndianCPU()){
printf("Little Endian.\n") ;
}else{
printf("Big Endian.\n") ;
}
return 0 ;
}