Is it safe to detect endianess with union?
In other words, according to the C standard , is this code safe? (Assume uint8_t is one byte) void detectEndianness(void){ union { uint16_t w; uint8_t b; } a; a.w = 0x00FFU; if (a.b == 0xFFU) { puts("Little endian."); } else if (a.b == 0U) { puts("Big endian."); } else { puts("Stack Overflow endian."); } } What if I change it into this? Note the third if case that I'm aware of. a.w = 1U; if (a.b == 1U) { puts("Little endian."); } else if (a.b == 0U) { puts ("Big endian."); } else if (a.b == 0x80U) { /* Special potential */ } else { puts("Stack Overflow endian."); } Quoting from n1570: 6.5.2.3