We all know that the char type occupies 8 characters and the int type occupies 16 digits. What will happen when the int value is assigned to char? What are the processing rules?
Method 1:
Write the following code for testing:
[Html]
# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Char sum;
Int operator1 = 4874;
// 4874 = 0000 bytes, 1010 bytes, hexadecimal 00 00 13 0A
Sum = operator1;
// Situation 1 sum = 0000 0000; cut the higher bits: 13
// Situation 2 sum = 0000 1010; cut the lower bits: 0A
Printf ("sum = % x \ n", sum );
Return EXIT_SUCCESS;
}
# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Char sum;
Int operator1 = 4874;
// 4874 = 0000 bytes, 1010 bytes, hexadecimal 00 00 13 0A
Sum = operator1;
// Situation 1 sum = 0000 0000; cut the higher bits: 13
// Situation 2 sum = 0000 1010; cut the lower bits: 0A
Printf ("sum = % x \ n", sum );
Return EXIT_SUCCESS;
}
If the value is assigned, the high byte is retained, and the low byte is discarded, and 13 H is printed. On the contrary, if the low byte is retained, the console prints the 0A value;
The following compilation result is:
[Html]
[Root @ localhost program] # vi addoverflowDemo. c
[Root @ localhost program] # gcc-g addoverflowDemo. c-o addoverflowDemo
[Root @ localhost program] #./addoverflowDemo
Sum =
[Root @ localhost program] # vi addoverflowDemo. c
[Root @ localhost program] # gcc-g addoverflowDemo. c-o addoverflowDemo
[Root @ localhost program] #./addoverflowDemo
Sum =
The output under GCC is a, which indicates that the source program retains the low byte and discards the high byte.
Method 2:
In addition, you can also determine the input of the machine by judging its cpu size and mode. The specific method is as follows:
For example, determine the machine size mode:
Judgment code:
[Html]
# Include <stdio. h>
# Include <stdlib. h>
Int checkCPU ();
Int main ()
{
Printf ("little endian: % d \ n", checkCPU ());
Return EXIT_SUCCESS;
}
Int checkCPU ()
{
{
Union w
{
Int;
Char B;
} C;
C. a = 0x12345678;
Return (c. B = 0x78 );
}
}
# Include <stdio. h>
# Include <stdlib. h>
Int checkCPU ();
Int main ()
{
Printf ("little endian: % d \ n", checkCPU ());
Return EXIT_SUCCESS;
}
Int checkCPU ()
{
{
Union w
{
Int;
Char B;
} C;
C. a = 0x12345678;
Return (c. B = 0x78 );
}
}
The source code is explained and analyzed as follows:
The union clause defines the public address space of the variable. Therefore, the starting address of variable a and variable B of the integer type is the same, that is, the initial storage location is the same.
A = 0X12345678
If you want to determine whether the cpu system is large or small, you need to know whether the high byte of 12 is stored in the high address of the memory or low address, if a high byte is stored at the high address end, char (a) occupies only one byte, and its value should be the first address of the Space: 78, which is called the small-end mode;
However, if the char (a) value is 12, it means that the high byte is stored at the low address end. This is the big end mode: refer:
Memory Address, small-end mode, large-end Mode
0x8000 78 12
0x8001 56 34
0x8002 34 56
0x8003 12 78
The output result of the above Code is:
[Html]
[Root @ localhost program] # gcc-g checkendianDemo. c-o checkendianDemo
[Root @ localhost program] #./checkendianDemo
Little endian: 1
[Root @ localhost program] # gcc-g checkendianDemo. c-o checkendianDemo
[Root @ localhost program] #./checkendianDemo
Little endian: 1
The preceding two methods can be used to determine each other.