Q:

C program to extract individual bytes from an unsigned int using union

belongs to collection: C Structure and Union programs

0

C program to extract individual bytes from an unsigned int using union

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C union program to extract individual bytes from an unsigned int

#include<stdio.h>

union tagname
{
	unsigned int a;
	unsigned char s[4];
};

union tagname object;

int main()
{
	char i; //for loop counter

	//assign an integer number
	object.a=0xAABBCCDD;

	printf("Integer number: %ld, hex: %X\n",object.a,object.a);

	printf("Indivisual bytes: ");
	for(i=3;i>=0;i--)
		printf("%02X ",object.s[i]);

	printf("\n");
	return 0;
}

Output

Integer number: 2864434397, hex: AABBCCDD
Indivisual bytes: AA BB CC DD

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program for passing structures as function argum... >>
<< C program to add two distances in feet and inches ...