Unions Of Structure

structure மற்றும் union-னில் nested பயன்படுத்த முடியும். Yes. It is possible to nest unions within union, unions within structure

Example

#include<stdio.h>
#include<conio.h>
struct radio_spec{
	char  brand[30];
	int no_batteries;
};
struct tv_spec{
	int no_speakers;
	int tube_size;
};
struct watch_type{
	float power;
};

struct item_type{
	int item_flag;
	union diff_tue{
		struct radio_spec radio;
		struct tv_spec tv;
		struct watch_type watch;
	};
};

int main(){
	struct item_type itp;
	
	printf("\nEnter Brand: ");
	scanf("%s",itp.radio.brand);
	printf("\nEnter no.of batteries: ");
	scanf("%d",&itp.radio.no_batteries);
	
	printf("\nEnter no.of speaker: ");
	scanf("%d",&itp.tv.no_speakers);
	printf("\nEnter tube size: ");
	scanf("%d",&itp.tv.tube_size);
	
	printf("\nEnter power supply: ");
	scanf("%f",&itp.watch.power);
	
	printf("\nYour records are: ");
	printf("\n %s %d %d %d %f",itp.radio.brand,itp.radio.no_batteries,itp.tv.no_speakers,itp.tv.tube_size,itp.watch.power);
return 0;	
}	

Output:

Enter Brand: HMT
Enter no.of batteries: 3
Enter no.of speaker:1
Enter tube size: 5
Enter power supply: 2.4

Your records are:
HMT  3 1 5 2.4

Comments