The following program decides the byte order of a given computer by verifying th
ID: 3633849 • Letter: T
Question
The following program decides the byte order of a given computer by verifying the byte order of a short integer. Modify that program so that it will decide the byte order of a computer by checking the byte order of a long integer on department Linux workstations. Run your program on at least two Linux workstations to see their byte orders.1 #include "unp.h"
2 int
3 main(int argc, char **argv)
4 {
5 union {
6 short s;
7 char c[sizeof(short)];
8 } un;
9 un.s = 0x0102;
10 printf("%s: ", CPU_VENDOR_OS);
11 if (sizeof(short) == 2) {
12 if (un.c[0] == 1 && un.c[1] == 2)
13 printf("big-endian ");
14 else if (un.c[0] == 2 && un.c[1] == 1)
15 printf("little-endian ");
16 else
17 printf("unknown ");
18 } else
19 printf("sizeof(short) = %d ", sizeof(short));
20 exit(0);
21 }
Explanation / Answer
Dear.... #include int test() { union ] { short s; char c[sizeof(short)]; } un; un.s = 0x0102; if (sizeof(short) = =2) if (sizeof(short)==2) { if (un.c[0] ==1 && un.c[1]==2) printf("big endian "); else if (un.c[0] ==2 && un.c[1]==1) printf("little endian "); else printf("unknown "); } else { printf("sizeof(short) = %d", sizeof(short)); if (sizeof(short)!=2) { return sizeof(short); } if (un.c[0] ==1 && un.c[1]==2) { return -1; } if (un.c[0] ==2 && un.c[1]==1) { return -2; } return -3; } int main(int argc, char * argv[]) { int t=test(); switch ( t ) { case -3: { printf("big endian "); break; } case -2: { printf("little endian "); break; } case -1: { printf("unknown "); break; } default: { printf("sizeof(short) = %d", t); } } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.