473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Endianness of machine

Code to check endianness of machine

Feb 16 '06 #1
18 14010
friend.05 schrieb:
Code to check endianness of machine


You forgot to include your code.
Please make sure that it is a compiling piece of
code and explain your exact problem with it.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 16 '06 #2
#include <stdio.h>

int main()
{
long x = 0x34333231;
char *y = (char *) &x;

if(strncmp(y,"1234",4))
printf("Big Endian");
else
printf("little Endian");
}

Feb 16 '06 #3


friend.05 wrote:
Code to check endianness of machine

Its in C-FAQ

Check Q 20.9 at
http://www.eskimo.com/~scs/C-faq/top.html

- Ravi

Feb 16 '06 #4
"Sreekanth" <sr********************@gmail.com> writes:
#include <stdio.h>

int main()
{
long x = 0x34333231;
char *y = (char *) &x;

if(strncmp(y,"1234",4))
printf("Big Endian");
else
printf("little Endian");
}


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 16 '06 #5

Keith Thompson wrote:
"Sreekanth" <sr********************@gmail.com> writes:
#include <stdio.h>

int main()
{
long x = 0x34333231;
char *y = (char *) &x;

if(strncmp(y,"1234",4))
printf("Big Endian");
else
printf("little Endian");
}


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Sorry for the bad post. I am learning C programming been a java
programmer all my life. This is the first time I am using USENET group.
So that is not an excuse for a poor post. Anyway I will see to that it
doesnt repeat

Feb 16 '06 #6
Ico
friend.05 <hi**********@gmail.com> wrote:
Code to check endianness of machine


Usenet etiquette
--
:wq
^X^Cy^K^X^C^C^C^C
Feb 16 '06 #7
On Wed, 15 Feb 2006 21:42:15 -0800, Sreekanth wrote:
#include <stdio.h>

int main()
{
long x = 0x34333231;
char *y = (char *) &x;

if(strncmp(y,"1234",4))
printf("Big Endian");
else
printf("little Endian");
}


Rather too many assumptions here (as already pointed out). 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[b]);
printf("\n");
return 0;
}

Although there n! possible results on an n-byte architecture, sanity and
engineering mean that you are unlikely to come across more than 3.

--
Ben.

Feb 16 '06 #8
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Sreekanth" <sr********************@gmail.com> writes:
#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
I think 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.


Also, the OP forgot to include <string.h>
Feb 16 '06 #9
"Ben Bacarisse" <be********@bsb.me.uk> wrote in message
news:pa****************************@bsb.me.uk...
Endian-ness is
essentially the relationship between byte addressing and artimetic
significance so you can investigate it like this:
Yes, allow me some comments.
#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)
Is this void or am i missing something?
{
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[b]);
That will not print digits if sizeof(integer) exceeds 9.
printf("\n");
return 0;
}

Although there n! possible results on an n-byte architecture, sanity and
engineering mean that you are unlikely to come across more than 3.

Feb 16 '06 #10
On Thu, 16 Feb 2006 17:40:41 +0200, stathis gotsis wrote:
"Ben Bacarisse" <be********@bsb.me.uk> wrote in message
news:pa****************************@bsb.me.uk...

int main(avoid)


Is this void or am i missing something?


:-) Yes, void. It is my **** touchpad -- I need to have it switch off
when typing or I can insert random characters where the (mouse) cursor is
without noticing!
printf("%c", '0' + u.bytes[b]);


That will not print digits if sizeof(integer) exceeds 9.


Good point, thanks. Can't think now why I didn't just write:

printf("%d ", u.bytes[b]);

--
Ben.

Feb 16 '06 #11
stathis gotsis wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Sreekanth" <sr********************@gmail.com> writes:
#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


I think sizeof(long)>=4.


I know you are wrong. 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

<snip>
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
Feb 16 '06 #12
"stathis gotsis" <st***********@hotmail.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Sreekanth" <sr********************@gmail.com> writes:
> #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


I think sizeof(long)>=4.


If you're saying that's a language requirement, it isn't.
If CHAR_BIT > 8, sizeof(long) can be less than 4.

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 16 '06 #13
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:1e************@news.flash-gordon.me.uk...
stathis gotsis wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
The program makes the following unwarranted and unnecessary
assumptions:

ASCII character set

sizeof(long)==4
I think sizeof(long)>=4.


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.
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.
That is interesting. Can you provide more details about what implentation
that was?
So tell me, what is the endianness if
sizeof(long)==sizeof(int)==sizeof(short)==1


In how many ways can a 1-byte value be stored into memory? 1?
Feb 17 '06 #14
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"stathis gotsis" <st***********@hotmail.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Sreekanth" <sr********************@gmail.com> writes:
> #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


I think sizeof(long)>=4.

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.


Would it work if the above system were little-endian?
Feb 17 '06 #15
"stathis gotsis" <st***********@hotmail.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"stathis gotsis" <st***********@hotmail.com> writes:
> "Keith Thompson" <ks***@mib.org> wrote in message
> news:ln************@nuthaus.mib.org...
>> "Sreekanth" <sr********************@gmail.com> writes:
>> > #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
>
> I think sizeof(long)>=4.

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.


Would it work if the above system were little-endian?


It seems to (assuming an ASCII character set and all the other
implicit assumptions actually hold).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 17 '06 #16
stathis gotsis wrote:
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:1e************@news.flash-gordon.me.uk...
stathis gotsis wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
The program makes the following unwarranted and unnecessary
assumptions:

ASCII character set

sizeof(long)==4
I think sizeof(long)>=4.
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.
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.


That is interesting. Can you provide more details about what implentation
that was?


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.

These days I'm doing Linux and Windows development with lots fo 3rd
party libraries and not doing any DSP work.
So tell me, what is the endianness if
sizeof(long)==sizeof(int)==sizeof(short)==1


In how many ways can a 1-byte value be stored into memory? 1?


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
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
Feb 17 '06 #17
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:rf************@news.flash-gordon.me.uk...
stathis gotsis wrote:
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:1e************@news.flash-gordon.me.uk...
stathis gotsis wrote:
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.


That is interesting. Can you provide more details about what implentation
that was?


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.


Thank you for the extra information.
So tell me, what is the endianness if
sizeof(long)==sizeof(int)==sizeof(short)==1


In how many ways can a 1-byte value be stored into memory? 1?


Yes, but do you call it big-endian or little-endian? If you want to make
a portable endianness test you have to decide.


I do not know how it can be called since it conforms to both big and little
endian definitions (there is one single byte which can be regarded MSB or
LSB). Furthermore, i have not come up with a portable program that tests
endianness. At this point, i prefer to see the way bytes in a long are
stored into memory, and decide the endianness myself.
Feb 18 '06 #18
Flash Gordon <sp**@flash-gordon.me.uk> writes:
[...]
So tell me, what is the endianness if
sizeof(long)==sizeof(int)==sizeof(short)==1


What is the sound of one hand clapping?

Such a system has no defined endianness. A portable function to
determine endianness must allow for that possibility, just as it must
allow for exotic possibilites such as the PDP-11's "middle-endian"
representation.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 18 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Case | last post by:
#include <string.h> int i; /* 4-byte == 4-char */ char data = { 0x78, 0x56, 0x34, 0x12 }; int main() { memcpy(&i, data, 4); /*
15
by: T Koster | last post by:
Hi group, I'm having some difficulty figuring out the most portable way to read 24 bits from a file. This is related to a Base-64 encoding. The file is opened in binary mode, and I'm using...
7
by: bush | last post by:
hi folks I am new to this group.i need to know how i can find the endianness of my system.help me out.
2
by: SSM | last post by:
Hi, Does C standard comment about "Endianness" to be used to store a structure/union variables? Thanks & Regards, Mehta
72
by: gamehack | last post by:
Hi all, I was thinking today, suppose we have the number n = 0xAB 0xFF which is equivalent to 44031 in decimal. In big endian it will be stored as 10101011 11111111 but in little endian...
7
by: bob_jenkins | last post by:
When comparing strings, if a machine is big-endian, even if strings are not aligned, you can do some shifts then do word comparisons rather than byte-by-byte comparisons. Little-endian machines,...
18
by: Indian.croesus | last post by:
Hi, If I am right Endianness is CPU related. I do not know if the question is right in itself but if it is then how does C handle issues arising out of Endianness. I understand that if we pass...
5
by: Rahul | last post by:
Hi Everyone, I have a program unit which does >and << of an integer which is of 4 bytes length. The logic of shifting and action based on the result, assumes that the system is big-endian. ...
19
by: perry.yuan | last post by:
How could I determine the endianness of my compile environment at compile time, instead of run time? I need a macro ("some_expression"), i.e. #if some_expression #define TARGET_IS_LITTLE_ENDIAN...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.