determining endian ?

Randal Schwartz merlyn at iwarp.intel.com
Thu Nov 30 03:29:54 AEST 1989


In article <858 at rna.UUCP>, dan at rna (Dan Ts'o) writes:
| 	I'm sure this is old hat, but I recently needed to figure out if a
| particular host machine was big-endian or little endian. Comments, please,
| on this little snippet of code.
[code deleted]

Well, Larry Wall uses the following piece of code in his marvelous
Configure script (created with metaConfigure, of course).  The output
of the script gives the byte order as an integer, little-endian ending
up as 1234, big endian as 4321, and so on.

-------------------------------------------------- cut here
#include <stdio.h>
main()
{
    int i;
    union {
	unsigned long l;
	char c[sizeof(long)];
    } u;

    if (sizeof(long) > 4)
	u.l = 0x0807060504030201;
    else
	u.l = 0x04030201;
    for (i=0; i < sizeof(long); i++)
	printf("%c",u.c[i]+'0');
    printf("\n");
}
-------------------------------------------------- cut here

You could probably build your thing on top of this.

Just another C hacker,
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn at iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/



More information about the Comp.lang.c mailing list