Homepage › Forums › Articles › Programming › C, C++, and C# › C Code Examples: Return System Endianness
Tagged: c-language, code, endianness, example, programming, sample
This topic was published by DevynCJohnson and viewed 1641 times since "". The last page revision was "".
Viewing 1 post (of 1 total)
- AuthorPosts
const char *endianness(void) { // Return the system's endianness #ifdef __BYTE_ORDER__ # if (__ORDER_LITTLE_ENDIAN__ == __BYTE_ORDER__) return "ENDIAN_LITTLE"; # elif (__ORDER_BIG_ENDIAN__ == __BYTE_ORDER__) return "ENDIAN_BIG"; # elif (__ORDER_PDP_ENDIAN__ == __BYTE_ORDER__) return "ENDIAN_PDP"; #endif # else union { uint32_t read32_value; uint8_t data[sizeof(uint32_t)]; } number; number.data[0] = 0x00; number.data[1] = 0x01; number.data[2] = 0x02; number.data[3] = 0x03; switch (number.read32_value) { case UINT32_C(0x00010203): return "ENDIAN_BIG"; case UINT32_C(0x03020100): return "ENDIAN_LITTLE"; case UINT32_C(0x02030001): return "ENDIAN_BIG_WORD"; case UINT32_C(0x01000302): return "ENDIAN_LITTLE_WORD"; default: return "ENDIAN_UNKNOWN"; } # endif }
Further Reading
- Programming and Development - https://dcjtech.info/topic/programming-and-development/
- C Programming Article Forum - https://dcjtech.info/forum/articles/programming/c-c-and-c/
- C Code Examples: Array Rotations - https://dcjtech.info/topic/c-code-examples-array-rotations/
- C Code Examples: File IO Tests - https://dcjtech.info/topic/c-code-examples-file-io-tests/
- C Code Examples: Stack and Queue Datatype - https://dcjtech.info/topic/c-code-examples-stack-and-queue-datatype/
- 8051 Microcontroller and Assembly - https://dcjtech.info/topic/8051-microcontroller-and-assembly/
- Embedding Files in C Source Code - https://dcjtech.info/topic/embedding-files-in-c-source-code/
- AuthorPosts
Viewing 1 post (of 1 total)