Advertisements

Search results for union

Example:[1]

            Write a program to swap the 4bytes of an integer. If input is provided as 0x12345678 output is generated as 0x78563412.
          

            #include <stdio.h>
union out
{
   struct
   {
      unsigned char ll;
      unsigned char lh;
      unsigned char hl;
      unsigned char hh;
   }in;
   unsigned int b;
};
int main()
{
   union out aa;
   aa.b = 0x12345678;
   printf("Before swapping:%x\n", aa.b);
   unsigned char ll = aa.in.ll;
   unsigned char lh = aa.in.lh;
   aa.in.ll = aa.in.hh;
   aa.in.hh = ll;
   aa.in.lh = aa.in.hl;
   aa.in.hl = lh;
   printf("After swapping:%x\n", aa.b);
   return 0;
}
          
Example:[2]

            Write a function to determine whether the machine is Little Endian or Big Endian.
          

            #include <stdio.h>
unsigned int EndianCheck();
union endian
{
   unsigned int a;
   char b;
};
int main()
{
   unsigned int res;
   res = EndianCheck();
   if(res == 0)
   {
      printf("Little endian\n");
   }else
   {
      printf("Big endian\n");
   }
   return 0;
}
unsigned int EndianCheck()
{
   union endian test;
   test.a = 1;
   if(test.b == 1)
   {
      return 0;
   }else
   {
      return 1;
   }
}
          

 Search Links


 Popular C search MCQs


 Popular C search examples