little endian or big endian ???

G

guthena

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

Walter Roberson

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

First, write a small document to describe precisely what
big-endian and little-endian -mean-. There are some systems
that are arguably both at the same time. In fact, there are some
systems that *are* both, with the "type" for any given executable
determined by a bit in one of the processor control words.
 
F

Flash Gordon

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.
 
E

Eric Sosman

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!
 
T

Tejas Kokje

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
 
M

Martin Ambuhl

guthena said:
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?
 
W

William Pursell

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

This is only portable if you allow information
to be passed in a compile time...(eg: -DBYTE_ORDER=4321...)


#include <stdio.h>

int
main(void)
{
#if BYTE_ORDER == LITTLE_ENDIAN
fputs( "little endian\n", stdout );
#elif BYTE_ORDER == BIG_ENDIAN
fputs( "big endian\n", stdout );
#else
fputs( "unknown \n", stdout );
#endif
return 0;
}
 
C

Chris Hills

Chris Dollin said:
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)
 
C

Chris Dollin

Chris said:
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.
 
R

Richard Bos

Tejas Kokje said:
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
 
C

Chris Hills

Chris Dollin said:
You missed my signature, yes?


No problem.


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

Who?
Why?
 
S

Spade

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* ?
 
R

Richard Tobin

[/QUOTE]
Can you tell me how it is flawed? Is it because of casting from int*
to char* ?

No. To determine endianness, you have to dereference the same address
as each of two different types, so a cast is quite natural. You could
do it with a union instead.

The main theoretical flaw is the assumption (perhaps implicit in the
question, though pedants will dispute this) that a system will be
either big or little endian. Though this is usually true, there have
been real machines in which it wasn't. A more thorough test would use
a value that put different values in each byte, and verify that the
were in one of the expected orders.

You also forgot to include <stdio.h>.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top