fork download
  1. /******************************************************************************
  2.  
  3.   By Codeky.dev
  4.  
  5.   This code is to demonstrate the Endianness
  6.  
  7.   Explanation:
  8.   -----------
  9.  
  10.   The endianess is how the cpu arrange bytes in memory
  11.   in Big endian system, MSB is placed at lower address,
  12.   while in Little endian system, MSB is placed higher address.
  13.   So, if we have an unsigned integer: 0x00000001
  14.   it is represented in the memory as below in both systems
  15.  
  16.   | | byte[0] | byte[1] | byte[2] | byte[3] |
  17.   |----+---------+---------+---------+---------|
  18.   | BE | 00 | 00 | 00 | 01 |
  19.   | LE | 01 | 00 | 00 | 00 |
  20.  
  21.   If we have a char pointer point to that integer variable,
  22.   derefenced the pointer will be 00 in BE and 01 in LE
  23.  
  24.   That's it :)
  25.  
  26. *******************************************************************************/
  27. #include <stdio.h>
  28.  
  29. int isLittleEndian() {
  30. unsigned int x = 1;
  31. return *(char*) &x;
  32. }
  33.  
  34. int main()
  35. {
  36. if (isLittleEndian()) {
  37. printf("Little endian\n");
  38. } else {
  39. printf("Big endian\n");
  40. }
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Little endian