[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Machine type.

Posted on 2000-01-31
3
Medium Priority
?
197 Views
Last Modified: 2010-04-15
How can I know the machine type
whether it is Big-Endian or Little-Endian programmatically.
0
Comment
Question by:bhat
3 Comments
 
LVL 2

Accepted Solution

by:
dirc earned 90 total points
ID: 2466821
Assuming a 32-bit machine:

#include <stdio.h>

main(int argc, char **argv)
{
    int i = 0x01020304;
    unsigned char buf[4];
    static unsigned char big[4] = { 0x1, 0x2, 0x3, 0x4};
    static unsigned char little[4] = { 0x4, 0x3, 0x2, 0x1 };

    memcpy(buf, &i, sizeof(buf));

    if (memcmp(buf, big, sizeof(buf)) == 0) {
        printf("Big Endian\n");
    } else if (memcmp(buf, little, sizeof(buf)) == 0) {
        printf("Little Endian\n");
    } else {
        printf("Unknown Layout\n");
    }
}
0
 
LVL 3

Expert Comment

by:ufolk123
ID: 2477117
One more small code

typedef union s
{
int i;
char c[4];
};

main()
{
s u;
u.i=0x11223344;
if(u.c==0x44)
      printf("Little Endian");
else
      printf("Big Endian");
}



ufolk123
0
 
LVL 85

Expert Comment

by:ozo
ID: 2477243
Both routines seem to assume 32 bit int and 8 bit char,
u.c==0x44 is comparing a (char *) with an (int),
But even after that's fixed, I don't think the the C standard actually guarantees that the semantics of union members will work that way.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question