469,352 Members | 1,668 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

little endian or big endian ???

Write a small C program to determine whether a machine's type is
little-endian or big-endian.

Jun 28 '07 #1
23 13899
On Jun 28, 12:01 pm, guthena <r4r...@gmail.comwrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
It's a FAQ.

Jun 28 '07 #2
guthena wrote, On 28/06/07 20:01:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
Save yourself the trouble and use a machine where char, short, int and
long are all the same size.

Alternatively, do your own homework. Once you've posted your attempt
someone might help you with it.
--
Flash Gordon
Jun 28 '07 #3
In article <11*********************@j4g2000prf.googlegroups.c om>,
guthena <r4****@gmail.comwrites
>Write a small C program to determine whether a machine's type is
little-endian or big-endian.
When does this homework have to be completed by?

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jun 28 '07 #4
guthena wrote On 06/28/07 15:01,:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
This small C program handles other possibilities, too:

#include <stdio.h>
int main(void){int q=puts("Enter 1 for big-endian or"
" 2 for little-endian: ");fflush(0);switch(q=scanf(
"%d",&q)>0?q:0){defualt:puts("mixed-endian");break;
case 1:puts("big-endian");break;defau1t:puts("other"
"-endian");break;case 2:puts("little-endian");break;
default:puts("anti-endian");break;}return q?!q:q;}

Enjoy!

--
Er*********@sun.com
Jun 28 '07 #5
"guthena" writes:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
Take a look at this.
http://en.wikipedia.org/wiki/Big_endian
Jun 28 '07 #6
guthena <r4****@gmail.comwrites:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
Why?

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 28 '07 #7
On Jun 28, 12:01 pm, guthena <r4r...@gmail.comwrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
Here you go..

int main()
{
int num=1;
char *cptr;

cptr = (char *)&num;

if (*cptr)
printf ("little endian\n");
else
printf ("big endian\n");

return 0;
}

Which company interview question is this ? :-D

Tejas Kokje

Jun 28 '07 #8
guthena wrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
No, thank you. Why are you giving out assignments?
Jun 28 '07 #9
guthena wrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
OK, I've done that.

--
Chris "it'ss /mine/, preciousss" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 29 '07 #10
In article <f6**********@murdoch.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrites
>guthena wrote:
>Write a small C program to determine whether a machine's type is
little-endian or big-endian.

OK, I've done that.
To save time please put Guthena's name at the top and email it direct
to his lecturer.

Further to save time can I email my homework direct to you in the
future?

Chris (The other one)

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jun 29 '07 #11
On 28 Jun, 20:01, guthena <r4r...@gmail.comwrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
No

Jun 29 '07 #12
Chris Hills wrote:
In article <f6**********@murdoch.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrites
>>guthena wrote:
>>Write a small C program to determine whether a machine's type is
little-endian or big-endian.

OK, I've done that.
To save time please put Guthena's name at the top and email it direct
to his lecturer.
You missed my signature, yes?
Further to save time can I email my homework direct to you in the
future?
No problem.
Chris (The other one)
I thought you were the /other/ other one. /The/ Chris, of course, being
Chris Torek.

--
Chris "not Hilly, but definitely not flat" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jun 29 '07 #13
Tejas Kokje <bi*************@gmail.comwrote:
On Jun 28, 12:01 pm, guthena <r4r...@gmail.comwrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.

Here you go..

int main()
{
int num=1;
char *cptr;

cptr = (char *)&num;

if (*cptr)
printf ("little endian\n");
else
printf ("big endian\n");

return 0;
}

Which company interview question is this ? :-D
Congratulations. You just did his homework for him. Since it's a crappy
assignment, his teacher probably thinks he did a sterling job, he'll
probably pass the course on the back of your work, and he'll end up
holding a job as a programmer _still_ thinking that this is a good
program. Which it is not; it is flawed for several reasons. I presume
_you_ will be the one to clean up after his incompetent behind, not us?

Richard
Jun 29 '07 #14
In article <f6**********@murdoch.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrites
>Chris Hills wrote:
>In article <f6**********@murdoch.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrites
>>>guthena wrote:

Write a small C program to determine whether a machine's type is
little-endian or big-endian.

OK, I've done that.
To save time please put Guthena's name at the top and email it direct
to his lecturer.

You missed my signature, yes?
>Further to save time can I email my homework direct to you in the
future?

No problem.
>Chris (The other one)

I thought you were the /other/ other one. /The/ Chris, of course, being
Chris Torek.
Who?
Why?

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Jun 29 '07 #15
On Jun 29, 3:22 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Tejas Kokje <binarysemaph...@gmail.comwrote:
On Jun 28, 12:01 pm, guthena <r4r...@gmail.comwrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
Here you go..
int main()
{
int num=1;
char *cptr;
cptr = (char *)&num;
if (*cptr)
printf ("little endian\n");
else
printf ("big endian\n");
return 0;
}
Which company interview question is this ? :-D

Congratulations. You just did his homework for him. Since it's a crappy
assignment,
This most probably isn't a homework question. Its a typical question
thats asked mostly in interviews in India.
his teacher probably thinks he did a sterling job, he'll
probably pass the course on the back of your work, and he'll end up
holding a job as a programmer _still_ thinking that this is a good
program. Which it is not; it is flawed for several reasons.
Can you tell me how it is flawed? Is it because of casting from int*
to char* ?
Jun 29 '07 #16
Chris Hills wrote:
In article <f6**********@murdoch.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrites
>>I thought you were the /other/ other one. /The/ Chris, of course, being
Chris Torek.

Who?
You're /surely/ not serious.
Why?
You're /surely/ not serious.

--
Chris "!?" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 29 '07 #17
In article <f6**********@pc-news.cogsci.ed.ac.uk>,
Richard Tobin <ri*****@cogsci.ed.ac.ukwrote:
>*Any* test of endianness is bound to use implementation-defined or
undefined behaviour, otherwise all machines would be found to have the
same endianness!
Take an unsigned integral type. C promises no internal padding
and no trap representations. Initialize it. Take its address and cast the
address to unsigned char pointer. C defines this transformation
as being possible and that the resulting unsigned chars will
each have no internal non-value bits and no trap representations.
Now read through the unsigned char array. If the sizeof the
unsigned integral type exceeds the number of bits you initialized
to, then the value bits may be spread out in unusual ways,
most likely by all-0 unsigned char showing up, but perhaps more
complicated patterns are possible? (I'd have to go back and
re-check the values implementation clause.) But you could track
each bit by shifting it through the unsigned integral type and
seeing where it shows up in the unsigned char array.

Tracking down non-value bits and sign representation for signed
integral types -- that's harder.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Jun 29 '07 #18
Chris Dollin wrote:
Chris Hills wrote:
>In article <f6**********@murdoch.hpl.hp.com>, Chris Dollin
<ch**********@hp.comwrites
>>I thought you were the /other/ other one. /The/ Chris, of course, being
Chris Torek.
Who?

You're /surely/ not serious.
>Why?

You're /surely/ not serious.
He is. And stop calling him Shirley.
--
clvrmnky <mailto:sp******@clevermonkey.org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
Jun 29 '07 #19
Spade wrote:
>
.... snip ...
>
Can you tell me how it is flawed? Is it because of casting from
int* to char* ?
For example, with a 32 bit int, you have 32 places for the most
sig. bit, 31 for the next bit, etc. for 32! total possibilities.
Yes, some combinations are more likely, but that isn't detecting,
it is discriminating.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 29 '07 #20
Walter Roberson wrote:
>
.... snip ...
>
Take an unsigned integral type. C promises no internal padding
and no trap representations. Initialize it. Take its address and
cast the address to unsigned char pointer. C defines this
transformation as being possible and that the resulting unsigned
chars will each have no internal non-value bits and no trap
representations. ....
Nonsense.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 29 '07 #21
In article <46***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>Walter Roberson wrote:
>Take an unsigned integral type. C promises no internal padding
and no trap representations. Initialize it. Take its address and
cast the address to unsigned char pointer. C defines this
transformation as being possible and that the resulting unsigned
chars will each have no internal non-value bits and no trap
representations. ....
>Nonsense.
Hmm? Which part is nonsense? Is it the part of C89 3.3 Expressions
that says, "An object shall have its stored value accessed only
by an lvalue that has one of the following types:
[...] - a character type" ?
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Jun 29 '07 #22
Walter Roberson wrote:
CBFalconer <cb********@maineline.netwrote:
>Walter Roberson wrote:
>>Take an unsigned integral type. C promises no internal padding
and no trap representations. Initialize it. Take its address and
cast the address to unsigned char pointer. C defines this
transformation as being possible and that the resulting unsigned
chars will each have no internal non-value bits and no trap
representations. ....
>Nonsense.

Hmm? Which part is nonsense? Is it the part of C89 3.3 Expressions
that says, "An object shall have its stored value accessed only
by an lvalue that has one of the following types:
[...] - a character type" ?
I think the following quote (from N869) is adequate:

6.2.6.2 Integer types

[#1] For unsigned integer types other than unsigned char,
the bits of the object representation shall be divided into
two groups: value bits and padding bits (there need not be
any of the latter). If there are N value bits, each bit
shall represent a different power of 2 between 1 and 2N-1,
so that objects of that type shall be capable of
representing values from 0 to 2N-1 using a pure binary
representation; this shall be known as the value
representation. The values of any padding bits are
unspecified.39)

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Jun 29 '07 #23
Spade <st***********@gmail.comwrote:
On Jun 29, 3:22 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Tejas Kokje <binarysemaph...@gmail.comwrote:
On Jun 28, 12:01 pm, guthena <r4r...@gmail.comwrote:
Write a small C program to determine whether a machine's type is
little-endian or big-endian.
Here you go..
int main()
{
int num=1;
char *cptr;
cptr = (char *)&num;
if (*cptr)
printf ("little endian\n");
else
printf ("big endian\n");
return 0;
}
Which company interview question is this ? :-D
Congratulations. You just did his homework for him. Since it's a crappy
assignment,

This most probably isn't a homework question. Its a typical question
thats asked mostly in interviews in India.
It's also a typical question that's asked in many badly thought out
computing courses.
his teacher probably thinks he did a sterling job, he'll
probably pass the course on the back of your work, and he'll end up
holding a job as a programmer _still_ thinking that this is a good
program. Which it is not; it is flawed for several reasons.

Can you tell me how it is flawed?
I can, but I won't. The whole exercise is a wild goose chase, and I'm
not going to help you by telling you which other wild species of goose
you could chase. That would only make a futile pursuit differently
futile, not less.

Richard
Jul 3 '07 #24

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by hicham | last post: by
8 posts views Thread by Perception | last post: by
2 posts views Thread by bhatia | last post: by
5 posts views Thread by manishster | last post: by
23 posts views Thread by Niranjan | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.