Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C Determine endianess

Name: Anonymous 2007-06-27 10:43 ID:Uo4E/xRf

I wrote it and i edited wikipedias article which had a terrible code and added mine, but i'm still not sure about my code, is it SCALABLE ENOUGH?

#include <stdio.h>

int main() {

        union {
                int s;
                char c[sizeof(int)];
        } un;
        const char *endianess[[] = { "big endian", "little endian" };

        un.s = (~(int)0) ^ (~(char)0);
        puts(endianess[!un.c[0]]);

        return 0;
}

Name: Anonymous 2007-06-27 23:39 ID:K2AuVMv6

>>16
Lots of algorithms are byte-order-dependent, and moreover, they need to be compiled with different compilers on different systems with different endianness. Sure, many compilers identify endianness in some manner, but it's far from portable, and often the simplest route to take is to figure it out for yourself.

It might seem stupid to start with, but every once in a while, it proves to be an extremely handy tool to have strapped on your hacker belt.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List