C language union

This is a structure that I have never understood, and now I think about it and record my thoughts. If there are mistakes, I hope everyone can correct them.

The definition format of the union:

union 共用体名{
    成员列表
};

The definition is similar to the structure. The keyword of the structure is struct, and the keyword of the union is union. Each member of the structure occupies different memory and has no influence on each other; while all members of the union occupy the same memory, Modifying one member affects all other members.

The difference between union and structure (difference between Union and typedef ) , but I still want to express my original thinking.

The first is to see the program below.

#include<stdio.h>
int main(){
	union{
		char ch[2];
		int d;
	}s;
	s.d=0x1234;
	printf("%d\n",sizeof(s));//4
	printf("%x,%x\n",s.ch[0],s.ch[1]);//34,12
	return 0;
}

First of all, in this program, the s variable has two member variables, one is a character array and the other is an integer variable. We know that int is 4 bytes and char is 1 byte, so s occupies four bytes, and then The d variable is assigned a value, d and ch share an area, and the corresponding s also has a value, so the value in the ch array is not a random number. The low byte is stored in the low address. So ch[0] is the low bit, and ch [1] is the high bit. 0x1234 is the hexadecimal number 1234. The advantage of using hexadecimal is that exactly two bits are a byte. In the character array, %x is used to output the hexadecimal, which means It is to output the number of a byte as a hexadecimal integer, so it has nothing to do with ascii here, it is to output the stored data. After all, characters and integers are stored in the form of complement code in the computer. Characters It is converted to integer storage according to ascii.

#include<stdio.h>
int main(){
	union{
		char ch[4];
		int d;
	}s;
	s.d=0x12344578;
    //78,45,34,12
	printf("%x,%x,%x,%x\n",s.ch[0],s.ch[1],s.ch[2],s.ch[3]);
	//120,69,52,18
	printf("%d,%d,%d,%d\n",s.ch[0],s.ch[1],s.ch[2],s.ch[3]);
	//x,E,4,.
	printf("%c,%c,%c,%c\n",s.ch[0],s.ch[1],s.ch[2],s.ch[3]);
	return 0;
}

Then look at the next program.

#include<iostream>
using namespace std;
union{
	int i;
	double d;
}x;
union{
	int i;
	double d;
}y;
int main(){
	x.i=3;x.d=4.5;
	y.i=4;y.d=8.3;
	//0,4.5,-1717986918,8.3
	cout<<x.i<<","<<x.d<<","<<y.i<<","<<y.d<<endl;
	return 0;
}

It feels very strange to see the output. Why is xi equal to 0 and yi equal to a random number? So is it really a random number?

We already know that they share an area, so the value of xi is changed by modifying the value of xd, that is, the value has been modified, not that there is no value, so why is his output so strange? , it is related to the storage of floating-point numbers in the computer. We all know integers and characters, so how are floating-point numbers stored in the computer?

Detailed explanation of how floating-point numbers are stored in computers_In what form are floating-point constants stored in computers_Small C Blog Blog-CSDN Bloghttps://blog.csdn.net/lws123253/article/details/80266824So we know The storage mode of floating-point numbers is not in the form of complement code, which also explains why long occupies 8 bytes, but its value range is not as large as that of 4-byte float.

At that time, I looked at the definition of union and felt that its value was not a random number, but I didn't know how to tell people.

That's the end of it. Although I also know that the article is a bit watery this time, I still hope to help everyone, because I didn't understand it well when I was studying.

Guess you like

Origin blog.csdn.net/weixin_64066303/article/details/130607056