F
friend.05
Code to check endianness of machine
friend.05 said:Code to check endianness of machine
Its in C-FAQfriend.05 said:Code to check endianness of machine
Sreekanth said:#include <stdio.h>
int main()
{
long x = 0x34333231;
char *y = (char *) &x;
if(strncmp(y,"1234",4))
printf("Big Endian");
else
printf("little Endian");
}
Keith said:Please provide context, even if the previous post was only one line.
You *have* read <http://cfaj.freeshell.org/google/>, haven't you?
The program makes the following unwarranted and unnecessary
assumptions:
ASCII character set
sizeof(long)==4
Big-endian and little-endian are the only possibilities
Output will appear even without a new-line
It's acceptable to fall off the end of main() without returning a
value (true in C99, but still poor style)
And a minor point: "int main()" is acceptable, but the more explicit
"int main(void)" is preferred.
friend.05 said:Code to check endianness of machine
#include <stdio.h>
int main()
{
long x = 0x34333231;
char *y = (char *) &x;
if(strncmp(y,"1234",4))
printf("Big Endian");
else
printf("little Endian");
}
The program makes the following unwarranted and unnecessary
assumptions:
ASCII character set
sizeof(long)==4
Big-endian and little-endian are the only possibilities
Output will appear even without a new-line
It's acceptable to fall off the end of main() without returning a
value (true in C99, but still poor style)
And a minor point: "int main()" is acceptable, but the more explicit
"int main(void)" is preferred.
Ben Bacarisse said:Endian-ness is
essentially the relationship between byte addressing and artimetic
significance so you can investigate it like this:
#include <stdio.h>
/* Edit this if you have an old compiler: */
typedef unsigned long long int integer;
union {
integer number;
char bytes[sizeof(integer)];
} u;
int main(avoid)
{
int b;
u.number = (integer)0;
/* assign 1 to LSB, 2 to the next and so on... *.
for (b = 0; b < sizeof(integer); b++)
u.number |= (integer)(b + 1) << (8 * b);
/* convert byte numbers to digits and print in address order */
for (b = 0; b < sizeof(integer); b++)
printf("%c", '0' + u.bytes);
Is this void or am i missing something?
printf("%c", '0' + u.bytes);
That will not print digits if sizeof(integer) exceeds 9.
stathis said:I think sizeof(long)>=4.
stathis gotsis said:I think sizeof(long)>=4.
I've used a conforming implementation where
sizeof(long)==2 and sizeof(int)==1. It had 16 bit bytes. I have hear of
others with 24 bit bytes and 32 bit bytes.
So tell me, what is the endianness if
sizeof(long)==sizeof(int)==sizeof(short)==1
If you're saying that the program merely assumes that sizeof(long)>=4,
consider what happens on a big-endian system with CHAR_BIT==8 and
sizeof(long)==8. The bytes composing the long value would then be
(0x00, 0x00, 0x00, 0x00, 0x34, 0x33, 0x32, 0x31). The program happens
to print "Big Endian" in that case, but only because the bugs cancel
each other out.
stathis gotsis said:Would it work if the above system were little-endian?
stathis said:I meant that the program makes the assumption that sizeof(long)>=4, as
opposed to the assumption that sizeof(long)==4. Yes, sizeof(long)>=4 is not
necessary.
That is interesting. Can you provide more details about what implentation
that was?
In how many ways can a 1-byte value be stored into memory? 1?
Flash Gordon said:The implementation I used with sizeof(long)==2 and sizeof(int)==1 with
CHAR_BIT==16 was for the TMS320c2x DSP processor. The one I can remember
hearing of with CHAR_BIT==24 was a Motorola DSP. The implementations
I've heard of with CHAR_BIT==32 are also DSPs.
Yes, but do you call it big-endian or little-endian? If you want to make
a portable endianness test you have to decide.
Flash Gordon said:So tell me, what is the endianness if
sizeof(long)==sizeof(int)==sizeof(short)==1
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.