Two methods to determine the machine size

Source: Internet
Author: User

Method 1

Idea: Use Pointer to force type conversion

Copy codeThe Code is as follows: # include <stdio. h>
Int main (void)
{
Int data1 = 0x12345678;
Int I;
For (I = 0; I <4; I ++)
{
Printf ("% # x -----> % p \ n", * (char *) & data1 + I), (char *) & data1 + I );
}
Return 0;
}

From the output results, we can see that the high address 0xbfc1b1ff stores 0x12 of the data, and the low address 0xbfc1b1fc stores 0x78 of the data. Therefore, it is a small client. We can also see that the data address actually points to the space for storing low-level data.

Method 2

Idea: Use the shared body to store all data from the same address.

Copy codeThe Code is as follows: # include <stdio. h>

Int main (void)

{

Int I;

Union endian

{

Int data;

Char ch;

} Test;

Test. data = 0x12345678;

If (test. ch = 0x78)

{

Printf ("little endian! \ N ");

}

Else

{

Printf ("big endian! \ N ");

}

For (I = 0; I <4; I ++)

{

Printf ("% # x ------- % p \ n", * (char *) & test. data + I), (char *) & test. data + I );

}

Return 0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.