0

I'm looking forward for a way whereby I can find the endianness without implicit/explicit casting, loops, switch, inbuilt functions, macros. I have tried a code but it uses explicit casting.

int is_little_endian(){
    int temp = 1;
    return *(char *) temp;//Returns 1 if it's a little endian machine.
}

[edit]

It has to be done without use of any casting. I couldn't find a question whereby we can do it without casting and use of macros. Implicit Casting herein in refers to automatic casting by compiler. Say from char to int.

Siddharth Kamaria
  • 2,448
  • 2
  • 17
  • 37
  • possible duplicate of [C Macro definition to determine big endian or little endian machine?](http://stackoverflow.com/questions/2100331/c-macro-definition-to-determine-big-endian-or-little-endian-machine) – 2501 Jan 16 '15 at 16:06
  • `*(char *) temp;` --> `*(char *) &temp;` – BLUEPIXY Jan 16 '15 at 16:07
  • 1
    It's unlikely that this hasn't been asked before. Not using a cast usually uses a `union` instead: `int is_little_endian(void) { union endianness { char c[2]; short s; } u; u.s = 1; return (u.c[0] == 1); }`. – Jonathan Leffler Jan 16 '15 at 16:10
  • 1
    I don't think this is a duplicate. The important new content here is that OP is asking to do this without "casting", which presumably means "without accessing the representation of an object", and I don't think that's been addressed before. – R.. GitHub STOP HELPING ICE Jan 16 '15 at 16:10
  • possible duplicate of http://stackoverflow.com/questions/4239993/determining-endianness-at-compile-time @R.. It has been. – 2501 Jan 16 '15 at 16:13
  • @sidkamaria It has been answered in the above link. – 2501 Jan 16 '15 at 16:18
  • 1
    @2501: Where is the key constraint in that question? The one you cited as duplicate is about practical compile-time ways to get an answer; macros in system headers are an admissible answer. This question here is asking about how to actually measure the endianness, not necessarily at compiletime, but with certain constraints on the language features used. – R.. GitHub STOP HELPING ICE Jan 16 '15 at 16:22
  • @R.. Measure? No. It is merely asking *I can find the endianness without...( conditions )*. The conditions don't make much sense, since they forbid pretty much everything, but there you go. It has been answered many many times. – 2501 Jan 16 '15 at 16:25
  • IMO, the best way to code ensures that endianness isn't an issue. You know the data format that's being written or read, and you use the same code to write the actual data correctly for that external format — whether that's the network or some file format. – Jonathan Leffler Jan 16 '15 at 16:26

3 Answers3

5

There's no such thing as "implicit casting" - a cast is an explicit operator. If what you're looking for is a way to observe endianness without examining the representation of an object, however, there is no way. Endianness is purely a property of representation, not of values, so if you restrict yourself from accessing representations, it doesn't exist. This is why most good code should not care about endianness.

If you do want to cheat and access representations, but without it looking like a cast, here are some ideas:

int i = 1;
char c;
memcpy(&c, &i, 1);
c;

or:

union { int i; char c; } x = { 1 };
x.c;

or:

int i = 1;
FILE *f = tmpfile();
fwrite(&i, sizeof i, 1, f);
rewind(f);
getc(f);

or using any library function that might do stuff like this for you under the hood. You could also, if you're on a POSIX system or any other system with socket operations, do:

htonl(1) != 1;

But all of these rely either on inspecting representations, or hard-coded assumptions in the implementation about what the target endianness is.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
2

Why not try

unsigned int i= 1;
char *p = &i;

if(*p)
    printf("Little endian\n");
else
    printf("Big endian\n");
pm100
  • 48,078
  • 23
  • 82
  • 145
Gopi
  • 19,784
  • 4
  • 24
  • 36
1

This is something that is done via the compiler itself.

int is_little_endian(void) {
#ifdef _LITTLE_ENDIAN
    return 1;
#else
    return 0;
#endif
}

When the program is built for a little endian target, define _LITTLE_ENDIAN in the build command (typically by adding -D_LITTLE_ENDIAN). If the program is built for a big endian target, define _BIG_ENDIAN in the build command (-D_BIG_ENDIAN).

The above example will let you know at run time whether it was built for a little endian target or not.

However, as the endianness is unlikely to change at runtime, I don't see much value in doing this at runtime. Instead leverage the C preprocessor and the ability to pass in defines via the command line to accomplish what you need.

Hope this helps.

Sparky
  • 13,505
  • 4
  • 26
  • 27