0

If I debug this C code:

unsigned int i = 0xFF;

Will the debugger show me in memory 0x00FF if it's a little endian system and 0xFF00 if it's a big endian system?

Mat
  • 202,337
  • 40
  • 393
  • 406
user1091856
  • 3,032
  • 6
  • 31
  • 42

2 Answers2

2

If you view the raw contents of memory around the address &i then yes, you will be able to tell the difference. But it's going to be 00 00 00 ff for big endian and ff 00 00 00 for little endian, assuming sizeof(unsigned int) is 4 and CHAR_BIT (number of bits per byte) is 8 -- in the question you have them swapped.

You can also detect endianness programmatically (i.e. you can make a program that prints out the endianness of the architecture it runs on) through one of several tricks; for example, see Detecting endianness programmatically in a C++ program (solutions apply to C too).

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • no, the C standard doesn't specify the size of an int, so it can vary between different systems. – Vicky May 04 '12 at 09:52
  • @user1091856: No, it's *at least* 2 bytes. – Jon May 04 '12 at 09:52
  • @Jon: no, `int` is at least `16 bits`, not `2 bytes`. A byte in C can be bigger than 8 bits. A typical example of such an implementation would be C for TI's DSPs such as TMS320C54xx (in there `char`=`short`=`int`=16 bits=single addressable unit of memory). – Alexey Frunze May 04 '12 at 10:04
  • @Alex: Agreed, but I don't think the difference is something the OP needs to be force-fed in this context because it dilutes the essence of the answer to this question. – Jon May 04 '12 at 10:08
  • @Jon: I think when programming in C one has to make a clear distinction between a `C byte` and an `8-bit byte` to avoid undue confusion and bugs. For this very reason in a number of places the word `byte` is either defined or replaced by the word `octet`. – Alexey Frunze May 04 '12 at 10:12
  • @Alex: Again, I agree. But you don't teach someone pointer arithmetic the same day you teach them "Hello world". There are innumerable amateur (and even "professional") C programmers who have no idea about `CHAR_BIT` or even the size limits of `int`, and yet they do manage to write programs. – Jon May 04 '12 at 10:14
  • @Jon: We haven't got there yet. :) – Alexey Frunze May 04 '12 at 10:15
2

If:

  • Your system has 8-bit chars.
  • Your system uses 16-bit unsigned ints
  • And your debugger displays memory as simply bytes of hex

You would see this at &i if it's little-endian:

ff 00 ?? ?? ?? ?? ?? ??

and this if it's big-endian:

00 ff ?? ?? ?? ?? ?? ??

The question marks simply represent the bytes following i in memory.

Note that:

  • A little-endian machine will store the byte with the lowest value at the lowest address.
  • A big-endian machine will store the byte with the largest value at the lowest address.
  • C doesn't specify how many bits are in an unsigned int. It's up to the implementation.
  • You can find out using CHAR_BIT * sizeof (unsigned int).

If instead your machine uses 32-bit unsigned char, you would see:

ff 00 00 00 ?? ?? ?? ??

in the little-endian case, and on a big-endian machine you would see:

00 00 00 ff ?? ?? ?? ??
unwind
  • 391,730
  • 64
  • 469
  • 606