Consider the following code :
unsigned char byte = 0x01;
In C/C++
the hexadecimal will be considered as an int
, and therefore will be expanded to more then one byte. Because there is more then one byte, if the system uses little or big endian has effect. My question is to what this will be expanded?
If the system has int
that contains 4 bytes, to which of the following it will be expanded :
0x00000001 OR 0x01000000
And does the endianness effect to which of them it will be expanded?
It is completely architecture specific. The compiler produces the output according to your computer's endianness. Assuming that your microprocessor is big-endian, the compiler will produce;
char x = 0x01; --> 01,
int x = 0x01; --> 00 00 00 01
On the other hand, if you use a cross compiler that targets little-endian or you compile the code on a little-endian machine, it will produce;
char x = 0x01; --> 01
int x = 0x01; --> 01 00 00 00
Summarily, there is no standard or default endianness for the output, the output will be specific to the architecture which the compiler targets to.