How to Use the union in C language to test the size-end mode of the system

Source: Internet
Author: User

First, parse the shared body and size mode.

In the C language, several different variables share the same memory structure, which is called the "shared body" structure. The Memory Length occupied by the shared body variable is equal to the length of the longest member.

The general form of defining a shared object type variable is:

Union shared body name

{

Member Columns

} Variable table column;

In the big-end format, the high byte of word data is stored in the low address, while the low byte of word data is stored in the high address. The small-end format is opposite to the big-end storage format.

The use of union is affected by the system size. For example, the storage formats of the following shared body variables in the large-end mode and small-end mode are as follows:

Union check

{

Int I;

Char c;

} Cc;


Big end mode:


Int I = 1;

0x0 0x0 0x0 0x1

Low address and high address


Small-end mode:


Int I = 1;

0x0 0x0 0x0 0x1

High address and low address


From the above analysis, you can write the following code to determine the size of the Terminal Mode:


# Include <stdio. h>

Int checkSys ()

{

Union check

{

Int I;

Char c;

} Cc;

Cc. I = 1;

Return (cc. c = 1 );

}


Int main ()

{

If (0! = CheckSys ())

{

Printf ("the current system is a small-end system ");

}

Else

{

Printf ("the current system is a big-end system ");

}

Return 0;

}

This article is from the "Ming" blog, please be sure to keep this source http://8010089.blog.51cto.com/8000089/1304437

Related Article

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.